From b02be2e3e307796dac05b4169291e7a1142cc7be Mon Sep 17 00:00:00 2001 From: Malaber Date: Fri, 15 Sep 2023 14:25:55 +0200 Subject: [PATCH 1/2] Fix group stage match sorting --- js/components/GroupStage.js | 14 +++++++++++++- js/components/Match.js | 1 + js/redux/tournamentApi.js | 22 ++++++++++++++++------ 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/js/components/GroupStage.js b/js/components/GroupStage.js index 2c9791e..a1469e0 100644 --- a/js/components/GroupStage.js +++ b/js/components/GroupStage.js @@ -35,6 +35,18 @@ function ShowMatchesToggleButton(props) { ); } +function sortMatchesByPositionAscending() { + return (a, b) => { + if (a.position < b.position) { + return -1; + } else if (a.position > b.position) { + return 1; + } else { + return 0; + } + }; +} + export class Group extends Component { constructor(props) { super(props); @@ -62,7 +74,7 @@ export class Group extends Component {

Gruppe {this.state.number}

- {this.state.matches.map((match => ( + {this.state.matches.sort(sortMatchesByPositionAscending()).map((match => ( )))} diff --git a/js/components/Match.js b/js/components/Match.js index b80b3f3..d9f3211 100644 --- a/js/components/Match.js +++ b/js/components/Match.js @@ -7,6 +7,7 @@ import {MatchTable} from './MatchTable'; import styles from './Match.module.css'; + export class Match extends React.Component { constructor(props) { super(props); diff --git a/js/redux/tournamentApi.js b/js/redux/tournamentApi.js index e1b9602..e1b37b4 100644 --- a/js/redux/tournamentApi.js +++ b/js/redux/tournamentApi.js @@ -71,16 +71,26 @@ function convertTournament(apiTournament) { } function convertPlayoffStage(apiStage) { - return { - id: apiStage.id, level: apiStage.level, matches: apiStage.matches.sort((a, b) => { + function sortMatchesByPositionAscending() { + return (a, b) => { if (a.position < b.position) { - return -1 + return -1; } else if (a.position > b.position) { - return 1 + return 1; } else { - return 0 + return 0; } - }).map(match => convertMatch(match, false)) + }; + } + + return { + id: apiStage.id, + level: apiStage.level, + matches: apiStage.matches.sort( + sortMatchesByPositionAscending() + ).map( + match => convertMatch(match, false) + ) }; } From 54e17ea4946c47730f0fefe334871d689c41f620 Mon Sep 17 00:00:00 2001 From: Malaber Date: Fri, 15 Sep 2023 14:28:56 +0200 Subject: [PATCH 2/2] DRY sorting --- js/components/GroupStage.js | 12 +----------- js/redux/tournamentApi.js | 13 +------------ js/utils/sorting.js | 11 +++++++++++ 3 files changed, 13 insertions(+), 23 deletions(-) create mode 100644 js/utils/sorting.js diff --git a/js/components/GroupStage.js b/js/components/GroupStage.js index a1469e0..7b0ac40 100644 --- a/js/components/GroupStage.js +++ b/js/components/GroupStage.js @@ -3,6 +3,7 @@ import {Match} from './Match'; import React, {Component} from 'react'; import {getGroup} from '../redux/tournamentApi'; import {notify} from 'react-notify-toast'; +import {sortMatchesByPositionAscending} from '../utils/sorting'; export default class GroupStage extends Component { constructor(props) { @@ -35,17 +36,6 @@ function ShowMatchesToggleButton(props) { ); } -function sortMatchesByPositionAscending() { - return (a, b) => { - if (a.position < b.position) { - return -1; - } else if (a.position > b.position) { - return 1; - } else { - return 0; - } - }; -} export class Group extends Component { constructor(props) { diff --git a/js/redux/tournamentApi.js b/js/redux/tournamentApi.js index e1b37b4..2a35483 100644 --- a/js/redux/tournamentApi.js +++ b/js/redux/tournamentApi.js @@ -1,5 +1,6 @@ import {getRequest} from './backendApi'; import {getState} from '../api'; +import {sortMatchesByPositionAscending} from '../utils/sorting'; export function getTournament(code, successCallback, errorCallback) { getRequest(getState(), '/tournaments/' + code) @@ -71,18 +72,6 @@ function convertTournament(apiTournament) { } function convertPlayoffStage(apiStage) { - function sortMatchesByPositionAscending() { - return (a, b) => { - if (a.position < b.position) { - return -1; - } else if (a.position > b.position) { - return 1; - } else { - return 0; - } - }; - } - return { id: apiStage.id, level: apiStage.level, diff --git a/js/utils/sorting.js b/js/utils/sorting.js new file mode 100644 index 0000000..40179c2 --- /dev/null +++ b/js/utils/sorting.js @@ -0,0 +1,11 @@ +export function sortMatchesByPositionAscending() { + return (a, b) => { + if (a.position < b.position) { + return -1; + } else if (a.position > b.position) { + return 1; + } else { + return 0; + } + }; +}