-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCreateMatch.js
More file actions
140 lines (122 loc) · 3.86 KB
/
Copy pathCreateMatch.js
File metadata and controls
140 lines (122 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import React, { Component } from 'react'
import {
Title,
GridContainer,
Box,
} from '../../../styles/blocks'
import { connect } from 'react-redux'
import { withRouter } from 'react-router-dom'
import { getPlayers } from '../../modules/players/players-selectors'
import { SelectTeamForm } from './SelectTeamForm'
import { MatchesActions } from '../../modules/matches/matches-actions'
import { StatusType } from '../../modules/api/request-status'
import { SnackbarAlert } from '../SnackbarAlert/SnackbarAlert'
import { RootActions } from '../../modules/root/root-actions'
import { CircularProgress } from '@material-ui/core'
export class CreateMatchComponent extends Component {
constructor(props) {
super(props)
this.state = {
team1: [],
team2: [],
}
}
team1Changed = team1 => {
this.setState({
team1,
})
}
team2Changed = team2 => {
this.setState({
team2,
})
}
hasEnoughPlayersOnTeam = team => team.length >= this.props.minPlayerNumber
arePlayersDistinct = () => {
const allPlayers = [...this.state.team1, ...this.state.team2]
const distinctPlayersSet = new Set(allPlayers)
return allPlayers.length === distinctPlayersSet.size
}
getInputErrorMessage = () => {
if (!this.hasEnoughPlayersOnTeam(this.state.team1)) {
return 'Not enough players on Team 1'
}
if (!this.hasEnoughPlayersOnTeam(this.state.team2)) {
return 'Not enough players on Team 2'
}
if (!this.arePlayersDistinct()) {
return 'A single player can\'t be selected twice'
}
return null
}
submit = team1Won => {
if (this.getInputErrorMessage()) {
return
}
this.props.createMatch({
team1: this.state.team1,
team2: this.state.team2,
team1Score: team1Won? 1 : 0,
team2Score: team1Won? 0 : 1,
})
}
componentWillReceiveProps = newProps => {
const redirect = newProps.activeRedirect
if (redirect != null) {
newProps.history.push(newProps.constructUrl(redirect))
newProps.dismissRedirect()
}
}
render = () => {
const errorMessage = this.getInputErrorMessage()
const canSubmit = !errorMessage && this.props.status.type !== StatusType.IN_PROGRESS
const progress = this.props.status.type === StatusType.IN_PROGRESS
? <CircularProgress variant="indeterminate" />
: null
return (
<>
<Title Margin="10px 0">Match</Title>
<GridContainer Column="1fr 1fr">
<Box Margin="0 10px 10px" Padding="10px 10px 20px">
<SelectTeamForm
id="team1"
maxPlayerNumber={this.props.maxPlayerNumber}
teamName="Team 1"
players={this.props.players}
teamChanged={this.team1Changed}
teamSubmitted={() => this.submit(true)}
canSubmit={canSubmit} />
</Box>
<Box Margin="0 10px 10px" Padding="10px 10px 20px">
<SelectTeamForm
id="team2"
maxPlayerNumber={this.props.maxPlayerNumber}
teamName="Team 2"
players={this.props.players}
teamChanged={this.team2Changed}
teamSubmitted={() => this.submit(false)}
canSubmit={canSubmit} />
</Box>
</GridContainer>
<div>{errorMessage || ''}</div>
<SnackbarAlert />
{progress}
</>
)
}
}
const mapStateToProps = state => ({
players: getPlayers(state),
status: state.matchesStatus,
activeRedirect: state.activeRedirect,
})
const mapDispatchToProps = dispatch => ({
createMatch: match => {
dispatch(MatchesActions.Creators.addMatch(match))
},
dismissRedirect: () => {
dispatch(RootActions.Creators.dismissRedirect())
},
})
const RoutingCreateMatchComponent = withRouter(CreateMatchComponent)
export const CreateMatch = connect(mapStateToProps, mapDispatchToProps)(RoutingCreateMatchComponent)