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.tsx 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 2019 - 2020 Arnaud Vergnet.
  3. *
  4. * This file is part of Campus INSAT.
  5. *
  6. * Campus INSAT is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Campus INSAT is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Campus INSAT. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. import React, { useState } from 'react';
  20. import { Avatar, Button, Card, RadioButton } from 'react-native-paper';
  21. import { FlatList, StyleSheet, View } from 'react-native';
  22. import i18n from 'i18n-js';
  23. import LoadingConfirmDialog from '../../Dialogs/LoadingConfirmDialog';
  24. import ErrorDialog from '../../Dialogs/ErrorDialog';
  25. import type { VoteTeamType } from '../../../screens/Amicale/VoteScreen';
  26. import { ApiRejectType } from '../../../utils/WebData';
  27. import { REQUEST_STATUS } from '../../../utils/Requests';
  28. import { useAuthenticatedRequest } from '../../../context/loginContext';
  29. type Props = {
  30. teams: Array<VoteTeamType>;
  31. onVoteSuccess: () => void;
  32. onVoteError: () => void;
  33. };
  34. const styles = StyleSheet.create({
  35. card: {
  36. margin: 10,
  37. },
  38. button: {
  39. marginLeft: 'auto',
  40. },
  41. });
  42. function VoteSelect(props: Props) {
  43. const [selectedTeam, setSelectedTeam] = useState('none');
  44. const [voteDialogVisible, setVoteDialogVisible] = useState(false);
  45. const [currentError, setCurrentError] = useState<ApiRejectType>({
  46. status: REQUEST_STATUS.SUCCESS,
  47. });
  48. const request = useAuthenticatedRequest('elections/vote', {
  49. team: parseInt(selectedTeam, 10),
  50. });
  51. const voteKeyExtractor = (item: VoteTeamType) => item.id.toString();
  52. const voteRenderItem = ({ item }: { item: VoteTeamType }) => (
  53. <RadioButton.Item label={item.name} value={item.id.toString()} />
  54. );
  55. const showVoteDialog = () => setVoteDialogVisible(true);
  56. const onVoteDialogDismiss = () => setVoteDialogVisible(false);
  57. const onVoteDialogAccept = async (): Promise<void> => {
  58. return new Promise((resolve: () => void) => {
  59. request()
  60. .then(() => {
  61. onVoteDialogDismiss();
  62. props.onVoteSuccess();
  63. resolve();
  64. })
  65. .catch((error: ApiRejectType) => {
  66. onVoteDialogDismiss();
  67. setCurrentError(error);
  68. resolve();
  69. });
  70. });
  71. };
  72. const onErrorDialogDismiss = () => {
  73. setCurrentError({ status: REQUEST_STATUS.SUCCESS });
  74. props.onVoteError();
  75. };
  76. return (
  77. <View>
  78. <Card style={styles.card}>
  79. <Card.Title
  80. title={i18n.t('screens.vote.select.title')}
  81. subtitle={i18n.t('screens.vote.select.subtitle')}
  82. left={(iconProps) => (
  83. <Avatar.Icon size={iconProps.size} icon="alert-decagram" />
  84. )}
  85. />
  86. <Card.Content>
  87. <RadioButton.Group
  88. onValueChange={setSelectedTeam}
  89. value={selectedTeam}
  90. >
  91. <FlatList
  92. data={props.teams}
  93. keyExtractor={voteKeyExtractor}
  94. extraData={selectedTeam}
  95. renderItem={voteRenderItem}
  96. />
  97. </RadioButton.Group>
  98. </Card.Content>
  99. <Card.Actions>
  100. <Button
  101. icon={'send'}
  102. mode={'contained'}
  103. onPress={showVoteDialog}
  104. style={styles.button}
  105. disabled={selectedTeam === 'none'}
  106. >
  107. {i18n.t('screens.vote.select.sendButton')}
  108. </Button>
  109. </Card.Actions>
  110. </Card>
  111. <LoadingConfirmDialog
  112. visible={voteDialogVisible}
  113. onDismiss={onVoteDialogDismiss}
  114. onAccept={onVoteDialogAccept}
  115. title={i18n.t('screens.vote.select.dialogTitle')}
  116. titleLoading={i18n.t('screens.vote.select.dialogTitleLoading')}
  117. message={i18n.t('screens.vote.select.dialogMessage')}
  118. />
  119. <ErrorDialog
  120. visible={currentError.status !== REQUEST_STATUS.SUCCESS}
  121. onDismiss={onErrorDialogDismiss}
  122. status={currentError.status}
  123. code={currentError.code}
  124. />
  125. </View>
  126. );
  127. }
  128. export default VoteSelect;