/* * Copyright (c) 2019 - 2020 Arnaud Vergnet. * * This file is part of Campus INSAT. * * Campus INSAT is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Campus INSAT is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Campus INSAT. If not, see . */ import * as React from 'react'; import { Avatar, Card, List, ProgressBar, Subheading, withTheme, } from 'react-native-paper'; import {FlatList, StyleSheet} from 'react-native'; import i18n from 'i18n-js'; import type {VoteTeamType} from '../../../screens/Amicale/VoteScreen'; type PropsType = { teams: Array; dateEnd: string; theme: ReactNativePaper.Theme; }; const styles = StyleSheet.create({ card: { margin: 10, }, icon: { backgroundColor: 'transparent', }, }); class VoteResults extends React.Component { totalVotes: number; winnerIds: Array; constructor(props: PropsType) { super(props); props.teams.sort(this.sortByVotes); this.totalVotes = this.getTotalVotes(props.teams); this.winnerIds = this.getWinnerIds(props.teams); } shouldComponentUpdate(): boolean { return false; } getTotalVotes(teams: Array) { let totalVotes = 0; for (let i = 0; i < teams.length; i += 1) { totalVotes += teams[i].votes; } return totalVotes; } getWinnerIds(teams: Array) { const max = teams[0].votes; let winnerIds = []; for (let i = 0; i < teams.length; i += 1) { if (teams[i].votes === max) { winnerIds.push(teams[i].id); } else { break; } } return winnerIds; } sortByVotes = (a: VoteTeamType, b: VoteTeamType): number => b.votes - a.votes; voteKeyExtractor = (item: VoteTeamType): string => item.id.toString(); resultRenderItem = ({item}: {item: VoteTeamType}) => { const isWinner = this.winnerIds.indexOf(item.id) !== -1; const isDraw = this.winnerIds.length > 1; const {props} = this; return ( isWinner ? ( ) : null } titleStyle={{ color: isWinner ? props.theme.colors.primary : props.theme.colors.text, }} style={{padding: 0}} /> ); }; render() { const {props} = this; return ( ( )} /> {`${i18n.t('screens.vote.results.totalVotes')} ${this.totalVotes}`} ); } } export default withTheme(VoteResults);