import React from 'react'; import { connect } from 'react-redux'; import { Icon, Form, Input, Button, Popconfirm, message } from 'antd'; import { User, TeamInvite, ProposalDraft } from 'types'; import TeamMemberComponent from './TeamMember'; import { postProposalInvite, deleteProposalInvite } from 'api/api'; import { isValidEmail } from 'utils/validators'; import { AppState } from 'store/reducers'; import './Team.less'; interface State { team: User[]; invites: TeamInvite[]; address: string; } interface StateProps { authUser: AppState['auth']['user']; } interface OwnProps { proposalId: number; initialState?: Partial; updateForm(form: Partial): void; } type Props = OwnProps & StateProps; const MAX_TEAM_SIZE = 6; const DEFAULT_STATE: State = { team: [], invites: [], address: '', }; class CreateFlowTeam extends React.Component { constructor(props: Props) { super(props); this.state = { ...DEFAULT_STATE, ...(props.initialState || {}), }; // Auth'd user is always first member of a team if (props.authUser && !this.state.team.length) { this.state.team[0] = { ...props.authUser, }; } } render() { const { team, invites, address } = this.state; const inviteError = address && !isValidEmail(address) && 'That doesn’t look like a valid email address'; const maxedOut = invites.length >= MAX_TEAM_SIZE - 1; const inviteDisabled = !!inviteError || !address || maxedOut; const pendingInvites = invites.filter(inv => inv.accepted === null); return (
{team.map(user => ( ))} {!!pendingInvites.length && (

Pending invitations

{pendingInvites.map(inv => (
{inv.address}
this.removeInvitation(inv.id)} >
))}
)}

Add a team member

); } private handleChangeInviteAddress = (ev: React.ChangeEvent) => { this.setState({ address: ev.currentTarget.value }); }; private handleAddSubmit = (ev: React.FormEvent) => { ev.preventDefault(); postProposalInvite(this.props.proposalId, this.state.address) .then(res => { const invites = [...this.state.invites, res.data]; this.setState({ invites, address: '', }); this.props.updateForm({ invites }); }) .catch((err: Error) => { console.error('Failed to send invite', err); message.error(err.message, 3); }); }; private removeInvitation = (invId: number) => { deleteProposalInvite(this.props.proposalId, invId) .then(() => { const invites = this.state.invites.filter(inv => inv.id !== invId); this.setState({ invites }); this.props.updateForm({ invites }); }) .catch((err: Error) => { console.error('Failed to remove invite', err); message.error(err.message, 3); }); }; } const withConnect = connect(state => ({ authUser: state.auth.user, })); export default withConnect(CreateFlowTeam);