Make calls to api for changing match scores

This commit is contained in:
Felix Hamme 2019-06-06 11:41:35 +02:00
parent 85a36a02c9
commit 4e8a690d2a
5 changed files with 142 additions and 53 deletions

View File

@ -235,6 +235,31 @@ const reducerTournamentinfo = (state = defaultStateTournamentinfo, action) => {
action.parameters.errorCallback(); action.parameters.errorCallback();
}); });
return Object.assign({}, state, {}); return Object.assign({}, state, {});
case actionTypesTournamentinfo.SUBMIT_MATCH_SCORES:
patchRequest(action.state, '/match_scores/' + action.parameters.scoreIdTeam1, {
points: action.parameters.scoreTeam1
}).then(resp => {
storeOptionalToken(resp);
patchRequest(action.state, '/match_scores/' + action.parameters.scoreIdTeam2, {
points: action.parameters.scoreTeam2
}).then(resp => {
storeOptionalToken(resp);
action.parameters.successCallback();
}).catch(error => {
if (error.response) {
storeOptionalToken(error.response);
}
action.parameters.errorCallback();
});
}).catch(error => {
if (error.response) {
storeOptionalToken(error.response);
}
action.parameters.errorCallback();
});
return Object.assign({}, state, {});
case actionTypesTournamentinfo.END_MATCH: case actionTypesTournamentinfo.END_MATCH:
patchRequest(action.state, '/matches/' + action.parameters.matchId, { patchRequest(action.state, '/matches/' + action.parameters.matchId, {
state: 'finished' state: 'finished'
@ -414,6 +439,21 @@ export function endMatch(matchId, successCallback, errorCallback) {
}); });
} }
export function submitMatchScores(scoreTeam1, scoreIdTeam1, scoreTeam2, scoreIdTeam2, successCallback, errorCallback) {
__store.dispatch({
type: actionTypesTournamentinfo.SUBMIT_MATCH_SCORES,
parameters: {
scoreTeam1: scoreTeam1,
scoreIdTeam1: scoreIdTeam1,
scoreTeam2: scoreTeam2,
scoreIdTeam2: scoreIdTeam2,
successCallback: successCallback,
errorCallback: errorCallback
},
state: __store.getState()
});
}
export function getState() { export function getState() {
return __store.getState(); return __store.getState();
} }

View File

@ -6,13 +6,13 @@ export function EditableMatchTable(props) {
<tbody> <tbody>
<tr> <tr>
<td className='scoreInput border-top-0'> <td className='scoreInput border-top-0'>
<ScoreInput score={props.match.team1.score}/> <ScoreInput score={props.match.team1.score} update={props.updateScoreTeam1}/>
</td> </td>
<td className='align-middle border-top-0'>{props.match.team1.name}</td> <td className='align-middle border-top-0'>{props.match.team1.name}</td>
</tr> </tr>
<tr> <tr>
<td className='scoreInput'> <td className='scoreInput'>
<ScoreInput score={props.match.team2.score}/> <ScoreInput score={props.match.team2.score} update={props.updateScoreTeam2}/>
</td> </td>
<td className='align-middle'>{props.match.team2.name}</td> <td className='align-middle'>{props.match.team2.name}</td>
</tr> </tr>
@ -24,28 +24,34 @@ class ScoreInput extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = {score: this.props.score}; this.state = {score: this.props.score};
this.updateScore = this.updateScore.bind(this); this.inputScore = this.inputScore.bind(this);
this.increaseScore = this.increaseScore.bind(this); this.increaseScore = this.increaseScore.bind(this);
this.decreaseScore = this.decreaseScore.bind(this); this.decreaseScore = this.decreaseScore.bind(this);
} }
updateScore(event) { inputScore(event) {
this.setState({score: event.target.value}); const newScore = event.target.value;
this.setState({score: newScore});
this.props.update(newScore);
} }
increaseScore() { increaseScore() {
this.setState({score: Number(this.state.score) + 1}); const newScore = Number(this.state.score) + 1;
this.setState({score: newScore});
this.props.update(newScore);
} }
decreaseScore() { decreaseScore() {
this.setState({score: Number(this.state.score) - 1}); const newScore = Number(this.state.score) - 1;
this.setState({score: newScore});
this.props.update(newScore);
} }
render() { render() {
return (<InputGroup> return (<InputGroup>
<InputGroupAddon addonType="prepend"><Button onClick={this.decreaseScore} color='danger' <InputGroupAddon addonType="prepend"><Button onClick={this.decreaseScore} color='danger'
outline={true}>-1</Button></InputGroupAddon> outline={true}>-1</Button></InputGroupAddon>
<Input className='font-weight-bold' value={this.state.score} onChange={this.updateScore} type='number' <Input className='font-weight-bold' value={this.state.score} onChange={this.inputScore} type='number'
step='1' placeholder='0'/> step='1' placeholder='0'/>
<InputGroupAddon addonType="append"><Button onClick={this.increaseScore} <InputGroupAddon addonType="append"><Button onClick={this.increaseScore}
color='success'>+1</Button></InputGroupAddon> color='success'>+1</Button></InputGroupAddon>

View File

@ -1,24 +1,62 @@
import {Button, Modal, ModalBody, ModalFooter, ModalHeader} from 'reactstrap'; import {Button, Modal, ModalBody, ModalFooter, ModalHeader} from 'reactstrap';
import React from 'react'; import React, {Component} from 'react';
import {EditableMatchTable} from './EditableMatchTable'; import {EditableMatchTable} from './EditableMatchTable';
import {MatchTable} from './MatchTable'; import {MatchTable} from './MatchTable';
import {submitMatchScores} from '../api';
import {notify} from 'react-notify-toast';
export function MatchModal(props) { export class MatchModal extends Component {
constructor(props) {
super(props);
this.state = {scoreTeam1: this.props.match.team1.score, scoreTeam2: this.props.match.team2.score};
this.updateScoreTeam1 = this.updateScoreTeam1.bind(this);
this.updateScoreTeam2 = this.updateScoreTeam2.bind(this);
this.submitScores = this.submitScores.bind(this);
this.onSubmitScoresError = this.onSubmitScoresError.bind(this);
this.onSubmitScoresSuccess = this.onSubmitScoresSuccess.bind(this);
}
updateScoreTeam1(newScore) {
this.setState({scoreTeam1: newScore});
}
updateScoreTeam2(newScore) {
this.setState({scoreTeam2: newScore});
}
submitScores() {
const match = this.props.match;
submitMatchScores(this.state.scoreTeam1, match.team1.scoreId, this.state.scoreTeam2, match.team2.scoreId,
this.onSubmitScoresSuccess, this.onSubmitScoresError);
}
onSubmitScoresError() {
this.props.toggle();
notify.show('Der Spielstand konnte nicht geändert werden.', 'error', 2500);
}
onSubmitScoresSuccess() {
this.props.toggle();
notify.show('Der Spielstand wurde geändert.', 'success', 2000);
}
render() {
let title; let title;
let actionButton = ''; let actionButton = '';
let submitScoresButton = ''; let submitScoresButton = '';
let matchTable = <MatchTable match={props.match} matchStatus={props.match.state}/>; let matchTable = <MatchTable match={this.props.match} matchStatus={this.props.match.state}/>;
// possible states: single_team not_ready not_started in_progress finished // possible states: single_team not_ready not_started in_progress finished
switch (props.match.state) { switch (this.props.match.state) {
case 'in_progress': case 'in_progress':
title = 'Spiel läuft'; title = 'Spiel läuft';
submitScoresButton = <Button color='primary'>Spielstand ändern</Button>; submitScoresButton = <Button color='primary' onClick={this.submitScores}>Spielstand ändern</Button>;
if (!props.match.allowUndecided && props.match.team1.score === props.match.team2.score) { if (!this.props.match.allowUndecided && this.props.match.team1.score === this.props.match.team2.score) {
actionButton = <Button color='primary' disabled>Spiel beenden</Button>; actionButton = <Button color='primary' disabled>Spiel beenden</Button>;
} else { } else {
actionButton = <Button color='primary' onClick={props.endMatch}>Spiel beenden</Button>; actionButton = <Button color='primary' onClick={this.props.endMatch}>Spiel beenden</Button>;
} }
matchTable = <EditableMatchTable match={props.match}/>; matchTable = <EditableMatchTable match={this.props.match} updateScoreTeam1={this.updateScoreTeam1}
updateScoreTeam2={this.updateScoreTeam2}/>;
break; break;
case 'finished': case 'finished':
title = 'Spiel beendet'; title = 'Spiel beendet';
@ -31,18 +69,19 @@ export function MatchModal(props) {
break; break;
case 'not_started': case 'not_started':
title = 'Spiel kann gestartet werden'; title = 'Spiel kann gestartet werden';
actionButton = <Button color='primary' onClick={props.startMatch}>Spiel starten</Button>; actionButton = <Button color='primary' onClick={this.props.startMatch}>Spiel starten</Button>;
break; break;
} }
return (<Modal isOpen={props.isOpen} toggle={props.toggle}> return (<Modal isOpen={this.props.isOpen} toggle={this.props.toggle}>
<ModalHeader toggle={props.toggle}>{title}</ModalHeader> <ModalHeader toggle={this.props.toggle}>{title}</ModalHeader>
<ModalBody> <ModalBody>
{matchTable} {matchTable}
</ModalBody> </ModalBody>
<ModalFooter> <ModalFooter>
{submitScoresButton} {submitScoresButton}
{actionButton} {actionButton}
<Button color='secondary' onClick={props.toggle}>Abbrechen</Button> <Button color='secondary' onClick={this.props.toggle}>Abbrechen</Button>
</ModalFooter> </ModalFooter>
</Modal>); </Modal>);
} }
}

View File

@ -9,6 +9,7 @@ export const actionTypesTournamentinfo = {
'MODIFY_TOURNAMENT_ERROR': 'MODIFY_TOURNAMENT_ERROR', 'MODIFY_TOURNAMENT_ERROR': 'MODIFY_TOURNAMENT_ERROR',
'START_MATCH': 'START_MATCH', 'START_MATCH': 'START_MATCH',
'SUBMIT_MATCH_SCORES': 'SUBMIT_MATCH_SCORES',
'END_MATCH': 'END_MATCH', 'END_MATCH': 'END_MATCH',
'REHYDRATE': 'TOURNAMENTINFO_REHYDRATE', 'REHYDRATE': 'TOURNAMENTINFO_REHYDRATE',

View File

@ -129,18 +129,21 @@ function convertMatch(apiMatch, allowUndecided) {
result.team1 = { result.team1 = {
name: apiMatch.match_scores[0].team.name, name: apiMatch.match_scores[0].team.name,
id: apiMatch.match_scores[0].team.id, id: apiMatch.match_scores[0].team.id,
score: apiMatch.match_scores[0].points score: apiMatch.match_scores[0].points,
scoreId: apiMatch.match_scores[0].id
}; };
result.team2 = { result.team2 = {
name: apiMatch.match_scores[1].team.name, name: apiMatch.match_scores[1].team.name,
id: apiMatch.match_scores[1].team.id, id: apiMatch.match_scores[1].team.id,
score: apiMatch.match_scores[1].points score: apiMatch.match_scores[1].points,
scoreId: apiMatch.match_scores[1].id
}; };
} else if (apiMatch.match_scores.length === 1) { } else if (apiMatch.match_scores.length === 1) {
result.team1 = { result.team1 = {
name: apiMatch.match_scores[0].team.name, name: apiMatch.match_scores[0].team.name,
id: apiMatch.match_scores[0].team.id, id: apiMatch.match_scores[0].team.id,
score: apiMatch.match_scores[0].points score: apiMatch.match_scores[0].points,
scoreId: apiMatch.match_scores[0].id
}; };
result.team2 = { result.team2 = {
name: 'TBD', name: 'TBD',