]> git.localhorst.tv Git - alttp.git/blob - resources/js/components/tournament/Detail.js
improved user context
[alttp.git] / resources / js / components / tournament / Detail.js
1 import PropTypes from 'prop-types';
2 import React from 'react';
3 import { Button, Col, Container, Row } from 'react-bootstrap';
4 import { useTranslation } from 'react-i18next';
5
6 import ApplyButton from './ApplyButton';
7 import Scoreboard from './Scoreboard';
8 import ScoreChartButton from './ScoreChartButton';
9 import SettingsButton from './SettingsButton';
10 import ApplicationsButton from '../applications/Button';
11 import Protocol from '../protocol/Protocol';
12 import Rounds from '../rounds/List';
13 import Box from '../users/Box';
14 import {
15         isRunner,
16         mayAddRounds,
17         mayUpdateTournament,
18         mayViewProtocol,
19 } from '../../helpers/permissions';
20 import {
21         getTournamentAdmins,
22         getTournamentMonitors,
23         hasRunners,
24         hasScoreboard,
25         hasTournamentAdmins,
26         hasTournamentMonitors,
27 } from '../../helpers/Tournament';
28 import { useUser } from '../../hooks/user';
29
30 const getClassName = (tournament, user) => {
31         const classNames = ['tournament'];
32         if (tournament.locked) {
33                 classNames.push('is-locked');
34         } else {
35                 classNames.push('is-active');
36         }
37         if (isRunner(user, tournament)) {
38                 classNames.push('is-runner');
39         }
40         return classNames.join(' ');
41 };
42
43 const Detail = ({
44         addRound,
45         tournament,
46 }) => {
47         const { t } = useTranslation();
48         const { user } = useUser();
49
50         return <Container className={getClassName(tournament, user)} fluid>
51                 <Row>
52                         <Col lg={8} xl={9}>
53                                 <div className="d-flex align-items-center justify-content-between">
54                                         <h1>{tournament.title}</h1>
55                                         <div className="button-bar">
56                                                 <ApplicationsButton tournament={tournament} />
57                                                 <ApplyButton tournament={tournament} />
58                                                 {mayUpdateTournament(user, tournament) ?
59                                                         <SettingsButton tournament={tournament} />
60                                                 : null}
61                                                 {mayViewProtocol(user, tournament) ?
62                                                         <Protocol id={tournament.id} />
63                                                 : null}
64                                         </div>
65                                 </div>
66                         </Col>
67                 </Row>
68                 <Row>
69                         <Col lg={{ order: 2, span: 4 }} xl={{ order: 2, span: 3 }}>
70                                 <div className="tournament-sidebar">
71                                         {hasScoreboard(tournament) ? <>
72                                                 <div className="d-flex align-items-center justify-content-between">
73                                                         <h2>{t('tournaments.scoreboard')}</h2>
74                                                         {hasRunners(tournament) && tournament.rounds.length > 2 ?
75                                                                 <ScoreChartButton tournament={tournament} />
76                                                         : null}
77                                                 </div>
78                                                 {hasRunners(tournament) ?
79                                                         <Scoreboard tournament={tournament} />
80                                                 : null}
81                                         </> : null}
82                                         {hasTournamentAdmins(tournament) ?
83                                                 <>
84                                                         <div className="d-flex align-items-center justify-content-between">
85                                                                 <h2>{t('tournaments.admins')}</h2>
86                                                         </div>
87                                                         {getTournamentAdmins(tournament).map(p =>
88                                                                 <p key={p.id}><Box user={p.user} /></p>
89                                                         )}
90                                                 </>
91                                         : null}
92                                         {hasTournamentMonitors(tournament) ?
93                                                 <>
94                                                         <div className="d-flex align-items-center justify-content-between">
95                                                                 <h2>{t('tournaments.monitors')}</h2>
96                                                         </div>
97                                                         {getTournamentMonitors(tournament).map(p =>
98                                                                 <p key={p.id}><Box user={p.user} /></p>
99                                                         )}
100                                                 </>
101                                         : null}
102                                 </div>
103                         </Col>
104                         <Col lg={{ order: 1, span: 8 }} xl={{ order: 1, span: 9 }}>
105                                 <div className="d-flex align-items-center justify-content-between">
106                                         <h2>{t('rounds.heading')}</h2>
107                                         {addRound && mayAddRounds(user, tournament) ?
108                                                 <Button onClick={addRound}>
109                                                         {t('rounds.new')}
110                                                 </Button>
111                                         : null}
112                                 </div>
113                                 {tournament.rounds ?
114                                         <Rounds rounds={tournament.rounds} tournament={tournament} />
115                                 : null}
116                         </Col>
117                 </Row>
118         </Container>;
119 };
120
121 Detail.propTypes = {
122         addRound: PropTypes.func,
123         tournament: PropTypes.shape({
124                 id: PropTypes.number,
125                 participants: PropTypes.arrayOf(PropTypes.shape({
126                 })),
127                 rounds: PropTypes.arrayOf(PropTypes.shape({
128                 })),
129                 title: PropTypes.string,
130         }),
131 };
132
133 export default Detail;