]> git.localhorst.tv Git - alttp.git/blobdiff - resources/js/components/rounds/Item.js
improved user context
[alttp.git] / resources / js / components / rounds / Item.js
index 10549dd151a5f52048e0fd56c19c0288e55ac92b..949eeb2f2ec910e99b94ba9bf0a01c67a5549ba3 100644 (file)
 import PropTypes from 'prop-types';
 import React from 'react';
-import { withTranslation } from 'react-i18next';
+import { useTranslation } from 'react-i18next';
 
+import EditButton from './EditButton';
+import LockButton from './LockButton';
 import SeedButton from './SeedButton';
 import SeedCode from './SeedCode';
+import SeedRolledBy from './SeedRolledBy';
 import List from '../results/List';
 import ReportButton from '../results/ReportButton';
-import { isParticipant } from '../../helpers/permissions';
-import { findParticipant } from '../../helpers/Tournament';
-import { withUser } from '../../helpers/UserContext';
-import i18n from '../../i18n';
+import { mayEditRound, mayReportResult, isRunner } from '../../helpers/permissions';
+import { isComplete } from '../../helpers/Round';
+import { hasFinishedRound } from '../../helpers/User';
+import { useUser } from '../../hooks/user';
+
+const getClassName = (round, tournament, user) => {
+       const classNames = ['round'];
+       if (round.locked) {
+               classNames.push('is-locked');
+       } else {
+               classNames.push('is-unlocked');
+       }
+       if (isComplete(tournament, round)) {
+               classNames.push('is-complete');
+       } else {
+               classNames.push('is-incomplete');
+       }
+       if (hasFinishedRound(user, round)) {
+               classNames.push('has-finished');
+       } else if (isRunner(user, tournament)) {
+               classNames.push('has-not-finished');
+       }
+       return classNames.join(' ');
+};
 
 const Item = ({
        round,
        tournament,
-       user,
-}) =>
-<li className="round d-flex">
-       <div className="info">
-               <p className="date">
-                       {round.number ? `#${round.number} ` : '#?'}
-                       {i18n.t('rounds.date', { date: new Date(round.created_at) })}
-               </p>
-               <p className="seed">
-                       {round.code ?
-                               <>
-                                       <SeedCode code={round.code} />
-                                       {' '}
-                               </>
-                       : null}
-                       <SeedButton
-                               round={round}
-                               tournament={tournament}
-                       />
-               </p>
-               {isParticipant(user, tournament) ?
-                       <p className="report">
-                               <ReportButton
-                                       participant={findParticipant(tournament, user)}
-                                       round={round}
-                                       tournament={tournament}
-                               />
-                       </p>
+}) => {
+       const { t } = useTranslation();
+       const { user } = useUser();
+
+return <li className={getClassName(round, tournament, user)}>
+               {round.title ?
+                       <h3>{round.title}</h3>
                : null}
-       </div>
-       <List round={round} tournament={tournament} />
-</li>;
+               <div className="d-flex">
+                       <div className="info">
+                               <p className="date">
+                                       {round.number ? `#${round.number} ` : '#?'}
+                                       {t('rounds.date', { date: new Date(round.created_at) })}
+                               </p>
+                               <p className="seed">
+                                       {round.code && round.code.length ?
+                                               <>
+                                                       <SeedCode code={round.code} game={round.game || 'alttpr'} />
+                                                       <br />
+                                               </>
+                                       : null}
+                                       <SeedButton
+                                               round={round}
+                                               tournament={tournament}
+                                       />
+                                       {' '}
+                                       <SeedRolledBy round={round} />
+                               </p>
+                               {mayReportResult(user, tournament) ?
+                                       <p className="report">
+                                               <ReportButton
+                                                       round={round}
+                                                       tournament={tournament}
+                                                       user={user}
+                                               />
+                                       </p>
+                               : null}
+                               {tournament.type === 'open-async' && round.results && round.results.length ?
+                                       <p>{t('rounds.numberOfResults', { count: round.results.length })}</p>
+                               : null}
+                               <div className="button-bar">
+                                       <LockButton round={round} tournament={tournament} />
+                                       {mayEditRound(user, tournament, round) ?
+                                               <EditButton round={round} tournament={tournament} />
+                                       : null}
+                               </div>
+                       </div>
+                       <List round={round} tournament={tournament} />
+               </div>
+       </li>;
+};
 
 Item.propTypes = {
        round: PropTypes.shape({
                code: PropTypes.arrayOf(PropTypes.string),
                created_at: PropTypes.string,
+               game: PropTypes.string,
+               locked: PropTypes.bool,
                number: PropTypes.number,
+               results: PropTypes.arrayOf(PropTypes.shape({
+               })),
                seed: PropTypes.string,
+               title: PropTypes.string,
        }),
        tournament: PropTypes.shape({
                participants: PropTypes.arrayOf(PropTypes.shape({
                })),
-       }),
-       user: PropTypes.shape({
+               type: PropTypes.string,
        }),
 };
 
-export default withTranslation()(withUser(Item));
+export default Item;