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.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 "react-native-vector-icons/MaterialCommunityIcons";
  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. this.icon = "";
  39. }
  40. generateMessage() {
  41. this.showLoginButton = false;
  42. if (this.props.errorCode !== 0) {
  43. switch (this.props.errorCode) {
  44. case ERROR_TYPE.BAD_CREDENTIALS:
  45. this.message = i18n.t("errors.badCredentials");
  46. this.icon = "account-alert-outline";
  47. break;
  48. case ERROR_TYPE.BAD_TOKEN:
  49. this.message = i18n.t("errors.badToken");
  50. this.icon = "account-alert-outline";
  51. this.showLoginButton = true;
  52. break;
  53. case ERROR_TYPE.NO_CONSENT:
  54. this.message = i18n.t("errors.noConsent");
  55. this.icon = "account-remove-outline";
  56. break;
  57. case ERROR_TYPE.TOKEN_SAVE:
  58. this.message = i18n.t("errors.tokenSave");
  59. this.icon = "alert-circle-outline";
  60. break;
  61. case ERROR_TYPE.BAD_INPUT:
  62. this.message = i18n.t("errors.badInput");
  63. this.icon = "alert-circle-outline";
  64. break;
  65. case ERROR_TYPE.FORBIDDEN:
  66. this.message = i18n.t("errors.forbidden");
  67. this.icon = "lock";
  68. break;
  69. case ERROR_TYPE.CONNECTION_ERROR:
  70. this.message = i18n.t("errors.connectionError");
  71. this.icon = "access-point-network-off";
  72. break;
  73. case ERROR_TYPE.SERVER_ERROR:
  74. this.message = i18n.t("errors.serverError");
  75. this.icon = "server-network-off";
  76. break;
  77. default:
  78. this.message = i18n.t("errors.unknown");
  79. this.icon = "alert-circle-outline";
  80. break;
  81. }
  82. this.message += "\n\nCode " + this.props.errorCode;
  83. } else {
  84. this.message = this.props.message;
  85. this.icon = this.props.icon;
  86. }
  87. }
  88. getRetryButton() {
  89. return <Button
  90. mode={'contained'}
  91. icon={'refresh'}
  92. onPress={this.props.onRefresh}
  93. style={styles.button}
  94. >
  95. {i18n.t("general.retry")}
  96. </Button>;
  97. }
  98. goToLogin = () => {
  99. this.props.navigation.navigate("login",
  100. {
  101. screen: 'login',
  102. params: {nextScreen: this.props.route.name}
  103. })
  104. };
  105. getLoginButton() {
  106. return <Button
  107. mode={'contained'}
  108. icon={'login'}
  109. onPress={this.goToLogin}
  110. style={styles.button}
  111. >
  112. {i18n.t("screens.login.title")}
  113. </Button>;
  114. }
  115. render() {
  116. this.generateMessage();
  117. return (
  118. <Animatable.View
  119. style={{
  120. ...styles.outer,
  121. backgroundColor: this.colors.background
  122. }}
  123. animation={"zoomIn"}
  124. duration={200}
  125. useNativeDriver
  126. >
  127. <View style={styles.inner}>
  128. <View style={styles.iconContainer}>
  129. <MaterialCommunityIcons
  130. name={this.icon}
  131. size={150}
  132. color={this.colors.textDisabled}/>
  133. </View>
  134. <Subheading style={{
  135. ...styles.subheading,
  136. color: this.colors.textDisabled
  137. }}>
  138. {this.message}
  139. </Subheading>
  140. {this.props.showRetryButton
  141. ? (this.showLoginButton
  142. ? this.getLoginButton()
  143. : this.getRetryButton())
  144. : null}
  145. </View>
  146. </Animatable.View>
  147. );
  148. }
  149. }
  150. const styles = StyleSheet.create({
  151. outer: {
  152. height: '100%',
  153. },
  154. inner: {
  155. marginTop: 'auto',
  156. marginBottom: 'auto',
  157. },
  158. iconContainer: {
  159. marginLeft: 'auto',
  160. marginRight: 'auto',
  161. marginBottom: 20
  162. },
  163. subheading: {
  164. textAlign: 'center',
  165. paddingHorizontal: 20
  166. },
  167. button: {
  168. marginTop: 10,
  169. marginLeft: 'auto',
  170. marginRight: 'auto',
  171. }
  172. });
  173. export default withTheme(ErrorView);