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.

PlanningDisplayScreen.js 4.9KB

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