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.tsx 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (c) 2019 - 2020 Arnaud Vergnet.
  3. *
  4. * This file is part of Campus INSAT.
  5. *
  6. * Campus INSAT is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Campus INSAT is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Campus INSAT. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. import * as React from 'react';
  20. import { StyleSheet, View } from 'react-native';
  21. import { Card } from 'react-native-paper';
  22. import i18n from 'i18n-js';
  23. import { StackNavigationProp } from '@react-navigation/stack';
  24. import { getDateOnlyString, getTimeOnlyString } from '../../utils/Planning';
  25. import DateManager from '../../managers/DateManager';
  26. import BasicLoadingScreen from '../../components/Screens/BasicLoadingScreen';
  27. import { ApiRejectType, apiRequest } from '../../utils/WebData';
  28. import ErrorView from '../../components/Screens/ErrorView';
  29. import CustomHTML from '../../components/Overrides/CustomHTML';
  30. import { TAB_BAR_HEIGHT } from '../../components/Tabbar/CustomTabBar';
  31. import CollapsibleScrollView from '../../components/Collapsible/CollapsibleScrollView';
  32. import type { PlanningEventType } from '../../utils/Planning';
  33. import ImageGalleryButton from '../../components/Media/ImageGalleryButton';
  34. import { API_REQUEST_CODES, REQUEST_STATUS } from '../../utils/Requests';
  35. type PropsType = {
  36. navigation: StackNavigationProp<any>;
  37. route: { params: { data: PlanningEventType; id: number; eventId: number } };
  38. };
  39. type StateType = {
  40. loading: boolean;
  41. };
  42. const styles = StyleSheet.create({
  43. container: {
  44. paddingLeft: 5,
  45. paddingRight: 5,
  46. },
  47. button: {
  48. width: 300,
  49. height: 300,
  50. marginLeft: 'auto',
  51. marginRight: 'auto',
  52. },
  53. });
  54. const EVENT_INFO_URL = 'event/info';
  55. /**
  56. * Class defining a planning event information page.
  57. */
  58. class PlanningDisplayScreen extends React.Component<PropsType, StateType> {
  59. displayData: null | PlanningEventType;
  60. shouldFetchData: boolean;
  61. eventId: number;
  62. error: ApiRejectType;
  63. /**
  64. * Generates data depending on whether the screen was opened from the planning or from a link
  65. *
  66. * @param props
  67. */
  68. constructor(props: PropsType) {
  69. super(props);
  70. if (props.route.params.data != null) {
  71. this.displayData = props.route.params.data;
  72. this.eventId = this.displayData.id;
  73. this.shouldFetchData = false;
  74. this.error = { status: REQUEST_STATUS.SUCCESS };
  75. this.state = {
  76. loading: false,
  77. };
  78. } else {
  79. this.displayData = null;
  80. this.eventId = props.route.params.eventId;
  81. this.shouldFetchData = true;
  82. this.error = { status: REQUEST_STATUS.SUCCESS };
  83. this.state = {
  84. loading: true,
  85. };
  86. this.fetchData();
  87. }
  88. }
  89. /**
  90. * Hides loading and saves fetched data
  91. *
  92. * @param data Received data
  93. */
  94. onFetchSuccess = (data: PlanningEventType) => {
  95. this.displayData = data;
  96. this.setState({ loading: false });
  97. };
  98. /**
  99. * Hides loading and saves the error code
  100. *
  101. * @param error
  102. */
  103. onFetchError = (error: ApiRejectType) => {
  104. this.error = error;
  105. this.setState({ loading: false });
  106. };
  107. /**
  108. * Gets content to display
  109. *
  110. * @returns {*}
  111. */
  112. getContent() {
  113. const { displayData } = this;
  114. if (displayData == null) {
  115. return null;
  116. }
  117. let subtitle = getTimeOnlyString(displayData.date_begin);
  118. const dateString = getDateOnlyString(displayData.date_begin);
  119. if (dateString !== null && subtitle != null) {
  120. subtitle += ` | ${DateManager.getInstance().getTranslatedDate(
  121. dateString
  122. )}`;
  123. }
  124. return (
  125. <CollapsibleScrollView style={styles.container} hasTab>
  126. <Card.Title title={displayData.title} subtitle={subtitle} />
  127. {displayData.logo !== null ? (
  128. <ImageGalleryButton
  129. images={[{ url: displayData.logo }]}
  130. style={styles.button}
  131. />
  132. ) : null}
  133. {displayData.description !== null ? (
  134. <Card.Content style={{ paddingBottom: TAB_BAR_HEIGHT + 20 }}>
  135. <CustomHTML html={displayData.description} />
  136. </Card.Content>
  137. ) : (
  138. <View />
  139. )}
  140. </CollapsibleScrollView>
  141. );
  142. }
  143. /**
  144. * Shows an error view and use a custom message if the event does not exist
  145. *
  146. * @returns {*}
  147. */
  148. getErrorView() {
  149. if (this.error.code === API_REQUEST_CODES.BAD_INPUT) {
  150. return (
  151. <ErrorView
  152. message={i18n.t('screens.planning.invalidEvent')}
  153. icon="calendar-remove"
  154. />
  155. );
  156. }
  157. return (
  158. <ErrorView
  159. status={this.error.status}
  160. code={this.error.code}
  161. button={{
  162. icon: 'refresh',
  163. text: i18n.t('general.retry'),
  164. onPress: this.fetchData,
  165. }}
  166. />
  167. );
  168. }
  169. /**
  170. * Fetches data for the current event id from the API
  171. */
  172. fetchData = () => {
  173. this.setState({ loading: true });
  174. apiRequest<PlanningEventType>(EVENT_INFO_URL, 'POST', { id: this.eventId })
  175. .then(this.onFetchSuccess)
  176. .catch(this.onFetchError);
  177. };
  178. render() {
  179. const { loading } = this.state;
  180. if (loading) {
  181. return <BasicLoadingScreen />;
  182. }
  183. if (
  184. this.error.status === REQUEST_STATUS.SUCCESS &&
  185. this.error.code === undefined
  186. ) {
  187. return this.getContent();
  188. }
  189. return this.getErrorView();
  190. }
  191. }
  192. export default PlanningDisplayScreen;