diff --git a/packages/client/src/team/TeamConnector.ts b/packages/client/src/team/TeamConnector.ts index 43577cf..5f18aad 100644 --- a/packages/client/src/team/TeamConnector.ts +++ b/packages/client/src/team/TeamConnector.ts @@ -4,7 +4,6 @@ import {ITeamState} from './TeamReducer' import {TeamActions} from './TeamActions' import {TeamManager} from './TeamManager' import {bindActionCreators} from 'redux' -import {withRouter} from 'react-router-dom' export class TeamConnector extends Connector { constructor(protected readonly teamActions: TeamActions) { @@ -32,6 +31,6 @@ export class TeamConnector extends Connector { TeamManager, ) - return withRouter(Component) + return Component } } diff --git a/packages/client/src/team/TeamEditor.tsx b/packages/client/src/team/TeamEditor.tsx new file mode 100644 index 0000000..931b490 --- /dev/null +++ b/packages/client/src/team/TeamEditor.tsx @@ -0,0 +1,88 @@ +import React from 'react' +import {Button, Control, Heading, Input} from 'bloomer' +import {ITeam} from '@rondo/common' +import {TeamActions} from './TeamActions' +import {FaPlusSquare, FaCheck, FaEdit} from 'react-icons/fa' + +export type ITeamEditorProps = { + type: 'add' + onAddTeam: TeamActions['createTeam'] +} | { + type: 'update' + onUpdateTeam: TeamActions['updateTeam'] + team: ITeam +} + +export interface ITeamEditorState { + name: string +} + +export class TeamEditor +extends React.PureComponent { + constructor(props: ITeamEditorProps) { + super(props) + this.state = { + name: props.type === 'update' ? this.getName(props.team) : '', + } + } + getName(team?: ITeam) { + return team ? team.name : '' + } + componentWillReceiveProps(nextProps: ITeamEditorProps) { + if (nextProps.type === 'update') { + const {team} = nextProps + if (team !== (this.props as any).team) { + this.setState({ + name: this.getName(team), + }) + } + } + } + handleChange = (event: React.ChangeEvent) => { + const name = event.target.value + this.setState({name}) + } + handleSubmit = async (event: React.FormEvent) => { + event.preventDefault() + const {name} = this.state + if (this.props.type === 'update') { + const {team} = this.props + await this.props.onUpdateTeam({id: team.id, name}) + } else { + await this.props.onAddTeam({name}) + } + this.setState({name: ''}) + } + render() { + return ( +
+ + {this.props.type === 'update' ? 'Edit team' : 'Add team'} + + + + + {this.props.type === 'update' ? : } + + +
+ +
+
+ ) + } +} diff --git a/packages/client/src/team/TeamList.tsx b/packages/client/src/team/TeamList.tsx index c130873..47f6070 100644 --- a/packages/client/src/team/TeamList.tsx +++ b/packages/client/src/team/TeamList.tsx @@ -1,12 +1,10 @@ import React from 'react' +import {Button, Panel, PanelHeading, PanelBlock} from 'bloomer' +import {FaEdit, FaTimes} from 'react-icons/fa' import {ITeam, ReadonlyRecord} from '@rondo/common' import {Link} from 'react-router-dom' import {TeamActions} from './TeamActions' -import {FaPlusSquare, FaCheck, FaEdit, FaTimes} from 'react-icons/fa' - -import { - Button, Control, Heading, Input, Panel, PanelHeading, PanelBlock -} from 'bloomer' +import {TeamEditor} from './TeamEditor' export interface ITeamListProps { teamsById: ReadonlyRecord, @@ -14,93 +12,14 @@ export interface ITeamListProps { onAddTeam: TeamActions['createTeam'] onRemoveTeam: TeamActions['removeTeam'] onUpdateTeam: TeamActions['updateTeam'] - editTeamId?: number } export interface ITeamProps { team: ITeam - editTeamId?: number onRemoveTeam: TeamActions['removeTeam'] onUpdateTeam: TeamActions['updateTeam'] } -export interface IAddTeamProps { - onAddTeam: TeamActions['createTeam'] - onUpdateTeam: TeamActions['updateTeam'] - team?: ITeam -} - -export interface IAddTeamState { - name: string -} - -export class TeamAdd extends React.PureComponent { - constructor(props: IAddTeamProps) { - super(props) - this.state = { - name: this.getName(props.team), - } - } - getName(team?: ITeam) { - return team ? team.name : '' - } - componentWillReceiveProps(nextProps: IAddTeamProps) { - const {team} = nextProps - if (team !== this.props.team) { - this.setState({ - name: this.getName(team), - }) - } - } - handleChange = (event: React.ChangeEvent) => { - const name = event.target.value - this.setState({name}) - } - handleSubmit = async (event: React.FormEvent) => { - event.preventDefault() - const {team, onAddTeam, onUpdateTeam} = this.props - const {name} = this.state - if (team) { - await onUpdateTeam({id: team.id, name}) - } else { - await onAddTeam({name}) - } - this.setState({name: ''}) - } - render() { - return ( -
- - {this.props.team ? 'Edit team' : 'Add team'} - - - - - {this.props.team ? : } - - -
- -
-
- ) - } -} - export class TeamRow extends React.PureComponent { handleRemove = async () => { const {onRemoveTeam, team: {id}} = this.props @@ -115,7 +34,7 @@ export class TeamRow extends React.PureComponent {
- @@ -124,6 +43,7 @@ export class TeamRow extends React.PureComponent { aria-label='Remove' onClick={this.handleRemove} isColor='danger' + isInverted > @@ -135,45 +55,30 @@ export class TeamRow extends React.PureComponent { export class TeamList extends React.PureComponent { render() { - const {editTeamId, teamIds, teamsById} = this.props + const {teamIds, teamsById} = this.props return ( - {!editTeamId && ( - - Teams - {teamIds.map(teamId => { - const team = teamsById[teamId] - return ( - - - - ) - })} - - - Teams + {teamIds.map(teamId => { + const team = teamsById[teamId] + return ( + + - - )} + ) + })} - {editTeamId && ( - - - - )} + + + ) } diff --git a/packages/client/src/team/TeamManager.tsx b/packages/client/src/team/TeamManager.tsx index 625e707..63de648 100644 --- a/packages/client/src/team/TeamManager.tsx +++ b/packages/client/src/team/TeamManager.tsx @@ -1,19 +1,13 @@ import React from 'react' -import {History, Location} from 'history' import {ITeam, IUserInTeam, ReadonlyRecord} from '@rondo/common' +import {Route, Switch} from 'react-router-dom' import {TeamActions} from './TeamActions' +import {TeamEditor} from './TeamEditor' import {TeamList} from './TeamList' import {TeamUserList} from './TeamUserList' -import {Title} from 'bloomer' -import {match} from 'react-router' +import {Panel, PanelBlock, PanelHeading} from 'bloomer' export interface ITeamManagerProps { - history: History - location: Location - match: match<{ - teamId: string | undefined - }> - createTeam: TeamActions['createTeam'] updateTeam: TeamActions['updateTeam'] removeTeam: TeamActions['removeTeam'] @@ -36,34 +30,50 @@ export class TeamManager extends React.PureComponent { await this.props.fetchMyTeams() } render() { - const {teamId} = this.props.match.params - const editTeamId = teamId ? Number(teamId) : undefined + const {teamsById} = this.props return (
- Teams - - - - {editTeamId && } - + + + + }/> + { + const {teamId: teamIdParam} = match.params + const teamId = teamIdParam ? Number(teamIdParam) : undefined + const team = teamId ? teamsById[teamId] : undefined + return ( + + + Edit Team: {team && team.name} + + {team && } + {!team && 'No team loaded'} + + + {team && } + + ) + }}/> +
) } diff --git a/packages/client/src/team/TeamUserList.tsx b/packages/client/src/team/TeamUserList.tsx index 4a473f9..739a982 100644 --- a/packages/client/src/team/TeamUserList.tsx +++ b/packages/client/src/team/TeamUserList.tsx @@ -1,5 +1,5 @@ import React from 'react' -import {IUser, IUserInTeam, ReadonlyRecord} from '@rondo/common' +import {ITeam, IUser, IUserInTeam, ReadonlyRecord} from '@rondo/common' import {TeamActions} from './TeamActions' import {FaUser, FaCheck, FaTimes} from 'react-icons/fa' @@ -17,7 +17,7 @@ export interface ITeamUsersProps { onAddUser: TeamActions['addUser'] onRemoveUser: TeamActions['removeUser'] - teamId: number + team: ITeam userKeysByTeamId: ReadonlyRecord> usersByKey: ReadonlyRecord } @@ -136,11 +136,11 @@ export class AddUser extends React.PureComponent { export class TeamUserList extends React.PureComponent { async componentDidMount() { - await this.fetchUsersInTeam(this.props.teamId) + await this.fetchUsersInTeam(this.props.team.id) } async componentWillReceiveProps(nextProps: ITeamUsersProps) { - const {teamId} = nextProps - if (teamId !== this.props.teamId) { - this.fetchUsersInTeam(teamId) + const {team} = nextProps + if (team.id !== this.props.team.id) { + this.fetchUsersInTeam(team.id) } } async fetchUsersInTeam(teamId: number) { @@ -149,7 +149,7 @@ export class TeamUserList extends React.PureComponent { } } render() { - const userKeysByTeamId = this.props.userKeysByTeamId[this.props.teamId] + const userKeysByTeamId = this.props.userKeysByTeamId[this.props.team.id] || EMPTY_ARRAY return ( @@ -172,7 +172,7 @@ export class TeamUserList extends React.PureComponent {