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

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