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.

PlanningDisplayScreen.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // @flow
  2. import * as React from 'react';
  3. import {View} from 'react-native';
  4. import {Card} from 'react-native-paper';
  5. import i18n from 'i18n-js';
  6. import {StackNavigationProp} from '@react-navigation/stack';
  7. import {getDateOnlyString, getFormattedEventTime} from '../../utils/Planning';
  8. import DateManager from '../../managers/DateManager';
  9. import BasicLoadingScreen from '../../components/Screens/BasicLoadingScreen';
  10. import {apiRequest, ERROR_TYPE} from '../../utils/WebData';
  11. import ErrorView from '../../components/Screens/ErrorView';
  12. import CustomHTML from '../../components/Overrides/CustomHTML';
  13. import CustomTabBar from '../../components/Tabbar/CustomTabBar';
  14. import CollapsibleScrollView from '../../components/Collapsible/CollapsibleScrollView';
  15. import type {PlanningEventType} from '../../utils/Planning';
  16. import ImageGalleryButton from '../../components/Media/ImageGalleryButton';
  17. type PropsType = {
  18. navigation: StackNavigationProp,
  19. route: {params: {data: PlanningEventType, id: number, eventId: number}},
  20. };
  21. type StateType = {
  22. loading: boolean,
  23. };
  24. const EVENT_INFO_URL = 'event/info';
  25. /**
  26. * Class defining a planning event information page.
  27. */
  28. class PlanningDisplayScreen extends React.Component<PropsType, StateType> {
  29. displayData: null | PlanningEventType;
  30. shouldFetchData: boolean;
  31. eventId: number;
  32. errorCode: number;
  33. /**
  34. * Generates data depending on whether the screen was opened from the planning or from a link
  35. *
  36. * @param props
  37. */
  38. constructor(props: PropsType) {
  39. super(props);
  40. if (props.route.params.data != null) {
  41. this.displayData = props.route.params.data;
  42. this.eventId = this.displayData.id;
  43. this.shouldFetchData = false;
  44. this.errorCode = 0;
  45. this.state = {
  46. loading: false,
  47. };
  48. } else {
  49. this.displayData = null;
  50. this.eventId = props.route.params.eventId;
  51. this.shouldFetchData = true;
  52. this.errorCode = 0;
  53. this.state = {
  54. loading: true,
  55. };
  56. this.fetchData();
  57. }
  58. }
  59. /**
  60. * Hides loading and saves fetched data
  61. *
  62. * @param data Received data
  63. */
  64. onFetchSuccess = (data: PlanningEventType) => {
  65. this.displayData = data;
  66. this.setState({loading: false});
  67. };
  68. /**
  69. * Hides loading and saves the error code
  70. *
  71. * @param error
  72. */
  73. onFetchError = (error: number) => {
  74. this.errorCode = error;
  75. this.setState({loading: false});
  76. };
  77. /**
  78. * Gets content to display
  79. *
  80. * @returns {*}
  81. */
  82. getContent(): React.Node {
  83. const {navigation} = this.props;
  84. const {displayData} = this;
  85. if (displayData == null) return null;
  86. let subtitle = getFormattedEventTime(
  87. displayData.date_begin,
  88. displayData.date_end,
  89. );
  90. const dateString = getDateOnlyString(displayData.date_begin);
  91. if (dateString !== null)
  92. subtitle += ` | ${DateManager.getInstance().getTranslatedDate(
  93. dateString,
  94. )}`;
  95. return (
  96. <CollapsibleScrollView style={{paddingLeft: 5, paddingRight: 5}} hasTab>
  97. <Card.Title title={displayData.title} subtitle={subtitle} />
  98. {displayData.logo !== null ? (
  99. <ImageGalleryButton
  100. navigation={navigation}
  101. images={[{url: displayData.logo}]}
  102. style={{
  103. width: 300,
  104. height: 300,
  105. marginLeft: 'auto',
  106. marginRight: 'auto',
  107. }}
  108. />
  109. ) : null}
  110. {displayData.description !== null ? (
  111. <Card.Content
  112. style={{paddingBottom: CustomTabBar.TAB_BAR_HEIGHT + 20}}>
  113. <CustomHTML html={displayData.description} />
  114. </Card.Content>
  115. ) : (
  116. <View />
  117. )}
  118. </CollapsibleScrollView>
  119. );
  120. }
  121. /**
  122. * Shows an error view and use a custom message if the event does not exist
  123. *
  124. * @returns {*}
  125. */
  126. getErrorView(): React.Node {
  127. const {navigation} = this.props;
  128. if (this.errorCode === ERROR_TYPE.BAD_INPUT)
  129. return (
  130. <ErrorView
  131. navigation={navigation}
  132. showRetryButton={false}
  133. message={i18n.t('screens.planning.invalidEvent')}
  134. icon="calendar-remove"
  135. />
  136. );
  137. return (
  138. <ErrorView
  139. navigation={navigation}
  140. errorCode={this.errorCode}
  141. onRefresh={this.fetchData}
  142. />
  143. );
  144. }
  145. /**
  146. * Fetches data for the current event id from the API
  147. */
  148. fetchData = () => {
  149. this.setState({loading: true});
  150. apiRequest(EVENT_INFO_URL, 'POST', {id: this.eventId})
  151. .then(this.onFetchSuccess)
  152. .catch(this.onFetchError);
  153. };
  154. render(): React.Node {
  155. const {loading} = this.state;
  156. if (loading) return <BasicLoadingScreen />;
  157. if (this.errorCode === 0) return this.getContent();
  158. return this.getErrorView();
  159. }
  160. }
  161. export default PlanningDisplayScreen;