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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // @flow
  2. import * as React from 'react';
  3. import {Button, Subheading, withTheme} from 'react-native-paper';
  4. import {StyleSheet, View} from "react-native";
  5. import {MaterialCommunityIcons} from "@expo/vector-icons";
  6. import i18n from 'i18n-js';
  7. import {ERROR_TYPE} from "../../utils/WebData";
  8. import * as Animatable from 'react-native-animatable';
  9. type Props = {
  10. navigation: Object,
  11. route: Object,
  12. errorCode: number,
  13. onRefresh: Function,
  14. icon: string,
  15. message: string,
  16. showRetryButton: boolean,
  17. }
  18. type State = {
  19. refreshing: boolean,
  20. }
  21. class ErrorView extends React.PureComponent<Props, State> {
  22. colors: Object;
  23. message: string;
  24. icon: string;
  25. showLoginButton: boolean;
  26. static defaultProps = {
  27. errorCode: 0,
  28. icon: '',
  29. message: '',
  30. showRetryButton: true,
  31. }
  32. state = {
  33. refreshing: false,
  34. };
  35. constructor(props) {
  36. super(props);
  37. this.colors = props.theme.colors;
  38. }
  39. generateMessage() {
  40. this.showLoginButton = false;
  41. if (this.props.errorCode !== 0) {
  42. switch (this.props.errorCode) {
  43. case ERROR_TYPE.BAD_CREDENTIALS:
  44. this.message = i18n.t("errors.badCredentials");
  45. this.icon = "account-alert-outline";
  46. break;
  47. case ERROR_TYPE.BAD_TOKEN:
  48. this.message = i18n.t("errors.badToken");
  49. this.icon = "account-alert-outline";
  50. this.showLoginButton = true;
  51. break;
  52. case ERROR_TYPE.NO_CONSENT:
  53. this.message = i18n.t("errors.noConsent");
  54. this.icon = "account-remove-outline";
  55. break;
  56. case ERROR_TYPE.BAD_INPUT:
  57. this.message = i18n.t("errors.badInput");
  58. this.icon = "alert-circle-outline";
  59. break;
  60. case ERROR_TYPE.FORBIDDEN:
  61. this.message = i18n.t("errors.forbidden");
  62. this.icon = "lock";
  63. break;
  64. case ERROR_TYPE.CONNECTION_ERROR:
  65. this.message = i18n.t("errors.connectionError");
  66. this.icon = "access-point-network-off";
  67. break;
  68. case ERROR_TYPE.SERVER_ERROR:
  69. this.message = i18n.t("errors.serverError");
  70. this.icon = "server-network-off";
  71. break;
  72. default:
  73. this.message = i18n.t("errors.unknown");
  74. this.icon = "alert-circle-outline";
  75. break;
  76. }
  77. } else {
  78. this.message = this.props.message;
  79. this.icon = this.props.icon;
  80. }
  81. }
  82. getRetryButton() {
  83. return <Button
  84. mode={'contained'}
  85. icon={'refresh'}
  86. onPress={this.props.onRefresh}
  87. style={styles.button}
  88. >
  89. {i18n.t("general.retry")}
  90. </Button>;
  91. }
  92. goToLogin = () => {
  93. this.props.navigation.navigate("login",
  94. {
  95. screen: 'login',
  96. params: {nextScreen: this.props.route.name}
  97. })
  98. };
  99. getLoginButton() {
  100. return <Button
  101. mode={'contained'}
  102. icon={'login'}
  103. onPress={this.goToLogin}
  104. style={styles.button}
  105. >
  106. {i18n.t("screens.login")}
  107. </Button>;
  108. }
  109. render() {
  110. this.generateMessage();
  111. return (
  112. <Animatable.View
  113. style={{
  114. ...styles.outer,
  115. backgroundColor: this.colors.background
  116. }}
  117. animation={"zoomIn"}
  118. duration={200}
  119. useNativeDriver
  120. >
  121. <View style={styles.inner}>
  122. <View style={styles.iconContainer}>
  123. <MaterialCommunityIcons
  124. name={this.icon}
  125. size={150}
  126. color={this.colors.textDisabled}/>
  127. </View>
  128. <Subheading style={{
  129. ...styles.subheading,
  130. color: this.colors.textDisabled
  131. }}>
  132. {this.message}
  133. </Subheading>
  134. {this.props.showRetryButton
  135. ? (this.showLoginButton
  136. ? this.getLoginButton()
  137. : this.getRetryButton())
  138. : null}
  139. </View>
  140. </Animatable.View>
  141. );
  142. }
  143. }
  144. const styles = StyleSheet.create({
  145. outer: {
  146. height: '100%',
  147. },
  148. inner: {
  149. marginTop: 'auto',
  150. marginBottom: 'auto',
  151. },
  152. iconContainer: {
  153. marginLeft: 'auto',
  154. marginRight: 'auto',
  155. marginBottom: 20
  156. },
  157. subheading: {
  158. textAlign: 'center',
  159. paddingHorizontal: 20
  160. },
  161. button: {
  162. marginTop: 10,
  163. marginLeft: 'auto',
  164. marginRight: 'auto',
  165. }
  166. });
  167. export default withTheme(ErrorView);