-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtournament-edit.js
More file actions
395 lines (340 loc) · 14.6 KB
/
Copy pathtournament-edit.js
File metadata and controls
395 lines (340 loc) · 14.6 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import Head from 'next/head';
import React from 'react';
import {connect} from 'react-redux';
import {notify} from 'react-notify-toast';
import {
Col, Container, Button, Card, CardBody, Table, FormGroup, Label
} from 'reactstrap';
import {requestTournament} from '../js/api';
import {TurniereNavigation} from '../js/components/Navigation';
import {BigImage} from '../js/components/BigImage';
import {UserRestrictor, Option} from '../js/components/UserRestrictor';
import {Footer} from '../js/components/Footer';
import {Login} from '../js/components/Login';
import {ErrorPageComponent} from '../js/components/ErrorComponents';
import {updateTournament, updateTeamName} from '../js/api';
import NumericInput from '../js/components/NumericInput';
import {WarningPopup} from '../js/components/WarningPopup';
import 'bootstrap/dist/css/bootstrap.min.css';
import '../static/css/everypage.css';
import '../static/css/index.css';
class EditTournamentPage extends React.Component {
static async getInitialProps({query}) {
return {query};
}
constructor(props) {
super(props);
this.state = {
validCode: false
};
}
componentDidMount() {
requestTournament(this.props.query.code, () => {
this.setState({validCode: true});
if (this._edittournamentcontent != null) {
this._edittournamentcontent.notifyOfContentUpdate();
}
}, () => {
this.setState({validCode: false});
});
}
render() {
const {validCode} = this.state;
const {tournamentname, ownerUsername, isSignedIn, username} = this.props;
return (<UserRestrictor>
<Option condition={validCode && isSignedIn && ownerUsername === username}>
<div className='pb-5'>
<Head>
<title>Turnie.re - Turnier bearbeiten</title>
</Head>
<TurniereNavigation/>
<BigImage text={tournamentname}/>
<EditTournamentContent ref={edittournamentcontent => {
this._edittournamentcontent = edittournamentcontent;
}}/>
<Footer/>
</div>
</Option>
<Option condition={validCode && isSignedIn}>
<ErrorPageComponent statusCode={403}/>
</Option>
<Option condition={!isSignedIn}>
<div className="main generic-fullpage-bg">
<Head>
<title>Turnie.re - Turnier bearbeiten</title>
</Head>
<TurniereNavigation/>
<div>
<Login hint="Sie müssen angemeldet sein, um ein Turnier zu bearbeiten."/>
</div>
<Footer/>
</div>
</Option>
<Option condition={true}>
<ErrorPageComponent statusCode={404}/>
</Option>
</UserRestrictor>);
}
}
function mapStateToTournamentInfo(state) {
const {tournamentname, ownerUsername} = state.tournamentinfo;
const {isSignedIn, username} = state.userinfo;
return {tournamentname, ownerUsername, isSignedIn, username};
}
export default connect(mapStateToTournamentInfo)(EditTournamentPage);
class EditTournamentContent extends React.Component {
render() {
return (<div className='mb-5'>
<ReturnToTournamentButton/>
<EditTournamentPropertiesField ref={field => {
this._edittournamentpropertiesfield = field;
}}/>
<EditTeamField ref={field => {
this._editteamfield = field;
}}/>
</div>);
}
notifyOfContentUpdate() {
this._edittournamentpropertiesfield.notifyOfContentUpdate();
this._editteamfield.notifyOfContentUpdate();
}
}
function ReturnToTournamentButton() {
return (<Container className="px-0">
<Button color="secondary" className="mb-5 w-100" href="./">Zurück zum Turnier</Button>
</Container>);
}
class EditTournamentPropertiesField extends React.Component {
render() {
return (<Card className="container">
<CardBody>
<h2>Turnier-Eigenschaften ändern</h2>
<VisibleEditTournamentForm ref={form => {
this._visibleedittournamentform = form;
}}/>
</CardBody>
</Card>);
}
notifyOfContentUpdate() {
this._visibleedittournamentform.getWrappedInstance().notifyOfContentUpdate();
}
}
class EditTournamentForm extends React.Component {
constructor(props) {
super(props);
this.state = {
name: '',
description: '',
isPublic: false,
playoffTeamsAmount: 0,
instantFinalistAmount: 0,
intermediateRoundParticipants: 0
};
this.increasePlayoffTeamsAmount = this.increasePlayoffTeamsAmount.bind(this);
this.decreasePlayoffTeamsAmount = this.decreasePlayoffTeamsAmount.bind(this);
this.increaseInstantFinalistsAmount = this.increaseInstantFinalistsAmount.bind(this);
this.decreaseInstantFinalistsAmount = this.decreaseInstantFinalistsAmount.bind(this);
this.increaseIntermediateRoundParticipants = this.increaseIntermediateRoundParticipants.bind(this);
this.decreaseIntermediateRoundParticipants = this.decreaseIntermediateRoundParticipants.bind(this);
}
render() {
const {name, description, isPublic} = this.state;
return (<div>
<div className="form-group">
<label htmlFor="name">Turnier-Name</label>
<input className="form-control" type="text" name="name" id="edittournament-textfield-name"
value={name} placeholder={name} onChange={this.handleNameInput.bind(this)}/>
</div>
<div className="form-group">
<label htmlFor="name">Turnier-Beschreibung</label>
<input className="form-control" type="text" name="name" id="edittournament-textfield-description"
value={description} placeholder={description}
onChange={this.handleDescriptionInput.bind(this)}/>
</div>
<div className="form-group custom-control custom-checkbox">
<input className="custom-control-input" type="checkbox" name="isPublic"
id="edittournament-checkbox-isPublic" value={isPublic}
onChange={this.handlePublicInput.bind(this)}/>
<label htmlFor="isPublic" className="custom-control-label">Das Turnier öffentlich anzeigen</label>
</div>
<WarningPopup
text="Die Anzahl der Teams im Playoff muss der Anzahl an Teams, die direkt im Playoff sind
plus der Hälfte der Anzahl an Teams in der Zwischenrunde entsprechen."
shown={this.state.playoffTeamsAmount !== this.state.instantFinalistAmount +
(this.state.intermediateRoundParticipants / 2)}>
<FormGroup>
<Label for="playoff-teams-amount">Anzahl Teams in der Playoff-Stage</Label>
<Col xs="3" className="pl-0">
<NumericInput value={this.state.playoffTeamsAmount}
incrementText="×2" incrementCallback={this.increasePlayoffTeamsAmount}
decrementText="÷2" decrementCallback={this.decreasePlayoffTeamsAmount}/>
</Col>
</FormGroup>
<FormGroup>
<Label for="instant-finalists-amount">
Anzahl Teams, die direkt in die Playoff-Stage weiter kommen</Label>
<Col xs="3" className="pl-0">
<NumericInput value={this.state.instantFinalistAmount}
incrementText="+1" incrementCallback={this.increaseInstantFinalistsAmount}
decrementText="-1" decrementCallback={this.decreaseInstantFinalistsAmount}/>
</Col>
</FormGroup>
<FormGroup>
<Label for="intermediate-round-participants">
Anzahl Teams, die in einer Zwischenrunde um die Playoff-Stage spielen müssen</Label>
<Col xs="3" className="pl-0">
<NumericInput value={this.state.intermediateRoundParticipants}
incrementText="+1" incrementCallback={this.increaseIntermediateRoundParticipants}
decrementText="-1" decrementCallback={this.decreaseIntermediateRoundParticipants}/>
</Col>
</FormGroup>
</WarningPopup>
<div className="form-group">
<div className="input-group">
<Button color="success" className="px-5" id="edittournament-button"
onClick={this.handleClick.bind(this)}>Ändern</Button>
</div>
</div>
</div>);
}
notifyOfContentUpdate() {
const {name, description, isPublic, playoffTeamsAmount, instantFinalistAmount,
intermediateRoundParticipants} = this.props;
this.setState({
playoffTeamsAmount: playoffTeamsAmount,
instantFinalistAmount: instantFinalistAmount,
intermediateRoundParticipants: intermediateRoundParticipants,
name: name ? name : '', description: description ? description : '', isPublic: isPublic
});
}
handleClick() {
if (this.state.name !== '' && this.state.playoffTeamsAmount === this.state.instantFinalistAmount +
(this.state.intermediateRoundParticipants / 2)) {
updateTournament(this.generateUpdatedTeam(), () => {
notify.show('Turnier wurde erfolgreich geändert.', 'success', 5000);
}, () => {
notify.show('Turnier konnte nicht aktualisiert werden.', 'warning', 5000);
});
} else {
notify.show('Bitte korrigiere deine Eingaben zuerst.', 'warning', 5000);
}
}
generateUpdatedTeam() {
return {
id: this.props.id,
name: this.state.name,
description: this.state.description,
public: this.state.isPublic,
playoff_teams_amount: this.state.playoffTeamsAmount,
instant_finalists_amount: this.state.instantFinalistAmount,
intermediate_round_participants_amount: this.state.intermediateRoundParticipants
};
}
handleNameInput(input) {
this.setState({name: input.target.value});
}
handleDescriptionInput(input) {
this.setState({description: input.target.value});
}
handlePublicInput(input) {
this.setState({public: input.target.value});
}
increasePlayoffTeamsAmount() {
this.setState({playoffTeamsAmount: this.state.playoffTeamsAmount * 2});
}
decreasePlayoffTeamsAmount() {
if (this.state.playoffTeamsAmount > 1) {
this.setState({playoffTeamsAmount: Math.floor(this.state.playoffTeamsAmount / 2)});
}
}
increaseInstantFinalistsAmount() {
this.setState({instantFinalistAmount: this.state.instantFinalistAmount + 1});
}
decreaseInstantFinalistsAmount() {
if (this.state.instantFinalistAmount > 0) {
this.setState({instantFinalistAmount: this.state.instantFinalistAmount - 1});
}
}
increaseIntermediateRoundParticipants() {
this.setState({intermediateRoundParticipants: this.state.intermediateRoundParticipants + 1});
}
decreaseIntermediateRoundParticipants() {
if (this.state.intermediateRoundParticipants > 0) {
this.setState({intermediateRoundParticipants: this.state.intermediateRoundParticipants - 1});
}
}
}
function mapStateToTournamentFormProps(state) {
const {name, id, description, isPublic, stages, playoffTeamsAmount, instantFinalistAmount,
intermediateRoundParticipants} = state.tournamentinfo;
return {name, id, description, isPublic, stages, playoffTeamsAmount, instantFinalistAmount,
intermediateRoundParticipants};
}
const VisibleEditTournamentForm = connect(mapStateToTournamentFormProps, null, null,
{withRef: true})(EditTournamentForm);
class EditTeamField extends React.Component {
render() {
return (<Card className="container my-4">
<CardBody>
<h2>Team-Namen ändern</h2>
<VisibleEditTeamNamesForm ref={form => {
this._visibleeditteamnamesform = form;
}}/>
</CardBody>
</Card>);
}
notifyOfContentUpdate() {
this._visibleeditteamnamesform.getWrappedInstance().notifyOfContentUpdate();
}
}
class EditTeamNamesForm extends React.Component {
constructor(props) {
super(props);
this.state = {
teams: []
};
}
render() {
const {teams} = this.state;
return (<div>
<Table className="table-striped mt-3">
<tbody>
{teams.map((team, index) => <tr key={index}>
<td><Button outline size="sm" className="changeTeamnameButton"
id={'editteam-button-team_' + team.id}
onClick={this.handleClick.bind(this, index)}>Ändern</Button></td>
<td className="w-100"><input className="form-control" type="text"
id={'editteam-textfield-team_' + team.id} value={team.name}
placeholder={team.name}
onChange={this.handleNameInput.bind(this, index)}/></td>
</tr>)}
</tbody>
</Table>
</div>);
}
notifyOfContentUpdate() {
const {teams} = this.props;
this.setState({
teams: teams
});
}
handleNameInput(index, input) {
const team = this.state.teams.slice();
team[index].name = input.target.value;
this.setState({
teams: team
});
}
handleClick(index) {
updateTeamName(this.state.teams[index], () => {
notify.show('Team Name wurde erfolgreich geändert.', 'success', 5000);
}, () => {
notify.show('Team Name konnte nicht geändert werden.', 'warning', 5000);
});
}
}
function mapStateToTeamFormProps(state) {
const {teams} = state.tournamentinfo;
return {teams};
}
const VisibleEditTeamNamesForm = connect(mapStateToTeamFormProps, null, null, {withRef: true})(EditTeamNamesForm);