Reference Source

src/components/views/authloading-view.js

  1. import React, { Component } from 'react';
  2. import { ActivityIndicator, StyleSheet, View, Alert, Image } from 'react-native';
  3.  
  4. import { styles, colours, authloading_styles } from './stylesheets/authloading-styles';
  5. let logo = require('../../assets/images/pplogo.png');
  6.  
  7. import Safearea from './helpers/safearea';
  8.  
  9. import BaseView from './view';
  10. import AuthLoadingPresenter from '../presenters/authloading-presenter';
  11.  
  12. /**
  13. * Class for the auth loading view to process the state before continuing.
  14. * @extends BaseView
  15. */
  16. class AuthLoadingView extends BaseView {
  17. state = {
  18. shouldLogout: false
  19. }
  20.  
  21. /**
  22. * Creates an instance of AuthLoadingView.
  23. *
  24. * @constructor
  25. * @param {Object} props - Component properties
  26. */
  27. constructor(props) {
  28. super(props);
  29. this.AuthLoadingP = new AuthLoadingPresenter(this);
  30. }
  31.  
  32. /**
  33. * Component is about to mount
  34. */
  35. componentWillMount = () => {
  36. const { navigation } = this.props;
  37. const shouldLogout = navigation.getParam('logout', false);
  38. this.AuthLoadingP.tryLogout(shouldLogout, this.onLogoutSuccess, this.onLogoutFailure);
  39. this.setState({shouldLogout});
  40. }
  41.  
  42. /**
  43. * Component mounted
  44. */
  45. componentDidMount = () => {
  46. if (!this.state.shouldLogout) {
  47. this.AuthLoadingP.checkAuthState(this.onAuthenticationSuccess, this.onAuthenticationFailure);
  48. }
  49. }
  50.  
  51. /**
  52. * Component will unmount after this method is called, do any clean up here.
  53. * Call viewUnmounting in base class so it can do any cleanup for the view before calling the presenter destroy method.
  54. */
  55. componentWillUnmount = () => {
  56. this.viewUnmounting(this.AuthLoadingP);
  57. }
  58.  
  59. /**
  60. * A function to call on the success of authentication verification.
  61. */
  62. onAuthenticationSuccess = () => {
  63. console.log('Authentication Success!');
  64. this.navigateToScreen('App');
  65. }
  66.  
  67. /**
  68. * A function to call on the failure of authentication verification.
  69. */
  70. onAuthenticationFailure = () => {
  71. console.log('Authentication Failure!');
  72. this.navigateToScreen('AuthStack');
  73. }
  74.  
  75. /**
  76. * A function to call on a successful logout.
  77. */
  78. onLogoutSuccess = () => {
  79. console.log('Logout Success');
  80. this.navigateToScreen('AuthStack');
  81. }
  82.  
  83. /**
  84. * A function to call on a failure to logout.
  85. */
  86. onLogoutFailure = () => {
  87. console.log('Logout Failure');
  88. Alert.alert(
  89. "Error: Unable to logout",
  90. "Please try again.",
  91. [
  92. { text: "Ok", style: "ok" },
  93. ],
  94. { cancelable: false },
  95. );
  96. this.navigateToScreen('App');
  97. }
  98.  
  99. /**
  100. * Navigate to a particular screen
  101. *
  102. * @param {string} screen - A screen to navigate to. See defined screens in navigation.js
  103. */
  104. navigateToScreen = (screen) => {
  105. const { navigate } = this.props.navigation;
  106. navigate(screen);
  107. }
  108.  
  109.  
  110. render() {
  111. return (
  112. <View style = {[styles.loading, authloading_styles.container]}>
  113. <Safearea overrideColour={colours.ppGreen}/>
  114. <Image source={logo} style={authloading_styles.image} resizeMode="contain" />
  115. <ActivityIndicator style={authloading_styles.load} size='small' color="#FFFFFF"/>
  116. </View>
  117.  
  118. );
  119. }
  120. }
  121.  
  122. export default AuthLoadingView;