/** Used for handling logging out a user **/
import React, { Component } from 'react';
import { AsyncStorage } from 'react-native';
import { NavigationActions } from 'react-navigation';
import { purgeStoredState } from 'redux-persist';
import firebase from 'firebase';
import { connect } from 'react-redux';
import { Spinner } from '../components/common';
import { profileListReset, propertiesReset, userFormReset } from '../actions';
import { strings } from '../locales/i18n';

class UserLogout extends Component {
  static navigationOptions = {
      title: strings('logout.title'),
  }

  /**
  * Component Will Mount
  **/
  async componentWillMount() {
    firebase.auth().signOut();
    this.props.userFormReset();
    this.props.propertiesReset();
    this.props.profileListReset();
    purgeStoredState({ storage: AsyncStorage });
    this.navigateToLoginScreen();
  }

  /**
  * Navigates to login screen and resets the navigation so no back option
  **/
  navigateToLoginScreen() {
    const resetAction = NavigationActions.reset({
      index: 0,
      actions: [
        NavigationActions.navigate({ routeName: 'userLogin' })
      ]
    });

    this.props.navigation.dispatch(resetAction);
  }

  /**
  * Render method
  **/
  render() {
    return (
      <Spinner />
    );
  }
}

export default connect(null, {
  profileListReset,
  propertiesReset,
  userFormReset
})(UserLogout);
