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.

VoteScreen.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. // @flow
  20. import * as React from 'react';
  21. import {RefreshControl, View} from 'react-native';
  22. import {StackNavigationProp} from '@react-navigation/stack';
  23. import i18n from 'i18n-js';
  24. import {Button} from 'react-native-paper';
  25. import AuthenticatedScreen from '../../components/Amicale/AuthenticatedScreen';
  26. import {getTimeOnlyString, stringToDate} from '../../utils/Planning';
  27. import VoteTease from '../../components/Amicale/Vote/VoteTease';
  28. import VoteSelect from '../../components/Amicale/Vote/VoteSelect';
  29. import VoteResults from '../../components/Amicale/Vote/VoteResults';
  30. import VoteWait from '../../components/Amicale/Vote/VoteWait';
  31. import {MASCOT_STYLE} from '../../components/Mascot/Mascot';
  32. import MascotPopup from '../../components/Mascot/MascotPopup';
  33. import AsyncStorageManager from '../../managers/AsyncStorageManager';
  34. import VoteNotAvailable from '../../components/Amicale/Vote/VoteNotAvailable';
  35. import CollapsibleFlatList from '../../components/Collapsible/CollapsibleFlatList';
  36. import type {ApiGenericDataType} from '../../utils/WebData';
  37. export type VoteTeamType = {
  38. id: number,
  39. name: string,
  40. votes: number,
  41. };
  42. type TeamResponseType = {
  43. has_voted: boolean,
  44. teams: Array<VoteTeamType>,
  45. };
  46. type VoteDatesStringType = {
  47. date_begin: string,
  48. date_end: string,
  49. date_result_begin: string,
  50. date_result_end: string,
  51. };
  52. type VoteDatesObjectType = {
  53. date_begin: Date,
  54. date_end: Date,
  55. date_result_begin: Date,
  56. date_result_end: Date,
  57. };
  58. // const FAKE_DATE = {
  59. // "date_begin": "2020-08-19 15:50",
  60. // "date_end": "2020-08-19 15:50",
  61. // "date_result_begin": "2020-08-19 19:50",
  62. // "date_result_end": "2020-08-19 22:50",
  63. // };
  64. //
  65. // const FAKE_DATE2 = {
  66. // "date_begin": null,
  67. // "date_end": null,
  68. // "date_result_begin": null,
  69. // "date_result_end": null,
  70. // };
  71. //
  72. // const FAKE_TEAMS = {
  73. // has_voted: false,
  74. // teams: [
  75. // {
  76. // id: 1,
  77. // name: "TEST TEAM1",
  78. // },
  79. // {
  80. // id: 2,
  81. // name: "TEST TEAM2",
  82. // },
  83. // ],
  84. // };
  85. // const FAKE_TEAMS2 = {
  86. // has_voted: false,
  87. // teams: [
  88. // {
  89. // id: 1,
  90. // name: "TEST TEAM1",
  91. // votes: 9,
  92. // },
  93. // {
  94. // id: 2,
  95. // name: "TEST TEAM2",
  96. // votes: 9,
  97. // },
  98. // {
  99. // id: 3,
  100. // name: "TEST TEAM3",
  101. // votes: 5,
  102. // },
  103. // ],
  104. // };
  105. const MIN_REFRESH_TIME = 5 * 1000;
  106. type PropsType = {
  107. navigation: StackNavigationProp,
  108. };
  109. type StateType = {
  110. hasVoted: boolean,
  111. mascotDialogVisible: boolean,
  112. };
  113. /**
  114. * Screen displaying vote information and controls
  115. */
  116. export default class VoteScreen extends React.Component<PropsType, StateType> {
  117. teams: Array<VoteTeamType>;
  118. hasVoted: boolean;
  119. datesString: null | VoteDatesStringType;
  120. dates: null | VoteDatesObjectType;
  121. today: Date;
  122. mainFlatListData: Array<{key: string}>;
  123. lastRefresh: Date | null;
  124. authRef: {current: null | AuthenticatedScreen};
  125. constructor() {
  126. super();
  127. this.state = {
  128. hasVoted: false,
  129. mascotDialogVisible: AsyncStorageManager.getBool(
  130. AsyncStorageManager.PREFERENCES.voteShowMascot.key,
  131. ),
  132. };
  133. this.hasVoted = false;
  134. this.today = new Date();
  135. this.authRef = React.createRef();
  136. this.lastRefresh = null;
  137. this.mainFlatListData = [{key: 'main'}, {key: 'info'}];
  138. }
  139. /**
  140. * Gets the string representation of the given date.
  141. *
  142. * If the given date is the same day as today, only return the tile.
  143. * Otherwise, return the full date.
  144. *
  145. * @param date The Date object representation of the wanted date
  146. * @param dateString The string representation of the wanted date
  147. * @returns {string}
  148. */
  149. getDateString(date: Date, dateString: string): string {
  150. if (this.today.getDate() === date.getDate()) {
  151. const str = getTimeOnlyString(dateString);
  152. return str != null ? str : '';
  153. }
  154. return dateString;
  155. }
  156. getMainRenderItem = ({item}: {item: {key: string}}): React.Node => {
  157. if (item.key === 'info')
  158. return (
  159. <View>
  160. <Button
  161. mode="contained"
  162. icon="help-circle"
  163. onPress={this.showMascotDialog}
  164. style={{
  165. marginLeft: 'auto',
  166. marginRight: 'auto',
  167. marginTop: 20,
  168. }}>
  169. {i18n.t('screens.vote.mascotDialog.title')}
  170. </Button>
  171. </View>
  172. );
  173. return this.getContent();
  174. };
  175. getScreen = (data: Array<ApiGenericDataType | null>): React.Node => {
  176. const {state} = this;
  177. // data[0] = FAKE_TEAMS2;
  178. // data[1] = FAKE_DATE;
  179. this.lastRefresh = new Date();
  180. const teams: TeamResponseType | null = data[0];
  181. const dateStrings: VoteDatesStringType | null = data[1];
  182. if (dateStrings != null && dateStrings.date_begin == null)
  183. this.datesString = null;
  184. else this.datesString = dateStrings;
  185. if (teams != null) {
  186. this.teams = teams.teams;
  187. this.hasVoted = teams.has_voted;
  188. }
  189. this.generateDateObject();
  190. return (
  191. <CollapsibleFlatList
  192. data={this.mainFlatListData}
  193. refreshControl={
  194. <RefreshControl refreshing={false} onRefresh={this.reloadData} />
  195. }
  196. extraData={state.hasVoted.toString()}
  197. renderItem={this.getMainRenderItem}
  198. />
  199. );
  200. };
  201. getContent(): React.Node {
  202. const {state} = this;
  203. if (!this.isVoteStarted()) return this.getTeaseVoteCard();
  204. if (this.isVoteRunning() && !this.hasVoted && !state.hasVoted)
  205. return this.getVoteCard();
  206. if (!this.isResultStarted()) return this.getWaitVoteCard();
  207. if (this.isResultRunning()) return this.getVoteResultCard();
  208. return <VoteNotAvailable />;
  209. }
  210. onVoteSuccess = (): void => this.setState({hasVoted: true});
  211. /**
  212. * The user has not voted yet, and the votes are open
  213. */
  214. getVoteCard(): React.Node {
  215. return (
  216. <VoteSelect
  217. teams={this.teams}
  218. onVoteSuccess={this.onVoteSuccess}
  219. onVoteError={this.reloadData}
  220. />
  221. );
  222. }
  223. /**
  224. * Votes have ended, results can be displayed
  225. */
  226. getVoteResultCard(): React.Node {
  227. if (this.dates != null && this.datesString != null)
  228. return (
  229. <VoteResults
  230. teams={this.teams}
  231. dateEnd={this.getDateString(
  232. this.dates.date_result_end,
  233. this.datesString.date_result_end,
  234. )}
  235. />
  236. );
  237. return <VoteNotAvailable />;
  238. }
  239. /**
  240. * Vote will open shortly
  241. */
  242. getTeaseVoteCard(): React.Node {
  243. if (this.dates != null && this.datesString != null)
  244. return (
  245. <VoteTease
  246. startDate={this.getDateString(
  247. this.dates.date_begin,
  248. this.datesString.date_begin,
  249. )}
  250. />
  251. );
  252. return <VoteNotAvailable />;
  253. }
  254. /**
  255. * Votes have ended, or user has voted waiting for results
  256. */
  257. getWaitVoteCard(): React.Node {
  258. const {state} = this;
  259. let startDate = null;
  260. if (
  261. this.dates != null &&
  262. this.datesString != null &&
  263. this.dates.date_result_begin != null
  264. )
  265. startDate = this.getDateString(
  266. this.dates.date_result_begin,
  267. this.datesString.date_result_begin,
  268. );
  269. return (
  270. <VoteWait
  271. startDate={startDate}
  272. hasVoted={this.hasVoted || state.hasVoted}
  273. justVoted={state.hasVoted}
  274. isVoteRunning={this.isVoteRunning()}
  275. />
  276. );
  277. }
  278. /**
  279. * Reloads vote data if last refresh delta is smaller than the minimum refresh time
  280. */
  281. reloadData = () => {
  282. let canRefresh;
  283. const {lastRefresh} = this;
  284. if (lastRefresh != null)
  285. canRefresh =
  286. new Date().getTime() - lastRefresh.getTime() > MIN_REFRESH_TIME;
  287. else canRefresh = true;
  288. if (canRefresh && this.authRef.current != null)
  289. this.authRef.current.reload();
  290. };
  291. showMascotDialog = () => {
  292. this.setState({mascotDialogVisible: true});
  293. };
  294. hideMascotDialog = () => {
  295. AsyncStorageManager.set(
  296. AsyncStorageManager.PREFERENCES.voteShowMascot.key,
  297. false,
  298. );
  299. this.setState({mascotDialogVisible: false});
  300. };
  301. isVoteStarted(): boolean {
  302. return this.dates != null && this.today > this.dates.date_begin;
  303. }
  304. isResultRunning(): boolean {
  305. return (
  306. this.dates != null &&
  307. this.today > this.dates.date_result_begin &&
  308. this.today < this.dates.date_result_end
  309. );
  310. }
  311. isResultStarted(): boolean {
  312. return this.dates != null && this.today > this.dates.date_result_begin;
  313. }
  314. isVoteRunning(): boolean {
  315. return (
  316. this.dates != null &&
  317. this.today > this.dates.date_begin &&
  318. this.today < this.dates.date_end
  319. );
  320. }
  321. /**
  322. * Generates the objects containing string and Date representations of key vote dates
  323. */
  324. generateDateObject() {
  325. const strings = this.datesString;
  326. if (strings != null) {
  327. const dateBegin = stringToDate(strings.date_begin);
  328. const dateEnd = stringToDate(strings.date_end);
  329. const dateResultBegin = stringToDate(strings.date_result_begin);
  330. const dateResultEnd = stringToDate(strings.date_result_end);
  331. if (
  332. dateBegin != null &&
  333. dateEnd != null &&
  334. dateResultBegin != null &&
  335. dateResultEnd != null
  336. ) {
  337. this.dates = {
  338. date_begin: dateBegin,
  339. date_end: dateEnd,
  340. date_result_begin: dateResultBegin,
  341. date_result_end: dateResultEnd,
  342. };
  343. } else this.dates = null;
  344. } else this.dates = null;
  345. }
  346. /**
  347. * Renders the authenticated screen.
  348. *
  349. * Teams and dates are not mandatory to allow showing the information box even if api requests fail
  350. *
  351. * @returns {*}
  352. */
  353. render(): React.Node {
  354. const {props, state} = this;
  355. return (
  356. <View style={{flex: 1}}>
  357. <AuthenticatedScreen
  358. navigation={props.navigation}
  359. ref={this.authRef}
  360. requests={[
  361. {
  362. link: 'elections/teams',
  363. params: {},
  364. mandatory: false,
  365. },
  366. {
  367. link: 'elections/dates',
  368. params: {},
  369. mandatory: false,
  370. },
  371. ]}
  372. renderFunction={this.getScreen}
  373. />
  374. <MascotPopup
  375. visible={state.mascotDialogVisible}
  376. title={i18n.t('screens.vote.mascotDialog.title')}
  377. message={i18n.t('screens.vote.mascotDialog.message')}
  378. icon="vote"
  379. buttons={{
  380. action: null,
  381. cancel: {
  382. message: i18n.t('screens.vote.mascotDialog.button'),
  383. icon: 'check',
  384. onPress: this.hideMascotDialog,
  385. },
  386. }}
  387. emotion={MASCOT_STYLE.CUTE}
  388. />
  389. </View>
  390. );
  391. }
  392. }