import React, {useEffect} from 'react';
import NavigationBar from './tabs';
import Onboarding from './onboarding';
import {useSelector, useDispatch} from 'react-redux';
import {RootState} from '../store/rootReducer';
import {userLogin} from '../utils';

const Routes: React.FC = () => {
  const {
    user: {userId},
  } = useSelector((state: RootState) => state.user);
  const dispatch = useDispatch();

  /**
   * Load the user from AsyncStorage if any
   * Note that this makes logout triggered by invalid Token have no effect.
   * We should figure out a way to handle that.
   * Suggestions?
   * NOTE : Not something introduced by this commit but something we already have.
   */
  useEffect(() => {
    if (!userId) {
      userLogin(dispatch, {userId: '', username: ''});
    }
  }, [userId, userLogin]);

  return userId ? <NavigationBar /> : <Onboarding />;
};

export default Routes;
