123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- /*
- * Copyright (c) 2019 - 2020 Arnaud Vergnet.
- *
- * This file is part of Campus INSAT.
- *
- * Campus INSAT is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Campus INSAT is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Campus INSAT. If not, see <https://www.gnu.org/licenses/>.
- */
-
- import * as React from 'react';
- import { StyleSheet, View } from 'react-native';
- import { Card } from 'react-native-paper';
- import i18n from 'i18n-js';
- import { StackNavigationProp } from '@react-navigation/stack';
- import { getDateOnlyString, getTimeOnlyString } from '../../utils/Planning';
- import DateManager from '../../managers/DateManager';
- import BasicLoadingScreen from '../../components/Screens/BasicLoadingScreen';
- import { ApiRejectType, apiRequest } from '../../utils/WebData';
- import ErrorView from '../../components/Screens/ErrorView';
- import CustomHTML from '../../components/Overrides/CustomHTML';
- import { TAB_BAR_HEIGHT } from '../../components/Tabbar/CustomTabBar';
- import CollapsibleScrollView from '../../components/Collapsible/CollapsibleScrollView';
- import type { PlanningEventType } from '../../utils/Planning';
- import ImageGalleryButton from '../../components/Media/ImageGalleryButton';
- import { API_REQUEST_CODES, REQUEST_STATUS } from '../../utils/Requests';
-
- type PropsType = {
- navigation: StackNavigationProp<any>;
- route: { params: { data: PlanningEventType; id: number; eventId: number } };
- };
-
- type StateType = {
- loading: boolean;
- };
-
- const styles = StyleSheet.create({
- container: {
- paddingLeft: 5,
- paddingRight: 5,
- },
- button: {
- width: 300,
- height: 300,
- marginLeft: 'auto',
- marginRight: 'auto',
- },
- });
-
- const EVENT_INFO_URL = 'event/info';
-
- /**
- * Class defining a planning event information page.
- */
- class PlanningDisplayScreen extends React.Component<PropsType, StateType> {
- displayData: null | PlanningEventType;
-
- shouldFetchData: boolean;
-
- eventId: number;
-
- error: ApiRejectType;
-
- /**
- * Generates data depending on whether the screen was opened from the planning or from a link
- *
- * @param props
- */
- constructor(props: PropsType) {
- super(props);
-
- if (props.route.params.data != null) {
- this.displayData = props.route.params.data;
- this.eventId = this.displayData.id;
- this.shouldFetchData = false;
- this.error = { status: REQUEST_STATUS.SUCCESS };
- this.state = {
- loading: false,
- };
- } else {
- this.displayData = null;
- this.eventId = props.route.params.eventId;
- this.shouldFetchData = true;
- this.error = { status: REQUEST_STATUS.SUCCESS };
- this.state = {
- loading: true,
- };
- this.fetchData();
- }
- }
-
- /**
- * Hides loading and saves fetched data
- *
- * @param data Received data
- */
- onFetchSuccess = (data: PlanningEventType) => {
- this.displayData = data;
- this.setState({ loading: false });
- };
-
- /**
- * Hides loading and saves the error code
- *
- * @param error
- */
- onFetchError = (error: ApiRejectType) => {
- this.error = error;
- this.setState({ loading: false });
- };
-
- /**
- * Gets content to display
- *
- * @returns {*}
- */
- getContent() {
- const { displayData } = this;
- if (displayData == null) {
- return null;
- }
- let subtitle = getTimeOnlyString(displayData.date_begin);
- const dateString = getDateOnlyString(displayData.date_begin);
- if (dateString !== null && subtitle != null) {
- subtitle += ` | ${DateManager.getInstance().getTranslatedDate(
- dateString
- )}`;
- }
- return (
- <CollapsibleScrollView style={styles.container} hasTab>
- <Card.Title title={displayData.title} subtitle={subtitle} />
- {displayData.logo !== null ? (
- <ImageGalleryButton
- images={[{ url: displayData.logo }]}
- style={styles.button}
- />
- ) : null}
-
- {displayData.description !== null ? (
- <Card.Content style={{ paddingBottom: TAB_BAR_HEIGHT + 20 }}>
- <CustomHTML html={displayData.description} />
- </Card.Content>
- ) : (
- <View />
- )}
- </CollapsibleScrollView>
- );
- }
-
- /**
- * Shows an error view and use a custom message if the event does not exist
- *
- * @returns {*}
- */
- getErrorView() {
- if (this.error.code === API_REQUEST_CODES.BAD_INPUT) {
- return (
- <ErrorView
- message={i18n.t('screens.planning.invalidEvent')}
- icon="calendar-remove"
- />
- );
- }
- return (
- <ErrorView
- status={this.error.status}
- code={this.error.code}
- button={{
- icon: 'refresh',
- text: i18n.t('general.retry'),
- onPress: this.fetchData,
- }}
- />
- );
- }
-
- /**
- * Fetches data for the current event id from the API
- */
- fetchData = () => {
- this.setState({ loading: true });
- apiRequest<PlanningEventType>(EVENT_INFO_URL, 'POST', { id: this.eventId })
- .then(this.onFetchSuccess)
- .catch(this.onFetchError);
- };
-
- render() {
- const { loading } = this.state;
- if (loading) {
- return <BasicLoadingScreen />;
- }
- if (
- this.error.status === REQUEST_STATUS.SUCCESS &&
- this.error.code === undefined
- ) {
- return this.getContent();
- }
- return this.getErrorView();
- }
- }
-
- export default PlanningDisplayScreen;
|