Application Android et IOS pour l'amicale des élèves
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VoteResults.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // @flow
  2. import * as React from 'react';
  3. import {
  4. Avatar,
  5. Card,
  6. List,
  7. ProgressBar,
  8. Subheading,
  9. withTheme,
  10. } from 'react-native-paper';
  11. import {FlatList, StyleSheet} from 'react-native';
  12. import i18n from 'i18n-js';
  13. import type {VoteTeamType} from '../../../screens/Amicale/VoteScreen';
  14. import type {CustomThemeType} from '../../../managers/ThemeManager';
  15. type PropsType = {
  16. teams: Array<VoteTeamType>,
  17. dateEnd: string,
  18. theme: CustomThemeType,
  19. };
  20. const styles = StyleSheet.create({
  21. card: {
  22. margin: 10,
  23. },
  24. icon: {
  25. backgroundColor: 'transparent',
  26. },
  27. });
  28. class VoteResults extends React.Component<PropsType> {
  29. totalVotes: number;
  30. winnerIds: Array<number>;
  31. constructor(props: PropsType) {
  32. super();
  33. props.teams.sort(this.sortByVotes);
  34. this.getTotalVotes(props.teams);
  35. this.getWinnerIds(props.teams);
  36. }
  37. shouldComponentUpdate(): boolean {
  38. return false;
  39. }
  40. getTotalVotes(teams: Array<VoteTeamType>) {
  41. this.totalVotes = 0;
  42. for (let i = 0; i < teams.length; i += 1) {
  43. this.totalVotes += teams[i].votes;
  44. }
  45. }
  46. getWinnerIds(teams: Array<VoteTeamType>) {
  47. const max = teams[0].votes;
  48. this.winnerIds = [];
  49. for (let i = 0; i < teams.length; i += 1) {
  50. if (teams[i].votes === max) this.winnerIds.push(teams[i].id);
  51. else break;
  52. }
  53. }
  54. sortByVotes = (a: VoteTeamType, b: VoteTeamType): number => b.votes - a.votes;
  55. voteKeyExtractor = (item: VoteTeamType): string => item.id.toString();
  56. resultRenderItem = ({item}: {item: VoteTeamType}): React.Node => {
  57. const isWinner = this.winnerIds.indexOf(item.id) !== -1;
  58. const isDraw = this.winnerIds.length > 1;
  59. const {props} = this;
  60. return (
  61. <Card
  62. style={{
  63. marginTop: 10,
  64. elevation: isWinner ? 5 : 3,
  65. }}>
  66. <List.Item
  67. title={item.name}
  68. description={`${item.votes} ${i18n.t('screens.vote.results.votes')}`}
  69. left={({size}: {size: number}): React.Node =>
  70. isWinner ? (
  71. <List.Icon
  72. size={size}
  73. icon={isDraw ? 'trophy-outline' : 'trophy'}
  74. color={props.theme.colors.primary}
  75. />
  76. ) : null
  77. }
  78. titleStyle={{
  79. color: isWinner
  80. ? props.theme.colors.primary
  81. : props.theme.colors.text,
  82. }}
  83. style={{padding: 0}}
  84. />
  85. <ProgressBar
  86. progress={item.votes / this.totalVotes}
  87. color={props.theme.colors.primary}
  88. />
  89. </Card>
  90. );
  91. };
  92. render(): React.Node {
  93. const {props} = this;
  94. return (
  95. <Card style={styles.card}>
  96. <Card.Title
  97. title={i18n.t('screens.vote.results.title')}
  98. subtitle={`${i18n.t('screens.vote.results.subtitle')} ${
  99. props.dateEnd
  100. }`}
  101. left={({size}: {size: number}): React.Node => (
  102. <Avatar.Icon size={size} icon="podium-gold" />
  103. )}
  104. />
  105. <Card.Content>
  106. <Subheading>{`${i18n.t('screens.vote.results.totalVotes')} ${
  107. this.totalVotes
  108. }`}</Subheading>
  109. {/* $FlowFixMe */}
  110. <FlatList
  111. data={props.teams}
  112. keyExtractor={this.voteKeyExtractor}
  113. renderItem={this.resultRenderItem}
  114. />
  115. </Card.Content>
  116. </Card>
  117. );
  118. }
  119. }
  120. export default withTheme(VoteResults);