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.

AuthenticatedScreen.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // @flow
  2. import * as React from 'react';
  3. import {withTheme} from 'react-native-paper';
  4. import ConnectionManager, {ERROR_TYPE} from "../../managers/ConnectionManager";
  5. import NetworkErrorComponent from "../Custom/NetworkErrorComponent";
  6. import i18n from 'i18n-js';
  7. import BasicLoadingScreen from "../Custom/BasicLoadingScreen";
  8. type Props = {
  9. navigation: Object,
  10. theme: Object,
  11. link: string,
  12. renderFunction: Function,
  13. }
  14. type State = {
  15. loading: boolean,
  16. }
  17. class AuthenticatedScreen extends React.Component<Props, State> {
  18. state = {
  19. loading: true,
  20. };
  21. currentUserToken: string | null;
  22. connectionManager: ConnectionManager;
  23. errorCode: number;
  24. data: Object;
  25. colors: Object;
  26. constructor(props) {
  27. super(props);
  28. this.colors = props.theme.colors;
  29. this.connectionManager = ConnectionManager.getInstance();
  30. this.props.navigation.addListener('focus', this.onScreenFocus.bind(this));
  31. }
  32. onScreenFocus() {
  33. if (this.currentUserToken !== this.connectionManager.getToken())
  34. this.fetchData();
  35. }
  36. fetchData = () => {
  37. if (!this.state.loading)
  38. this.setState({loading: true});
  39. if (this.connectionManager.isLoggedIn()) {
  40. this.connectionManager.authenticatedRequest(this.props.link)
  41. .then((data) => {
  42. this.onFinishedLoading(data, -1);
  43. })
  44. .catch((error) => {
  45. this.onFinishedLoading(undefined, error);
  46. });
  47. } else {
  48. this.onFinishedLoading(undefined, ERROR_TYPE.BAD_CREDENTIALS);
  49. }
  50. };
  51. onFinishedLoading(data: Object, error: number) {
  52. this.data = data;
  53. this.currentUserToken = data !== undefined
  54. ? this.connectionManager.getToken()
  55. : null;
  56. this.errorCode = error;
  57. this.setState({loading: false});
  58. }
  59. getErrorRender() {
  60. let message;
  61. let icon;
  62. switch (this.errorCode) {
  63. case ERROR_TYPE.BAD_CREDENTIALS:
  64. message = i18n.t("loginScreen.errors.credentials");
  65. icon = "account-alert-outline";
  66. break;
  67. case ERROR_TYPE.CONNECTION_ERROR:
  68. message = i18n.t("loginScreen.errors.connection");
  69. icon = "access-point-network-off";
  70. break;
  71. default:
  72. message = i18n.t("loginScreen.errors.unknown");
  73. icon = "alert-circle-outline";
  74. break;
  75. }
  76. return (
  77. <NetworkErrorComponent
  78. {...this.props}
  79. icon={icon}
  80. message={message}
  81. onRefresh={this.fetchData}
  82. />
  83. );
  84. }
  85. render() {
  86. return (
  87. this.state.loading
  88. ? <BasicLoadingScreen/>
  89. : (this.data !== undefined
  90. ? this.props.renderFunction(this.data)
  91. : this.getErrorRender())
  92. );
  93. }
  94. }
  95. export default withTheme(AuthenticatedScreen);