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.

PlanningScreen.js 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // @flow
  2. import * as React from 'react';
  3. import {BackHandler, View} from 'react-native';
  4. import i18n from 'i18n-js';
  5. import {Agenda, LocaleConfig} from 'react-native-calendars';
  6. import {Avatar, Divider, List} from 'react-native-paper';
  7. import {StackNavigationProp} from '@react-navigation/stack';
  8. import {readData} from '../../utils/WebData';
  9. import type {PlanningEventType} from '../../utils/Planning';
  10. import {
  11. generateEventAgenda,
  12. getCurrentDateString,
  13. getDateOnlyString,
  14. getFormattedEventTime,
  15. } from '../../utils/Planning';
  16. import CustomAgenda from '../../components/Overrides/CustomAgenda';
  17. import {MASCOT_STYLE} from '../../components/Mascot/Mascot';
  18. import MascotPopup from '../../components/Mascot/MascotPopup';
  19. import AsyncStorageManager from '../../managers/AsyncStorageManager';
  20. LocaleConfig.locales.fr = {
  21. monthNames: [
  22. 'Janvier',
  23. 'Février',
  24. 'Mars',
  25. 'Avril',
  26. 'Mai',
  27. 'Juin',
  28. 'Juillet',
  29. 'Août',
  30. 'Septembre',
  31. 'Octobre',
  32. 'Novembre',
  33. 'Décembre',
  34. ],
  35. monthNamesShort: [
  36. 'Janv.',
  37. 'Févr.',
  38. 'Mars',
  39. 'Avril',
  40. 'Mai',
  41. 'Juin',
  42. 'Juil.',
  43. 'Août',
  44. 'Sept.',
  45. 'Oct.',
  46. 'Nov.',
  47. 'Déc.',
  48. ],
  49. dayNames: [
  50. 'Dimanche',
  51. 'Lundi',
  52. 'Mardi',
  53. 'Mercredi',
  54. 'Jeudi',
  55. 'Vendredi',
  56. 'Samedi',
  57. ],
  58. dayNamesShort: ['Dim', 'Lun', 'Mar', 'Mer', 'Jeu', 'Ven', 'Sam'],
  59. today: "Aujourd'hui",
  60. };
  61. type PropsType = {
  62. navigation: StackNavigationProp,
  63. };
  64. type StateType = {
  65. refreshing: boolean,
  66. agendaItems: {[key: string]: Array<PlanningEventType>},
  67. calendarShowing: boolean,
  68. };
  69. const FETCH_URL = 'https://www.amicale-insat.fr/api/event/list';
  70. const AGENDA_MONTH_SPAN = 3;
  71. /**
  72. * Class defining the app's planning screen
  73. */
  74. class PlanningScreen extends React.Component<PropsType, StateType> {
  75. agendaRef: null | Agenda;
  76. lastRefresh: Date;
  77. minTimeBetweenRefresh = 60;
  78. currentDate = getDateOnlyString(getCurrentDateString());
  79. constructor(props: PropsType) {
  80. super(props);
  81. if (i18n.currentLocale().startsWith('fr')) {
  82. LocaleConfig.defaultLocale = 'fr';
  83. }
  84. this.state = {
  85. refreshing: false,
  86. agendaItems: {},
  87. calendarShowing: false,
  88. };
  89. }
  90. /**
  91. * Captures focus and blur events to hook on android back button
  92. */
  93. componentDidMount() {
  94. const {navigation} = this.props;
  95. this.onRefresh();
  96. navigation.addListener('focus', () => {
  97. BackHandler.addEventListener(
  98. 'hardwareBackPress',
  99. this.onBackButtonPressAndroid,
  100. );
  101. });
  102. navigation.addListener('blur', () => {
  103. BackHandler.removeEventListener(
  104. 'hardwareBackPress',
  105. this.onBackButtonPressAndroid,
  106. );
  107. });
  108. }
  109. /**
  110. * Overrides default android back button behaviour to close the calendar if it was open.
  111. *
  112. * @return {boolean}
  113. */
  114. onBackButtonPressAndroid = (): boolean => {
  115. const {calendarShowing} = this.state;
  116. if (calendarShowing && this.agendaRef != null) {
  117. this.agendaRef.chooseDay(this.agendaRef.state.selectedDay);
  118. return true;
  119. }
  120. return false;
  121. };
  122. /**
  123. * Refreshes data and shows an animation while doing it
  124. */
  125. onRefresh = () => {
  126. let canRefresh;
  127. if (this.lastRefresh !== undefined)
  128. canRefresh =
  129. (new Date().getTime() - this.lastRefresh.getTime()) / 1000 >
  130. this.minTimeBetweenRefresh;
  131. else canRefresh = true;
  132. if (canRefresh) {
  133. this.setState({refreshing: true});
  134. readData(FETCH_URL)
  135. .then((fetchedData: Array<PlanningEventType>) => {
  136. this.setState({
  137. refreshing: false,
  138. agendaItems: generateEventAgenda(fetchedData, AGENDA_MONTH_SPAN),
  139. });
  140. this.lastRefresh = new Date();
  141. })
  142. .catch(() => {
  143. this.setState({
  144. refreshing: false,
  145. });
  146. });
  147. }
  148. };
  149. /**
  150. * Callback used when receiving the agenda ref
  151. *
  152. * @param ref
  153. */
  154. onAgendaRef = (ref: Agenda) => {
  155. this.agendaRef = ref;
  156. };
  157. /**
  158. * Callback used when a button is pressed to toggle the calendar
  159. *
  160. * @param isCalendarOpened True is the calendar is already open, false otherwise
  161. */
  162. onCalendarToggled = (isCalendarOpened: boolean) => {
  163. this.setState({calendarShowing: isCalendarOpened});
  164. };
  165. /**
  166. * Gets an event render item
  167. *
  168. * @param item The current event to render
  169. * @return {*}
  170. */
  171. getRenderItem = (item: PlanningEventType): React.Node => {
  172. const {navigation} = this.props;
  173. const onPress = () => {
  174. navigation.navigate('planning-information', {
  175. data: item,
  176. });
  177. };
  178. if (item.logo !== null) {
  179. return (
  180. <View>
  181. <Divider />
  182. <List.Item
  183. title={item.title}
  184. description={getFormattedEventTime(item.date_begin, item.date_end)}
  185. left={(): React.Node => (
  186. <Avatar.Image
  187. source={{uri: item.logo}}
  188. style={{backgroundColor: 'transparent'}}
  189. />
  190. )}
  191. onPress={onPress}
  192. />
  193. </View>
  194. );
  195. }
  196. return (
  197. <View>
  198. <Divider />
  199. <List.Item
  200. title={item.title}
  201. description={getFormattedEventTime(item.date_begin, item.date_end)}
  202. onPress={onPress}
  203. />
  204. </View>
  205. );
  206. };
  207. /**
  208. * Gets an empty render item for an empty date
  209. *
  210. * @return {*}
  211. */
  212. getRenderEmptyDate = (): React.Node => <Divider />;
  213. render(): React.Node {
  214. const {state, props} = this;
  215. return (
  216. <View style={{flex: 1}}>
  217. <CustomAgenda
  218. // eslint-disable-next-line react/jsx-props-no-spreading
  219. {...props}
  220. // the list of items that have to be displayed in agenda. If you want to render item as empty date
  221. // the value of date key kas to be an empty array []. If there exists no value for date key it is
  222. // considered that the date in question is not yet loaded
  223. items={state.agendaItems}
  224. // initially selected day
  225. selected={this.currentDate}
  226. // Minimum date that can be selected, dates before minDate will be grayed out. Default = undefined
  227. minDate={this.currentDate}
  228. // Max amount of months allowed to scroll to the past. Default = 50
  229. pastScrollRange={1}
  230. // Max amount of months allowed to scroll to the future. Default = 50
  231. futureScrollRange={AGENDA_MONTH_SPAN}
  232. // If provided, a standard RefreshControl will be added for "Pull to Refresh" functionality. Make sure to also set the refreshing prop correctly.
  233. onRefresh={this.onRefresh}
  234. // callback that fires when the calendar is opened or closed
  235. onCalendarToggled={this.onCalendarToggled}
  236. // Set this true while waiting for new data from a refresh
  237. refreshing={state.refreshing}
  238. renderItem={this.getRenderItem}
  239. renderEmptyDate={this.getRenderEmptyDate}
  240. // If firstDay=1 week starts from Monday. Note that dayNames and dayNamesShort should still start from Sunday.
  241. firstDay={1}
  242. // ref to this agenda in order to handle back button event
  243. onRef={this.onAgendaRef}
  244. />
  245. <MascotPopup
  246. prefKey={AsyncStorageManager.PREFERENCES.eventsShowMascot.key}
  247. title={i18n.t('screens.planning.mascotDialog.title')}
  248. message={i18n.t('screens.planning.mascotDialog.message')}
  249. icon="party-popper"
  250. buttons={{
  251. action: null,
  252. cancel: {
  253. message: i18n.t('screens.planning.mascotDialog.button'),
  254. icon: 'check',
  255. },
  256. }}
  257. emotion={MASCOT_STYLE.HAPPY}
  258. />
  259. </View>
  260. );
  261. }
  262. }
  263. export default PlanningScreen;