From d032b80158ae685c8188b4f0cffc542e1bb840df Mon Sep 17 00:00:00 2001 From: Arnaud Vergnet Date: Thu, 9 Apr 2020 17:39:09 +0200 Subject: [PATCH] Fixed vote api errors and handle draw --- src/components/Amicale/Vote/VoteResults.js | 17 ++++++++++++----- src/components/Amicale/Vote/VoteSelect.js | 2 +- src/components/Dialog/ErrorDialog.js | 2 +- src/screens/Amicale/VoteScreen.js | 21 +++++++++++++++------ 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/components/Amicale/Vote/VoteResults.js b/src/components/Amicale/Vote/VoteResults.js index 5e47f3d..6729906 100644 --- a/src/components/Amicale/Vote/VoteResults.js +++ b/src/components/Amicale/Vote/VoteResults.js @@ -13,7 +13,7 @@ type Props = { class VoteResults extends React.Component { totalVotes: number; - winnerId: number; + winnerIds: Array; colors: Object; constructor(props) { @@ -21,7 +21,7 @@ class VoteResults extends React.Component { this.colors = props.theme.colors; props.teams.sort(this.sortByVotes); this.getTotalVotes(props.teams); - this.getWinnerId(props.teams); + this.getWinnerIds(props.teams); } shouldComponentUpdate() { @@ -37,14 +37,21 @@ class VoteResults extends React.Component { } } - getWinnerId(teams: Array) { - this.winnerId = teams[0].id; + getWinnerIds(teams: Array){ + let max = teams[0].votes; + this.winnerIds= []; + for (let i = 0; i < teams.length; i++) { + if (teams[i].votes === max) + this.winnerIds.push(teams[i].id); + else + break; + } } voteKeyExtractor = (item: Object) => item.id.toString(); resultRenderItem = ({item}: Object) => { - const isWinner = this.winnerId === item.id; + const isWinner = this.winnerIds.indexOf(item.id) !== -1; return ( { return new Promise((resolve, reject) => { ConnectionManager.getInstance().authenticatedRequest( "elections/vote", - {"vote": parseInt(this.state.selectedTeam)}) + {"team": parseInt(this.state.selectedTeam)}) .then(() => { this.onVoteDialogDismiss(); this.props.onVoteSuccess(); diff --git a/src/components/Dialog/ErrorDialog.js b/src/components/Dialog/ErrorDialog.js index b9b2cd9..3fc708b 100644 --- a/src/components/Dialog/ErrorDialog.js +++ b/src/components/Dialog/ErrorDialog.js @@ -1,6 +1,6 @@ import * as React from 'react'; import i18n from "i18n-js"; -import {ERROR_TYPE} from "../../managers/ConnectionManager"; +import {ERROR_TYPE} from "../../utils/WebData"; import AlertDialog from "./AlertDialog"; type Props = { diff --git a/src/screens/Amicale/VoteScreen.js b/src/screens/Amicale/VoteScreen.js index 344033f..7d3a08f 100644 --- a/src/screens/Amicale/VoteScreen.js +++ b/src/screens/Amicale/VoteScreen.js @@ -9,12 +9,13 @@ import VoteTease from "../../components/Amicale/Vote/VoteTease"; import VoteSelect from "../../components/Amicale/Vote/VoteSelect"; import VoteResults from "../../components/Amicale/Vote/VoteResults"; import VoteWait from "../../components/Amicale/Vote/VoteWait"; +import {Button} from "react-native-paper"; const FAKE_DATE = { - "date_begin": "2020-04-07 21:50", - "date_end": "2020-04-06 23:50", - "date_result_begin": "2020-04-06 21:50", - "date_result_end": "2020-04-07 22:50", + "date_begin": "2020-04-09 15:50", + "date_end": "2020-04-09 15:50", + "date_result_begin": "2020-04-09 15:50", + "date_result_end": "2020-04-09 22:50", }; const FAKE_DATE2 = { @@ -43,13 +44,18 @@ const FAKE_TEAMS2 = { { id: 1, name: "TEST TEAM1", - votes: 1, + votes: 9, }, { id: 2, name: "TEST TEAM2", votes: 9, }, + { + id: 3, + name: "TEST TEAM3", + votes: 5, + }, ], }; @@ -158,6 +164,9 @@ export default class VoteScreen extends React.Component { extraData={this.state.hasVoted.toString()} renderItem={this.mainRenderItem} /> + ); }; @@ -223,7 +232,7 @@ export default class VoteScreen extends React.Component { mandatory: false, }, { - link: 'elections/datesString', + link: 'elections/dates', params: {}, mandatory: false, },