import React from 'react' import {ITeam, ReadonlyRecord} from '@rondo/common' import {Link} from 'react-router-dom' import {TeamActions} from './TeamActions' import {FaPlusSquare, FaSave, FaEdit, FaTimes} from 'react-icons/fa' import { Button, Control, Heading, Input, Panel, PanelHeading, PanelBlock } from 'bloomer' export interface ITeamListProps { teamsById: ReadonlyRecord, teamIds: ReadonlyArray, 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}) } } render() { return (
{this.props.team ? 'Edit team' : 'Add team'}
) } } export class TeamRow extends React.PureComponent { handleRemove = async () => { const {onRemoveTeam, team: {id}} = this.props await onRemoveTeam({id}) } render() { const {team} = this.props return (
{team.name}
 
) } } export class TeamList extends React.PureComponent { render() { const {editTeamId, teamIds, teamsById} = this.props return ( {!editTeamId && ( Teams {teamIds.map(teamId => { const team = teamsById[teamId] return ( ) })} )} {editTeamId && ( )} ) } }