// @flow 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'; import type {CustomThemeType} from '../../../managers/ThemeManager'; import type { CardTitleIconPropsType, ListIconPropsType, } from '../../../constants/PaperStyles'; type PropsType = { teams: Array, dateEnd: string, theme: CustomThemeType, }; const styles = StyleSheet.create({ card: { margin: 10, }, icon: { backgroundColor: 'transparent', }, }); class VoteResults extends React.Component { totalVotes: number; winnerIds: Array; constructor(props: PropsType) { super(); props.teams.sort(this.sortByVotes); this.getTotalVotes(props.teams); this.getWinnerIds(props.teams); } shouldComponentUpdate(): boolean { return false; } getTotalVotes(teams: Array) { this.totalVotes = 0; for (let i = 0; i < teams.length; i += 1) { this.totalVotes += teams[i].votes; } } getWinnerIds(teams: Array) { const max = teams[0].votes; this.winnerIds = []; for (let i = 0; i < teams.length; i += 1) { if (teams[i].votes === max) this.winnerIds.push(teams[i].id); else break; } } sortByVotes = (a: VoteTeamType, b: VoteTeamType): number => b.votes - a.votes; voteKeyExtractor = (item: VoteTeamType): string => item.id.toString(); resultRenderItem = ({item}: {item: VoteTeamType}): React.Node => { 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(): React.Node { const {props} = this; return ( ( )} /> {`${i18n.t('screens.vote.results.totalVotes')} ${ this.totalVotes }`} {/* $FlowFixMe */} ); } } export default withTheme(VoteResults);