Application Android et IOS pour l'amicale des élèves https://play.google.com/store/apps/details?id=fr.amicaleinsat.application
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.

VoteSelect.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // @flow
  2. import * as React from 'react';
  3. import {Avatar, Button, Card, RadioButton} from 'react-native-paper';
  4. import {FlatList, StyleSheet, View} from 'react-native';
  5. import i18n from 'i18n-js';
  6. import ConnectionManager from '../../../managers/ConnectionManager';
  7. import LoadingConfirmDialog from '../../Dialogs/LoadingConfirmDialog';
  8. import ErrorDialog from '../../Dialogs/ErrorDialog';
  9. import type {VoteTeamType} from '../../../screens/Amicale/VoteScreen';
  10. import type {CardTitleIconPropsType} from '../../../constants/PaperStyles';
  11. type PropsType = {
  12. teams: Array<VoteTeamType>,
  13. onVoteSuccess: () => void,
  14. onVoteError: () => void,
  15. };
  16. type StateType = {
  17. selectedTeam: string,
  18. voteDialogVisible: boolean,
  19. errorDialogVisible: boolean,
  20. currentError: number,
  21. };
  22. const styles = StyleSheet.create({
  23. card: {
  24. margin: 10,
  25. },
  26. icon: {
  27. backgroundColor: 'transparent',
  28. },
  29. });
  30. export default class VoteSelect extends React.PureComponent<
  31. PropsType,
  32. StateType,
  33. > {
  34. constructor() {
  35. super();
  36. this.state = {
  37. selectedTeam: 'none',
  38. voteDialogVisible: false,
  39. errorDialogVisible: false,
  40. currentError: 0,
  41. };
  42. }
  43. onVoteSelectionChange = (teamName: string): void =>
  44. this.setState({selectedTeam: teamName});
  45. voteKeyExtractor = (item: VoteTeamType): string => item.id.toString();
  46. voteRenderItem = ({item}: {item: VoteTeamType}): React.Node => (
  47. <RadioButton.Item label={item.name} value={item.id.toString()} />
  48. );
  49. showVoteDialog = (): void => this.setState({voteDialogVisible: true});
  50. onVoteDialogDismiss = (): void => this.setState({voteDialogVisible: false});
  51. onVoteDialogAccept = async (): Promise<void> => {
  52. return new Promise((resolve: () => void) => {
  53. const {state} = this;
  54. ConnectionManager.getInstance()
  55. .authenticatedRequest('elections/vote', {
  56. team: parseInt(state.selectedTeam, 10),
  57. })
  58. .then(() => {
  59. this.onVoteDialogDismiss();
  60. const {props} = this;
  61. props.onVoteSuccess();
  62. resolve();
  63. })
  64. .catch((error: number) => {
  65. this.onVoteDialogDismiss();
  66. this.showErrorDialog(error);
  67. resolve();
  68. });
  69. });
  70. };
  71. showErrorDialog = (error: number): void =>
  72. this.setState({
  73. errorDialogVisible: true,
  74. currentError: error,
  75. });
  76. onErrorDialogDismiss = () => {
  77. this.setState({errorDialogVisible: false});
  78. const {props} = this;
  79. props.onVoteError();
  80. };
  81. render(): React.Node {
  82. const {state, props} = this;
  83. return (
  84. <View>
  85. <Card style={styles.card}>
  86. <Card.Title
  87. title={i18n.t('screens.vote.select.title')}
  88. subtitle={i18n.t('screens.vote.select.subtitle')}
  89. left={(iconProps: CardTitleIconPropsType): React.Node => (
  90. <Avatar.Icon size={iconProps.size} icon="alert-decagram" />
  91. )}
  92. />
  93. <Card.Content>
  94. <RadioButton.Group
  95. onValueChange={this.onVoteSelectionChange}
  96. value={state.selectedTeam}>
  97. {/* $FlowFixMe */}
  98. <FlatList
  99. data={props.teams}
  100. keyExtractor={this.voteKeyExtractor}
  101. extraData={state.selectedTeam}
  102. renderItem={this.voteRenderItem}
  103. />
  104. </RadioButton.Group>
  105. </Card.Content>
  106. <Card.Actions>
  107. <Button
  108. icon="send"
  109. mode="contained"
  110. onPress={this.showVoteDialog}
  111. style={{marginLeft: 'auto'}}
  112. disabled={state.selectedTeam === 'none'}>
  113. {i18n.t('screens.vote.select.sendButton')}
  114. </Button>
  115. </Card.Actions>
  116. </Card>
  117. <LoadingConfirmDialog
  118. visible={state.voteDialogVisible}
  119. onDismiss={this.onVoteDialogDismiss}
  120. onAccept={this.onVoteDialogAccept}
  121. title={i18n.t('screens.vote.select.dialogTitle')}
  122. titleLoading={i18n.t('screens.vote.select.dialogTitleLoading')}
  123. message={i18n.t('screens.vote.select.dialogMessage')}
  124. />
  125. <ErrorDialog
  126. visible={state.errorDialogVisible}
  127. onDismiss={this.onErrorDialogDismiss}
  128. errorCode={state.currentError}
  129. />
  130. </View>
  131. );
  132. }
  133. }