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.

ErrorView.tsx 5.7KB

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