]> git.localhorst.tv Git - alttp.git/blob - resources/js/components/results/Item.js
result comments
[alttp.git] / resources / js / components / results / Item.js
1 import PropTypes from 'prop-types';
2 import React from 'react';
3 import { withTranslation } from 'react-i18next';
4
5 import Icon from '../common/Icon';
6 import Box from '../users/Box';
7 import { formatTime } from '../../helpers/Result';
8 import { findResult } from '../../helpers/Participant';
9 import { maySeeResults } from '../../helpers/permissions';
10 import { withUser } from '../../helpers/UserContext';
11
12 const getIcon = (result, maySee) => {
13         if (!result || !result.has_finished) {
14                 return <Icon.PENDING className="text-muted" size="lg" />;
15         }
16         if (result.forfeit && maySee) {
17                 return <Icon.FORFEIT className="text-danger" size="lg" />;
18         }
19         if (result.placement === 1) {
20                 return <Icon.FIRST_PLACE className="text-gold" size="lg" />;
21         }
22         if (result.placement === 2) {
23                 return <Icon.SECOND_PLACE className="text-silver" size="lg" />;
24         }
25         if (result.placement === 3) {
26                 return <Icon.THIRD_PLACE className="text-bronze" size="lg" />;
27         }
28         return <Icon.FINISHED className="text-success" size="lg" />;
29 };
30
31 const getTime = (result, maySee) => {
32         if (!result || !maySee) {
33                 return null;
34         }
35         if (result.time) {
36                 return formatTime(result);
37         }
38         if (result.forfeit) {
39                 return 'DNF';
40         }
41         return '?';
42 };
43
44 const getClassName = result => {
45         const classNames = ['status'];
46         if (result && result.has_finished) {
47                 classNames.push('finished');
48                 if (result.comment) {
49                         classNames.push('has-comment');
50                 }
51         } else {
52                 classNames.push('pending');
53         }
54         return classNames.join(' ');
55 };
56
57 const Item = ({
58         participant,
59         round,
60         tournament,
61         user,
62 }) => {
63         const result = findResult(participant, round);
64         const maySee = maySeeResults(user, tournament, round);
65         return <div className="result">
66                 <Box user={participant.user} />
67                 <div
68                         className={getClassName(result)}
69                         title={maySee && result && result.comment ? result.comment : null}
70                 >
71                         <span className="time">
72                                 {getTime(result, maySee)}
73                         </span>
74                         {getIcon(result, maySee)}
75                 </div>
76         </div>;
77 };
78
79 Item.propTypes = {
80         participant: PropTypes.shape({
81                 user: PropTypes.shape({
82                 }),
83         }),
84         round: PropTypes.shape({
85         }),
86         tournament: PropTypes.shape({
87         }),
88         user: PropTypes.shape({
89         }),
90 };
91
92 export default withTranslation()(withUser(Item));