]> git.localhorst.tv Git - alttp.git/blobdiff - resources/js/components/results/List.js
sort open-async results, if allowed
[alttp.git] / resources / js / components / results / List.js
index 11eb9a8f4f5eb1f07b7aaa7a13efb431de0923ec..fb26fa56f264d94c2c393a6dc70af0e23dea91f3 100644 (file)
@@ -2,25 +2,57 @@ import PropTypes from 'prop-types';
 import React from 'react';
 
 import Item from './Item';
+import { sortByFinished, sortByResult } from '../../helpers/Participant';
+import { maySeeResults } from '../../helpers/permissions';
+import { getRunners } from '../../helpers/Tournament';
+import { compareResult } from '../../helpers/Result';
+import { withUser } from '../../helpers/UserContext';
 
-const List = ({ round, tournament }) => <div className="results d-flex">
-       {tournament.participants.map(participant =>
-               <Item
-                       key={participant.id}
-                       participant={participant}
-                       round={round}
-                       tournament={tournament}
-               />
-       )}
-</div>;
+const List = ({ round, tournament, user }) => {
+       if (tournament.type === 'open-async') {
+               const results = maySeeResults(user, tournament, round)
+                       ? (round.results || []).sort(compareResult)
+                       : round.results || [];
+               return <div className="results d-flex flex-wrap">
+                       {results.map(result =>
+                               <Item
+                                       key={result.id}
+                                       round={round}
+                                       tournament={tournament}
+                                       user={result.user}
+                               />
+                       )}
+               </div>;
+       }
+       const runners = maySeeResults(user, tournament, round)
+               ? sortByResult(getRunners(tournament), round)
+               : sortByFinished(getRunners(tournament), round);
+       return <div className="results d-flex flex-wrap">
+               {runners.map(participant =>
+                       <Item
+                               key={participant.id}
+                               round={round}
+                               tournament={tournament}
+                               user={participant.user}
+                       />
+               )}
+       </div>;
+};
 
 List.propTypes = {
        round: PropTypes.shape({
+               results: PropTypes.arrayOf(PropTypes.shape({
+               })),
        }),
        tournament: PropTypes.shape({
                participants: PropTypes.arrayOf(PropTypes.shape({
                })),
+               type: PropTypes.string,
+               users: PropTypes.arrayOf(PropTypes.shape({
+               })),
+       }),
+       user: PropTypes.shape({
        }),
 };
 
-export default List;
+export default withUser(List);