import Head from 'next/head';
import React from 'react';
import { connect } from 'react-redux';
import {
Button,
Card,
CardBody,
Col,
Container,
Input,
InputGroup,
InputGroupAddon,
ListGroup,
ListGroupItem,
Modal,
ModalBody,
ModalFooter,
ModalHeader,
Row,
Table
} from 'reactstrap';
import { ErrorPageComponent } from '../js/components/ErrorComponents';
import { Footer } from '../js/components/Footer';
import { TurniereNavigation } from '../js/components/Navigation';
import { BigImage } from '../js/components/BigImage';
import {
getRequest,
getState,
verifyCredentials
} from '../js/api';
import 'bootstrap/dist/css/bootstrap.min.css';
import '../static/everypage.css';
import '../static/css/tournament.css';
class PrivateTournamentPage extends React.Component {
render() {
const { id, description, isPublic, code, ownerUsername, playoffStages } = this.props.tournament;
const { isSignedIn, username } = this.props;
// TODO: Change href-prop of the anchor tag to contain the tournament code
return (
{description}
{isPublic ? 'Das Turnier ist öffentlich.' : 'Das Turnier ist privat.'}
Turnier-Code: {code}
von {ownerUsername}
{playoffStages.map(stage =>
)}
);
}
}
function mapStateToTournamentPageProperties(state) {
const { isSignedIn, username } = state.userinfo;
return { isSignedIn, username };
}
const TournamentPage = connect(
mapStateToTournamentPageProperties
)(PrivateTournamentPage);
function EditButton(props) {
const { id, ownerName, isSignedIn, username } = props;
if(isSignedIn && ownerName === username) {
return (
Turnier bearbeiten
);
} else {
return null;
}
}
function getLevelName(levelNumber) {
const names = ['Finale', 'Halbfinale', 'Viertelfinale', 'Achtelfinale'];
if(levelNumber < names.length){
return names[levelNumber];
}else {
return Math.pow(2, levelNumber) + 'tel-Finale';
}
}
function Stage(props) {
return (
{props.level}
{props.matches.map((match => (
)))}
);
}
class Match extends React.Component {
constructor(props) {
super(props);
this.state = {
modal: false
};
this.toggleModal = this.toggleModal.bind(this);
}
toggleModal() {
const { isSignedIn, isOwner } = this.props;
if(isSignedIn && isOwner) {
this.setState({modal: !this.state.modal});
}
}
render() {
let cardClass, smallMessage, borderClass;
//possible states: single_team not_ready not_started in_progress team1_won team2_won undecided
switch (this.props.match.state) {
case 'in_progress':
cardClass = 'table-warning';
borderClass = 'border-warning';
smallMessage = 'Spiel läuft';
break;
case 'team1_won':
cardClass = 'table-success';
borderClass = 'border-success';
smallMessage = 'Gewinner: ' + this.props.match.team1;
break;
case 'team2_won':
cardClass = 'table-success';
borderClass = 'border-success';
smallMessage = 'Gewinner: ' + this.props.match.team2;
break;
case 'single_team':
cardClass = 'table-success';
borderClass = 'border-success';
smallMessage = 'kein Gegner, Team kommt weiter';
break;
case 'not_ready':
smallMessage = 'Spiel kann noch nicht gestartet werden';
break;
case 'not_started':
smallMessage = 'Spiel kann gestartet werden';
break;
case 'undecided':
cardClass = 'table-success';
borderClass = 'border-success';
smallMessage = 'Spiel beendet, unentschieden';
break;
}
return (
{smallMessage}
);
}
}
function MatchModal(props) {
let title;
let actionButton = '';
//possible states: single_team not_ready not_started in_progress team1_won team2_won undecided
switch (props.match.state) {
case 'in_progress':
title = 'Spiel läuft';
actionButton = ;
break;
case 'team1_won':
title = 'Spiel beendet';
break;
case 'team2_won':
title = 'Spiel beendet';
break;
case 'single_team':
title = 'kein Gegner, Team kommt weiter';
break;
case 'not_ready':
title = 'Spiel kann noch nicht gestartet werden';
break;
case 'not_started':
title = 'Spiel kann gestartet werden';
actionButton = ;
break;
case 'undecided':
title = 'Spiel beendet';
break;
}
return (
{title}
{props.match.state === 'in_progress' ? :
}
{actionButton}
);
}
function MatchTable(props) {
let team1Class, team2Class;
//possible states: single_team not_ready not_started in_progress team1_won team2_won undecided
switch (props.match.state) {
case 'in_progress':
break;
case 'team1_won':
team1Class = 'font-weight-bold';
team2Class = 'lost-team';
break;
case 'team2_won':
team1Class = 'lost-team';
team2Class = 'font-weight-bold';
break;
case 'single_team':
team2Class = 'text-muted';
break;
case 'not_ready':
break;
case 'not_started':
break;
case 'undecided':
break;
}
if(props.match.state === 'single_team'){
return (
| {props.match.team1} |
| kein Gegner |
);
} else {
return (
| {props.match.scoreTeam1} |
{props.match.team1} |
| {props.match.scoreTeam2} |
{props.match.team2} |
);
}
}
function EditableMatchTable(props) {
return (
|
|
{props.match.team1} |
|
|
{props.match.team2} |
);
}
class ScoreInput extends React.Component {
constructor(props) {
super(props);
this.state = {score: this.props.score};
this.updateScore = this.updateScore.bind(this);
this.increaseScore = this.increaseScore.bind(this);
this.decreaseScore = this.decreaseScore.bind(this);
}
updateScore(event){
this.setState({score: event.target.value});
}
increaseScore(){
this.setState({score: Number(this.state.score) + 1});
}
decreaseScore(){
this.setState({score: Number(this.state.score) - 1});
}
render() {
return (
);
}
}
function convertTournament(apiTournament) {
let groupStage = null;
let playoffStages = [];
for (let stage of apiTournament.stages) {
if(stage.groups.length > 0){
//group stage
groupStage = {groups: stage.groups.map(group => convertGroup(group))};
}else{
//playoff stage
playoffStages.push({
id: stage.id,
level: stage.level,
matches: stage.matches.map(match => convertMatch(match))
});
}
}
return {
id: apiTournament.id,
code: apiTournament.code,
description: apiTournament.description,
name: apiTournament.name,
public: apiTournament.public,
ownerUsername: apiTournament.owner_username,
groupStage: groupStage,
playoffStages: playoffStages
};
}
function convertGroup(apiGroup) {
return {
id: apiGroup.id,
number: apiGroup.number,
scores: apiGroup.group_scores,
matches: apiGroup.matches.map(match => convertMatch(match))
};
}
function convertMatch(apiMatch) {
return {
id: apiMatch.id,
state: apiMatch.state,
team1: apiMatch.match_scores[0].team.name,
team2: apiMatch.match_scores[1].team.name,
scoreTeam1: apiMatch.match_scores[0].points,
scoreTeam2: apiMatch.match_scores[1].points
};
}
class Main extends React.Component {
static async getInitialProps({query}) {
return {query};
}
constructor(props) {
super(props);
this.state = {
tournament : null
};
}
componentDidMount() {
verifyCredentials();
const code = this.props.query.code;
getRequest(getState(), '/tournaments/' + code)
.then(response => {
this.setState({ status : response.status, tournament : convertTournament(response.data)});
})
.catch((err) => {
if(err.response) {
this.setState({ status : err.response.status });
} else {
this.setState({ status : -1 });
}
});
}
render() {
const tournamentName = this.state.tournament === null ? 'Turnier' : this.state.tournament.name;
const { status, tournament } = this.state;
if (status == 200) {
return (
{tournamentName}: turnie.re
);
} else {
return ;
}
}
}
export default Main;