Fixed vote api errors and handle draw

This commit is contained in:
Arnaud Vergnet 2020-04-09 17:39:09 +02:00
parent 0ba48adb6e
commit d032b80158
4 changed files with 29 additions and 13 deletions

View file

@ -13,7 +13,7 @@ type Props = {
class VoteResults extends React.Component<Props> { class VoteResults extends React.Component<Props> {
totalVotes: number; totalVotes: number;
winnerId: number; winnerIds: Array<number>;
colors: Object; colors: Object;
constructor(props) { constructor(props) {
@ -21,7 +21,7 @@ class VoteResults extends React.Component<Props> {
this.colors = props.theme.colors; this.colors = props.theme.colors;
props.teams.sort(this.sortByVotes); props.teams.sort(this.sortByVotes);
this.getTotalVotes(props.teams); this.getTotalVotes(props.teams);
this.getWinnerId(props.teams); this.getWinnerIds(props.teams);
} }
shouldComponentUpdate() { shouldComponentUpdate() {
@ -37,14 +37,21 @@ class VoteResults extends React.Component<Props> {
} }
} }
getWinnerId(teams: Array<Object>) { getWinnerIds(teams: Array<Object>){
this.winnerId = teams[0].id; 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(); voteKeyExtractor = (item: Object) => item.id.toString();
resultRenderItem = ({item}: Object) => { resultRenderItem = ({item}: Object) => {
const isWinner = this.winnerId === item.id; const isWinner = this.winnerIds.indexOf(item.id) !== -1;
return ( return (
<Card style={{ <Card style={{
marginTop: 10, marginTop: 10,

View file

@ -45,7 +45,7 @@ export default class VoteSelect extends React.PureComponent<Props, State> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
ConnectionManager.getInstance().authenticatedRequest( ConnectionManager.getInstance().authenticatedRequest(
"elections/vote", "elections/vote",
{"vote": parseInt(this.state.selectedTeam)}) {"team": parseInt(this.state.selectedTeam)})
.then(() => { .then(() => {
this.onVoteDialogDismiss(); this.onVoteDialogDismiss();
this.props.onVoteSuccess(); this.props.onVoteSuccess();

View file

@ -1,6 +1,6 @@
import * as React from 'react'; import * as React from 'react';
import i18n from "i18n-js"; import i18n from "i18n-js";
import {ERROR_TYPE} from "../../managers/ConnectionManager"; import {ERROR_TYPE} from "../../utils/WebData";
import AlertDialog from "./AlertDialog"; import AlertDialog from "./AlertDialog";
type Props = { type Props = {

View file

@ -9,12 +9,13 @@ import VoteTease from "../../components/Amicale/Vote/VoteTease";
import VoteSelect from "../../components/Amicale/Vote/VoteSelect"; import VoteSelect from "../../components/Amicale/Vote/VoteSelect";
import VoteResults from "../../components/Amicale/Vote/VoteResults"; import VoteResults from "../../components/Amicale/Vote/VoteResults";
import VoteWait from "../../components/Amicale/Vote/VoteWait"; import VoteWait from "../../components/Amicale/Vote/VoteWait";
import {Button} from "react-native-paper";
const FAKE_DATE = { const FAKE_DATE = {
"date_begin": "2020-04-07 21:50", "date_begin": "2020-04-09 15:50",
"date_end": "2020-04-06 23:50", "date_end": "2020-04-09 15:50",
"date_result_begin": "2020-04-06 21:50", "date_result_begin": "2020-04-09 15:50",
"date_result_end": "2020-04-07 22:50", "date_result_end": "2020-04-09 22:50",
}; };
const FAKE_DATE2 = { const FAKE_DATE2 = {
@ -43,13 +44,18 @@ const FAKE_TEAMS2 = {
{ {
id: 1, id: 1,
name: "TEST TEAM1", name: "TEST TEAM1",
votes: 1, votes: 9,
}, },
{ {
id: 2, id: 2,
name: "TEST TEAM2", name: "TEST TEAM2",
votes: 9, votes: 9,
}, },
{
id: 3,
name: "TEST TEAM3",
votes: 5,
},
], ],
}; };
@ -158,6 +164,9 @@ export default class VoteScreen extends React.Component<Props, State> {
extraData={this.state.hasVoted.toString()} extraData={this.state.hasVoted.toString()}
renderItem={this.mainRenderItem} renderItem={this.mainRenderItem}
/> />
<Button onPress={this.reloadData}>
REFRESH
</Button>
</View> </View>
); );
}; };
@ -223,7 +232,7 @@ export default class VoteScreen extends React.Component<Props, State> {
mandatory: false, mandatory: false,
}, },
{ {
link: 'elections/datesString', link: 'elections/dates',
params: {}, params: {},
mandatory: false, mandatory: false,
}, },