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.

ErrorView.js 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. // @flow
  20. import * as React from 'react';
  21. import {Button, Subheading, withTheme} from 'react-native-paper';
  22. import {StyleSheet, View} from 'react-native';
  23. import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
  24. import i18n from 'i18n-js';
  25. import * as Animatable from 'react-native-animatable';
  26. import {StackNavigationProp} from '@react-navigation/stack';
  27. import {ERROR_TYPE} from '../../utils/WebData';
  28. import type {CustomThemeType} from '../../managers/ThemeManager';
  29. type PropsType = {
  30. navigation: StackNavigationProp,
  31. theme: CustomThemeType,
  32. route: {name: string},
  33. onRefresh?: () => void,
  34. errorCode?: number,
  35. icon?: string,
  36. message?: string,
  37. showRetryButton?: boolean,
  38. };
  39. const styles = StyleSheet.create({
  40. outer: {
  41. height: '100%',
  42. },
  43. inner: {
  44. marginTop: 'auto',
  45. marginBottom: 'auto',
  46. },
  47. iconContainer: {
  48. marginLeft: 'auto',
  49. marginRight: 'auto',
  50. marginBottom: 20,
  51. },
  52. subheading: {
  53. textAlign: 'center',
  54. paddingHorizontal: 20,
  55. },
  56. button: {
  57. marginTop: 10,
  58. marginLeft: 'auto',
  59. marginRight: 'auto',
  60. },
  61. });
  62. class ErrorView extends React.PureComponent<PropsType> {
  63. static defaultProps = {
  64. onRefresh: () => {},
  65. errorCode: 0,
  66. icon: '',
  67. message: '',
  68. showRetryButton: true,
  69. };
  70. message: string;
  71. icon: string;
  72. showLoginButton: boolean;
  73. constructor(props: PropsType) {
  74. super(props);
  75. this.icon = '';
  76. }
  77. getRetryButton(): React.Node {
  78. const {props} = this;
  79. return (
  80. <Button
  81. mode="contained"
  82. icon="refresh"
  83. onPress={props.onRefresh}
  84. style={styles.button}>
  85. {i18n.t('general.retry')}
  86. </Button>
  87. );
  88. }
  89. getLoginButton(): React.Node {
  90. return (
  91. <Button
  92. mode="contained"
  93. icon="login"
  94. onPress={this.goToLogin}
  95. style={styles.button}>
  96. {i18n.t('screens.login.title')}
  97. </Button>
  98. );
  99. }
  100. goToLogin = () => {
  101. const {props} = this;
  102. props.navigation.navigate('login', {
  103. screen: 'login',
  104. params: {nextScreen: props.route.name},
  105. });
  106. };
  107. generateMessage() {
  108. const {props} = this;
  109. this.showLoginButton = false;
  110. if (props.errorCode !== 0) {
  111. switch (props.errorCode) {
  112. case ERROR_TYPE.BAD_CREDENTIALS:
  113. this.message = i18n.t('errors.badCredentials');
  114. this.icon = 'account-alert-outline';
  115. break;
  116. case ERROR_TYPE.BAD_TOKEN:
  117. this.message = i18n.t('errors.badToken');
  118. this.icon = 'account-alert-outline';
  119. this.showLoginButton = true;
  120. break;
  121. case ERROR_TYPE.NO_CONSENT:
  122. this.message = i18n.t('errors.noConsent');
  123. this.icon = 'account-remove-outline';
  124. break;
  125. case ERROR_TYPE.TOKEN_SAVE:
  126. this.message = i18n.t('errors.tokenSave');
  127. this.icon = 'alert-circle-outline';
  128. break;
  129. case ERROR_TYPE.BAD_INPUT:
  130. this.message = i18n.t('errors.badInput');
  131. this.icon = 'alert-circle-outline';
  132. break;
  133. case ERROR_TYPE.FORBIDDEN:
  134. this.message = i18n.t('errors.forbidden');
  135. this.icon = 'lock';
  136. break;
  137. case ERROR_TYPE.CONNECTION_ERROR:
  138. this.message = i18n.t('errors.connectionError');
  139. this.icon = 'access-point-network-off';
  140. break;
  141. case ERROR_TYPE.SERVER_ERROR:
  142. this.message = i18n.t('errors.serverError');
  143. this.icon = 'server-network-off';
  144. break;
  145. default:
  146. this.message = i18n.t('errors.unknown');
  147. this.icon = 'alert-circle-outline';
  148. break;
  149. }
  150. this.message += `\n\nCode ${
  151. props.errorCode != null ? props.errorCode : -1
  152. }`;
  153. } else {
  154. this.message = props.message != null ? props.message : '';
  155. this.icon = props.icon != null ? props.icon : '';
  156. }
  157. }
  158. render(): React.Node {
  159. const {props} = this;
  160. this.generateMessage();
  161. let button;
  162. if (this.showLoginButton) button = this.getLoginButton();
  163. else if (props.showRetryButton) button = this.getRetryButton();
  164. else button = null;
  165. return (
  166. <Animatable.View
  167. style={{
  168. ...styles.outer,
  169. backgroundColor: props.theme.colors.background,
  170. }}
  171. animation="zoomIn"
  172. duration={200}
  173. useNativeDriver>
  174. <View style={styles.inner}>
  175. <View style={styles.iconContainer}>
  176. <MaterialCommunityIcons
  177. // $FlowFixMe
  178. name={this.icon}
  179. size={150}
  180. color={props.theme.colors.textDisabled}
  181. />
  182. </View>
  183. <Subheading
  184. style={{
  185. ...styles.subheading,
  186. color: props.theme.colors.textDisabled,
  187. }}>
  188. {this.message}
  189. </Subheading>
  190. {button}
  191. </View>
  192. </Animatable.View>
  193. );
  194. }
  195. }
  196. export default withTheme(ErrorView);