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.

VoteScreen.js 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // @flow
  2. import * as React from 'react';
  3. import {FlatList, RefreshControl, View} from "react-native";
  4. import AuthenticatedScreen from "../../components/Amicale/AuthenticatedScreen";
  5. import {getTimeOnlyString, stringToDate} from "../../utils/Planning";
  6. import VoteTitle from "../../components/Amicale/Vote/VoteTitle";
  7. import VoteTease from "../../components/Amicale/Vote/VoteTease";
  8. import VoteSelect from "../../components/Amicale/Vote/VoteSelect";
  9. import VoteResults from "../../components/Amicale/Vote/VoteResults";
  10. import VoteWait from "../../components/Amicale/Vote/VoteWait";
  11. import {StackNavigationProp} from "@react-navigation/stack";
  12. export type team = {
  13. id: number,
  14. name: string,
  15. votes: number,
  16. }
  17. type teamResponse = {
  18. has_voted: boolean,
  19. teams: Array<team>,
  20. };
  21. type stringVoteDates = {
  22. date_begin: string,
  23. date_end: string,
  24. date_result_begin: string,
  25. date_result_end: string,
  26. }
  27. type objectVoteDates = {
  28. date_begin: Date,
  29. date_end: Date,
  30. date_result_begin: Date,
  31. date_result_end: Date,
  32. }
  33. // const FAKE_DATE = {
  34. // "date_begin": "2020-04-19 15:50",
  35. // "date_end": "2020-04-19 15:50",
  36. // "date_result_begin": "2020-04-19 19:50",
  37. // "date_result_end": "2020-04-19 22:50",
  38. // };
  39. //
  40. // const FAKE_DATE2 = {
  41. // "date_begin": null,
  42. // "date_end": null,
  43. // "date_result_begin": null,
  44. // "date_result_end": null,
  45. // };
  46. //
  47. // const FAKE_TEAMS = {
  48. // has_voted: false,
  49. // teams: [
  50. // {
  51. // id: 1,
  52. // name: "TEST TEAM1",
  53. // },
  54. // {
  55. // id: 2,
  56. // name: "TEST TEAM2",
  57. // },
  58. // ],
  59. // };
  60. // const FAKE_TEAMS2 = {
  61. // has_voted: false,
  62. // teams: [
  63. // {
  64. // id: 1,
  65. // name: "TEST TEAM1",
  66. // votes: 9,
  67. // },
  68. // {
  69. // id: 2,
  70. // name: "TEST TEAM2",
  71. // votes: 9,
  72. // },
  73. // {
  74. // id: 3,
  75. // name: "TEST TEAM3",
  76. // votes: 5,
  77. // },
  78. // ],
  79. // };
  80. const MIN_REFRESH_TIME = 5 * 1000;
  81. type Props = {
  82. navigation: StackNavigationProp
  83. }
  84. type State = {
  85. hasVoted: boolean,
  86. }
  87. /**
  88. * Screen displaying vote information and controls
  89. */
  90. export default class VoteScreen extends React.Component<Props, State> {
  91. state = {
  92. hasVoted: false,
  93. };
  94. teams: Array<team>;
  95. hasVoted: boolean;
  96. datesString: null | stringVoteDates;
  97. dates: null | objectVoteDates;
  98. today: Date;
  99. mainFlatListData: Array<{ key: string }>;
  100. lastRefresh: Date | null;
  101. authRef: { current: null | AuthenticatedScreen };
  102. constructor() {
  103. super();
  104. this.hasVoted = false;
  105. this.today = new Date();
  106. this.authRef = React.createRef();
  107. this.lastRefresh = null;
  108. this.mainFlatListData = [
  109. {key: 'main'},
  110. {key: 'info'},
  111. ]
  112. }
  113. /**
  114. * Reloads vote data if last refresh delta is smaller than the minimum refresh time
  115. */
  116. reloadData = () => {
  117. let canRefresh;
  118. const lastRefresh = this.lastRefresh;
  119. if (lastRefresh != null)
  120. canRefresh = (new Date().getTime() - lastRefresh.getTime()) > MIN_REFRESH_TIME;
  121. else
  122. canRefresh = true;
  123. if (canRefresh && this.authRef.current != null)
  124. this.authRef.current.reload()
  125. };
  126. /**
  127. * Generates the objects containing string and Date representations of key vote dates
  128. */
  129. generateDateObject() {
  130. const strings = this.datesString;
  131. if (strings != null) {
  132. const dateBegin = stringToDate(strings.date_begin);
  133. const dateEnd = stringToDate(strings.date_end);
  134. const dateResultBegin = stringToDate(strings.date_result_begin);
  135. const dateResultEnd = stringToDate(strings.date_result_end);
  136. if (dateBegin != null && dateEnd != null && dateResultBegin != null && dateResultEnd != null) {
  137. this.dates = {
  138. date_begin: dateBegin,
  139. date_end: dateEnd,
  140. date_result_begin: dateResultBegin,
  141. date_result_end: dateResultEnd,
  142. };
  143. } else
  144. this.dates = null;
  145. } else
  146. this.dates = null;
  147. }
  148. /**
  149. * Gets the string representation of the given date.
  150. *
  151. * If the given date is the same day as today, only return the tile.
  152. * Otherwise, return the full date.
  153. *
  154. * @param date The Date object representation of the wanted date
  155. * @param dateString The string representation of the wanted date
  156. * @returns {string}
  157. */
  158. getDateString(date: Date, dateString: string): string {
  159. if (this.today.getDate() === date.getDate()) {
  160. const str = getTimeOnlyString(dateString);
  161. return str != null ? str : "";
  162. } else
  163. return dateString;
  164. }
  165. isVoteRunning() {
  166. return this.dates != null && this.today > this.dates.date_begin && this.today < this.dates.date_end;
  167. }
  168. isVoteStarted() {
  169. return this.dates != null && this.today > this.dates.date_begin;
  170. }
  171. isResultRunning() {
  172. return this.dates != null && this.today > this.dates.date_result_begin && this.today < this.dates.date_result_end;
  173. }
  174. isResultStarted() {
  175. return this.dates != null && this.today > this.dates.date_result_begin;
  176. }
  177. mainRenderItem = ({item}: { item: { key: string } }) => {
  178. if (item.key === 'info')
  179. return <VoteTitle/>;
  180. else if (item.key === 'main' && this.dates != null)
  181. return this.getContent();
  182. else
  183. return null;
  184. };
  185. getScreen = (data: Array<{ [key: string]: any } | null>) => {
  186. // data[0] = FAKE_TEAMS2;
  187. // data[1] = FAKE_DATE;
  188. this.lastRefresh = new Date();
  189. const teams: teamResponse | null = data[0];
  190. const dateStrings: stringVoteDates | null = data[1];
  191. if (dateStrings != null && dateStrings.date_begin == null)
  192. this.datesString = null;
  193. else
  194. this.datesString = dateStrings;
  195. if (teams != null) {
  196. this.teams = teams.teams;
  197. this.hasVoted = teams.has_voted;
  198. }
  199. this.generateDateObject();
  200. return (
  201. <View>
  202. {/*$FlowFixMe*/}
  203. <FlatList
  204. data={this.mainFlatListData}
  205. refreshControl={
  206. <RefreshControl
  207. refreshing={false}
  208. onRefresh={this.reloadData}
  209. />
  210. }
  211. extraData={this.state.hasVoted.toString()}
  212. renderItem={this.mainRenderItem}
  213. />
  214. </View>
  215. );
  216. };
  217. getContent() {
  218. if (!this.isVoteStarted())
  219. return this.getTeaseVoteCard();
  220. else if (this.isVoteRunning() && (!this.hasVoted && !this.state.hasVoted))
  221. return this.getVoteCard();
  222. else if (!this.isResultStarted())
  223. return this.getWaitVoteCard();
  224. else if (this.isResultRunning())
  225. return this.getVoteResultCard();
  226. else
  227. return null;
  228. }
  229. onVoteSuccess = () => this.setState({hasVoted: true});
  230. /**
  231. * The user has not voted yet, and the votes are open
  232. */
  233. getVoteCard() {
  234. return <VoteSelect teams={this.teams} onVoteSuccess={this.onVoteSuccess} onVoteError={this.reloadData}/>;
  235. }
  236. /**
  237. * Votes have ended, results can be displayed
  238. */
  239. getVoteResultCard() {
  240. if (this.dates != null && this.datesString != null)
  241. return <VoteResults
  242. teams={this.teams}
  243. dateEnd={this.getDateString(
  244. this.dates.date_result_end,
  245. this.datesString.date_result_end)}
  246. />;
  247. else
  248. return null;
  249. }
  250. /**
  251. * Vote will open shortly
  252. */
  253. getTeaseVoteCard() {
  254. if (this.dates != null && this.datesString != null)
  255. return <VoteTease
  256. startDate={this.getDateString(this.dates.date_begin, this.datesString.date_begin)}/>;
  257. else
  258. return null;
  259. }
  260. /**
  261. * Votes have ended, or user has voted waiting for results
  262. */
  263. getWaitVoteCard() {
  264. let startDate = null;
  265. if (this.dates != null && this.datesString != null && this.dates.date_result_begin != null)
  266. startDate = this.getDateString(this.dates.date_result_begin, this.datesString.date_result_begin);
  267. return <VoteWait startDate={startDate} hasVoted={this.hasVoted || this.state.hasVoted}
  268. justVoted={this.state.hasVoted}
  269. isVoteRunning={this.isVoteRunning()}/>;
  270. }
  271. /**
  272. * Renders the authenticated screen.
  273. *
  274. * Teams and dates are not mandatory to allow showing the information box even if api requests fail
  275. *
  276. * @returns {*}
  277. */
  278. render() {
  279. return (
  280. <AuthenticatedScreen
  281. {...this.props}
  282. ref={this.authRef}
  283. requests={[
  284. {
  285. link: 'elections/teams',
  286. params: {},
  287. mandatory: false,
  288. },
  289. {
  290. link: 'elections/dates',
  291. params: {},
  292. mandatory: false,
  293. },
  294. ]}
  295. renderFunction={this.getScreen}
  296. />
  297. );
  298. }
  299. }