]> git.localhorst.tv Git - alttp.git/blob - resources/js/components/results/List.js
sort open-async results, if allowed
[alttp.git] / resources / js / components / results / List.js
1 import PropTypes from 'prop-types';
2 import React from 'react';
3
4 import Item from './Item';
5 import { sortByFinished, sortByResult } from '../../helpers/Participant';
6 import { maySeeResults } from '../../helpers/permissions';
7 import { getRunners } from '../../helpers/Tournament';
8 import { compareResult } from '../../helpers/Result';
9 import { withUser } from '../../helpers/UserContext';
10
11 const List = ({ round, tournament, user }) => {
12         if (tournament.type === 'open-async') {
13                 const results = maySeeResults(user, tournament, round)
14                         ? (round.results || []).sort(compareResult)
15                         : round.results || [];
16                 return <div className="results d-flex flex-wrap">
17                         {results.map(result =>
18                                 <Item
19                                         key={result.id}
20                                         round={round}
21                                         tournament={tournament}
22                                         user={result.user}
23                                 />
24                         )}
25                 </div>;
26         }
27         const runners = maySeeResults(user, tournament, round)
28                 ? sortByResult(getRunners(tournament), round)
29                 : sortByFinished(getRunners(tournament), round);
30         return <div className="results d-flex flex-wrap">
31                 {runners.map(participant =>
32                         <Item
33                                 key={participant.id}
34                                 round={round}
35                                 tournament={tournament}
36                                 user={participant.user}
37                         />
38                 )}
39         </div>;
40 };
41
42 List.propTypes = {
43         round: PropTypes.shape({
44                 results: PropTypes.arrayOf(PropTypes.shape({
45                 })),
46         }),
47         tournament: PropTypes.shape({
48                 participants: PropTypes.arrayOf(PropTypes.shape({
49                 })),
50                 type: PropTypes.string,
51                 users: PropTypes.arrayOf(PropTypes.shape({
52                 })),
53         }),
54         user: PropTypes.shape({
55         }),
56 };
57
58 export default withUser(List);