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.tsx 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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, useTheme } from 'react-native-paper';
  21. import { StyleSheet, View, ViewStyle } 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 { REQUEST_CODES, REQUEST_STATUS } from '../../utils/Requests';
  26. type Props = {
  27. status?: REQUEST_STATUS;
  28. code?: REQUEST_CODES;
  29. icon?: string;
  30. message?: string;
  31. loading?: boolean;
  32. button?: {
  33. text: string;
  34. icon: string;
  35. onPress: () => void;
  36. };
  37. style?: ViewStyle;
  38. };
  39. const styles = StyleSheet.create({
  40. outer: {
  41. flex: 1,
  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. function getMessage(props: Props) {
  63. let fullMessage = {
  64. message: '',
  65. icon: '',
  66. };
  67. if (props.code === undefined) {
  68. switch (props.status) {
  69. case REQUEST_STATUS.BAD_INPUT:
  70. fullMessage.message = i18n.t('errors.badInput');
  71. fullMessage.icon = 'alert-circle-outline';
  72. break;
  73. case REQUEST_STATUS.FORBIDDEN:
  74. fullMessage.message = i18n.t('errors.forbidden');
  75. fullMessage.icon = 'lock';
  76. break;
  77. case REQUEST_STATUS.CONNECTION_ERROR:
  78. fullMessage.message = i18n.t('errors.connectionError');
  79. fullMessage.icon = 'access-point-network-off';
  80. break;
  81. case REQUEST_STATUS.SERVER_ERROR:
  82. fullMessage.message = i18n.t('errors.serverError');
  83. fullMessage.icon = 'server-network-off';
  84. break;
  85. default:
  86. fullMessage.message = i18n.t('errors.unknown');
  87. fullMessage.icon = 'alert-circle-outline';
  88. break;
  89. }
  90. } else {
  91. switch (props.code) {
  92. case REQUEST_CODES.BAD_CREDENTIALS:
  93. fullMessage.message = i18n.t('errors.badCredentials');
  94. fullMessage.icon = 'account-alert-outline';
  95. break;
  96. case REQUEST_CODES.BAD_TOKEN:
  97. fullMessage.message = i18n.t('errors.badToken');
  98. fullMessage.icon = 'account-alert-outline';
  99. break;
  100. case REQUEST_CODES.NO_CONSENT:
  101. fullMessage.message = i18n.t('errors.noConsent');
  102. fullMessage.icon = 'account-remove-outline';
  103. break;
  104. case REQUEST_CODES.TOKEN_SAVE:
  105. fullMessage.message = i18n.t('errors.tokenSave');
  106. fullMessage.icon = 'alert-circle-outline';
  107. break;
  108. case REQUEST_CODES.BAD_INPUT:
  109. fullMessage.message = i18n.t('errors.badInput');
  110. fullMessage.icon = 'alert-circle-outline';
  111. break;
  112. case REQUEST_CODES.FORBIDDEN:
  113. fullMessage.message = i18n.t('errors.forbidden');
  114. fullMessage.icon = 'lock';
  115. break;
  116. case REQUEST_CODES.CONNECTION_ERROR:
  117. fullMessage.message = i18n.t('errors.connectionError');
  118. fullMessage.icon = 'access-point-network-off';
  119. break;
  120. case REQUEST_CODES.SERVER_ERROR:
  121. fullMessage.message = i18n.t('errors.serverError');
  122. fullMessage.icon = 'server-network-off';
  123. break;
  124. default:
  125. fullMessage.message = i18n.t('errors.unknown');
  126. fullMessage.icon = 'alert-circle-outline';
  127. break;
  128. }
  129. }
  130. if (props.code !== undefined) {
  131. fullMessage.message += `\n\nCode {${props.status}:${props.code}}`;
  132. } else {
  133. fullMessage.message += `\n\nCode {${props.status}}`;
  134. }
  135. if (props.message != null) {
  136. fullMessage.message = props.message;
  137. }
  138. if (props.icon != null) {
  139. fullMessage.icon = props.icon;
  140. }
  141. return fullMessage;
  142. }
  143. function ErrorView(props: Props) {
  144. const theme = useTheme();
  145. const fullMessage = getMessage(props);
  146. const { button } = props;
  147. return (
  148. <View style={{ ...styles.outer, ...props.style }}>
  149. <Animatable.View
  150. style={{
  151. ...styles.outer,
  152. backgroundColor: theme.colors.background,
  153. }}
  154. animation="zoomIn"
  155. duration={200}
  156. useNativeDriver
  157. >
  158. <View style={styles.inner}>
  159. <View style={styles.iconContainer}>
  160. <MaterialCommunityIcons
  161. name={fullMessage.icon}
  162. size={150}
  163. color={theme.colors.disabled}
  164. />
  165. </View>
  166. <Subheading
  167. style={{
  168. ...styles.subheading,
  169. color: theme.colors.disabled,
  170. }}
  171. >
  172. {fullMessage.message}
  173. </Subheading>
  174. {button ? (
  175. <Button
  176. mode={'contained'}
  177. icon={button.icon}
  178. onPress={button.onPress}
  179. style={styles.button}
  180. >
  181. {button.text}
  182. </Button>
  183. ) : null}
  184. </View>
  185. </Animatable.View>
  186. </View>
  187. );
  188. }
  189. export default ErrorView;