]> git.localhorst.tv Git - alttp.git/blob - resources/js/components/results/Item.js
display forfeits with no time as DNF
[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, index) => {
13         if (!result || !result.has_finished) {
14                 return <Icon.PENDING className="text-muted" size="lg" />;
15         }
16         if (result.forfeit) {
17                 return <Icon.FORFEIT className="text-danger" size="lg" />;
18         }
19         if (index === 0) {
20                 return <Icon.FIRST_PLACE className="text-gold" size="lg" />;
21         }
22         if (index === 1) {
23                 return <Icon.SECOND_PLACE className="text-silver" size="lg" />;
24         }
25         if (index === 2) {
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 = (user, tournament, round, result) => {
32         if (!result || !maySeeResults(user, tournament, round)) {
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 Item = ({
45         index,
46         participant,
47         round,
48         tournament,
49         user,
50 }) => {
51         const result = findResult(participant, round);
52         return <div className="result">
53                 <Box user={participant.user} />
54                 <div className="status">
55                         <span className="time">
56                                 {getTime(user, tournament, round, result)}
57                         </span>
58                         {getIcon(result, index)}
59                 </div>
60         </div>;
61 };
62
63 Item.propTypes = {
64         index: PropTypes.number,
65         participant: PropTypes.shape({
66                 user: PropTypes.shape({
67                 }),
68         }),
69         round: PropTypes.shape({
70         }),
71         tournament: PropTypes.shape({
72         }),
73         user: PropTypes.shape({
74         }),
75 };
76
77 export default withTranslation()(withUser(Item));