]> git.localhorst.tv Git - alttp.git/blob - resources/js/components/users/Participation.js
b12adff897fdd964b6fd4af36bd487709031845e
[alttp.git] / resources / js / components / users / Participation.js
1 import PropTypes from 'prop-types';
2 import React from 'react';
3 import { Alert, Button, Table } from 'react-bootstrap';
4 import { withTranslation } from 'react-i18next';
5 import { useNavigate } from 'react-router-dom';
6
7 import Icon from '../common/Icon';
8 import { isRunner } from '../../helpers/Participant';
9 import i18n from '../../i18n';
10
11 const getIcon = participant => {
12         if (!isRunner(participant)) {
13                 return '—';
14         }
15         if (participant.placement === 1) {
16                 return <Icon.FIRST_PLACE className="text-gold" size="lg" />;
17         }
18         if (participant.placement === 2) {
19                 return <Icon.SECOND_PLACE className="text-silver" size="lg" />;
20         }
21         if (participant.placement === 3) {
22                 return <Icon.THIRD_PLACE className="text-bronze" size="lg" />;
23         }
24         return participant.placement;
25 };
26
27 const Participation = ({ user }) => {
28         const navigate = useNavigate();
29
30         if (!user || !user.participation || !user.participation.length) {
31                 return <Alert variant="info">
32                         {i18n.t('users.participationEmpty')}
33                 </Alert>;
34         }
35         return <Table className="participation align-middle">
36                 <thead>
37                         <tr>
38                                 <th>{i18n.t('participants.tournament')}</th>
39                                 <th>{i18n.t('participants.placement')}</th>
40                                 <th>{i18n.t('participants.roles')}</th>
41                         </tr>
42                 </thead>
43                 <tbody>
44                 {user.participation.map(p => <tr key={p.id}>
45                         <td>
46                                 <Button
47                                         onClick={() => navigate(`/tournaments/${p.tournament_id}`)}
48                                         variant="link"
49                                 >
50                                         {p.tournament.title}
51                                 </Button>
52                         </td>
53                         <td>
54                                 {getIcon(p)}
55                         {!p.tournament.locked && isRunner(p) ?
56                                 <span title={i18n.t('participants.placementSubjectToChange')}> *</span>
57                         : null}
58                         </td>
59                         <td>
60                                 {p.roles ? p.roles.map((role, index) =>
61                                         <span key={role}>
62                                                 {index === 0 ? '' : ', '}
63                                                 {i18n.t(`participants.roleNames.${role}`)}
64                                         </span>
65                                 ) : null}
66                         </td>
67                 </tr>)}
68                 </tbody>
69         </Table>;
70 };
71
72 Participation.propTypes = {
73         user: PropTypes.shape({
74                 participation: PropTypes.arrayOf(PropTypes.shape({
75                         id: PropTypes.number,
76                 })),
77         }),
78 };
79
80 export default withTranslation()(Participation);