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 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // @flow
  2. import * as React from 'react';
  3. import {FlatList, 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. const FAKE_DATE = {
  12. "date_begin": "2020-04-07 21:50",
  13. "date_end": "2020-04-06 23:50",
  14. "date_result_begin": "2020-04-06 21:50",
  15. "date_result_end": "2020-04-07 22:50",
  16. };
  17. const FAKE_DATE2 = {
  18. "date_begin": null,
  19. "date_end": null,
  20. "date_result_begin": null,
  21. "date_result_end": null,
  22. };
  23. const FAKE_TEAMS = {
  24. has_voted: false,
  25. teams: [
  26. {
  27. id: 1,
  28. name: "TEST TEAM1",
  29. },
  30. {
  31. id: 2,
  32. name: "TEST TEAM2",
  33. },
  34. ],
  35. };
  36. const FAKE_TEAMS2 = {
  37. has_voted: false,
  38. teams: [
  39. {
  40. id: 1,
  41. name: "TEST TEAM1",
  42. votes: 1,
  43. },
  44. {
  45. id: 2,
  46. name: "TEST TEAM2",
  47. votes: 9,
  48. },
  49. ],
  50. };
  51. type Props = {
  52. navigation: Object
  53. }
  54. type State = {
  55. hasVoted: boolean,
  56. }
  57. export default class VoteScreen extends React.Component<Props, State> {
  58. state = {
  59. hasVoted: false,
  60. };
  61. teams: Array<Object>;
  62. hasVoted: boolean;
  63. datesString: Object;
  64. dates: Object;
  65. today: Date;
  66. mainFlatListData: Array<Object>;
  67. authRef: Object;
  68. constructor() {
  69. super();
  70. this.hasVoted = false;
  71. this.today = new Date();
  72. this.authRef = React.createRef();
  73. this.mainFlatListData = [
  74. {key: 'main'},
  75. {key: 'info'},
  76. ]
  77. }
  78. reloadData = () => this.authRef.current.reload();
  79. generateDateObject() {
  80. this.dates = {
  81. date_begin: stringToDate(this.datesString.date_begin),
  82. date_end: stringToDate(this.datesString.date_end),
  83. date_result_begin: stringToDate(this.datesString.date_result_begin),
  84. date_result_end: stringToDate(this.datesString.date_result_end),
  85. };
  86. }
  87. getDateString(date: Date, dateString: string): string {
  88. if (this.today.getDate() === date.getDate()) {
  89. const str = getTimeOnlyString(dateString);
  90. return str !== null ? str : "";
  91. } else
  92. return dateString;
  93. }
  94. isVoteAvailable() {
  95. return this.dates.date_begin !== null;
  96. }
  97. isVoteRunning() {
  98. return this.today > this.dates.date_begin && this.today < this.dates.date_end;
  99. }
  100. isVoteStarted() {
  101. return this.today > this.dates.date_begin;
  102. }
  103. isResultRunning() {
  104. return this.today > this.dates.date_result_begin && this.today < this.dates.date_result_end;
  105. }
  106. isResultStarted() {
  107. return this.today > this.dates.date_result_begin;
  108. }
  109. mainRenderItem = ({item}: Object) => {
  110. if (item.key === 'info')
  111. return <VoteTitle/>;
  112. else if (item.key === 'main' && this.isVoteAvailable())
  113. return this.getContent();
  114. else
  115. return null;
  116. };
  117. getScreen = (data: Array<Object | null>) => {
  118. // data[0] = FAKE_TEAMS2;
  119. // data[1] = FAKE_DATE;
  120. if (data[1] === null)
  121. data[1] = {date_begin: null};
  122. if (data[0] !== null) {
  123. this.teams = data[0].teams;
  124. this.hasVoted = data[0].has_voted;
  125. }
  126. this.datesString = data[1];
  127. this.generateDateObject();
  128. return (
  129. <View>
  130. {/*$FlowFixMe*/}
  131. <FlatList
  132. data={this.mainFlatListData}
  133. extraData={this.state.hasVoted.toString()}
  134. renderItem={this.mainRenderItem}
  135. />
  136. </View>
  137. );
  138. };
  139. getContent() {
  140. if (!this.isVoteStarted())
  141. return this.getTeaseVoteCard();
  142. else if (this.isVoteRunning() && (!this.hasVoted && !this.state.hasVoted))
  143. return this.getVoteCard();
  144. else if (!this.isResultStarted())
  145. return this.getWaitVoteCard();
  146. else if (this.isResultRunning())
  147. return this.getVoteResultCard();
  148. else
  149. return null;
  150. }
  151. onVoteSuccess = () => this.setState({hasVoted: true});
  152. /**
  153. * The user has not voted yet, and the votes are open
  154. */
  155. getVoteCard() {
  156. return <VoteSelect teams={this.teams} onVoteSuccess={this.onVoteSuccess} onVoteError={this.reloadData}/>;
  157. }
  158. /**
  159. * Votes have ended, results can be displayed
  160. */
  161. getVoteResultCard() {
  162. return <VoteResults teams={this.teams}
  163. dateEnd={this.getDateString(this.dates.date_result_end, this.datesString.date_result_end)}/>;
  164. }
  165. /**
  166. * Vote will open shortly
  167. */
  168. getTeaseVoteCard() {
  169. return <VoteTease startDate={this.getDateString(this.dates.date_begin, this.datesString.date_begin)}/>;
  170. }
  171. /**
  172. * Votes have ended, or user has voted waiting for results
  173. */
  174. getWaitVoteCard() {
  175. let startDate = null;
  176. if (this.dates.date_result_begin !== null)
  177. startDate = this.getDateString(this.dates.date_result_begin, this.datesString.date_result_begin);
  178. return <VoteWait startDate={startDate} hasVoted={this.hasVoted || this.state.hasVoted}
  179. justVoted={this.state.hasVoted}
  180. isVoteRunning={this.isVoteRunning()}/>;
  181. }
  182. render() {
  183. return (
  184. <AuthenticatedScreen
  185. {...this.props}
  186. ref={this.authRef}
  187. requests={[
  188. {
  189. link: 'elections/teams',
  190. params: {},
  191. mandatory: false,
  192. },
  193. {
  194. link: 'elections/datesString',
  195. params: {},
  196. mandatory: false,
  197. },
  198. ]}
  199. renderFunction={this.getScreen}
  200. />
  201. );
  202. }
  203. }