From 67ca1571e494775fb8a7e64141f57d15cd1fdce0 Mon Sep 17 00:00:00 2001 From: Chris Bobbe Date: Mon, 5 Oct 2020 17:12:52 -0700 Subject: [PATCH] navReducer: Transplant away `LOGIN_SUCCESS` logic. Soon, we'll dismantle `navReducer`, as its purpose is incompatible with a design where we manage navigation state separately from our Redux-stored data (#3804). So, convert the `navReducer`'s `LOGIN_SUCCESS` case into instructions to navigate to the loading screen. Make the action creator into a thunk action creator and dispatch the navigation action from there, to reduce repetition. --- src/account/accountActions.js | 10 +++++++++- src/nav/__tests__/navReducer-test.js | 25 +------------------------ src/nav/navReducer.js | 5 +---- src/start/DevAuthScreen.js | 4 ++-- 4 files changed, 13 insertions(+), 31 deletions(-) diff --git a/src/account/accountActions.js b/src/account/accountActions.js index 7452e045a37..52fbcf057d6 100644 --- a/src/account/accountActions.js +++ b/src/account/accountActions.js @@ -36,13 +36,21 @@ export const removeAccount = (index: number): Action => ({ index, }); -export const loginSuccess = (realm: URL, email: string, apiKey: string): Action => ({ +const loginSuccessPlain = (realm: URL, email: string, apiKey: string): Action => ({ type: LOGIN_SUCCESS, realm, email, apiKey, }); +export const loginSuccess = (realm: URL, email: string, apiKey: string) => ( + dispatch: Dispatch, + getState: GetState, +) => { + dispatch(resetToLoading()); + dispatch(loginSuccessPlain(realm, email, apiKey)); +}; + const logoutPlain = (): Action => ({ type: LOGOUT, }); diff --git a/src/nav/__tests__/navReducer-test.js b/src/nav/__tests__/navReducer-test.js index ffde22bb222..2b468f38cdb 100644 --- a/src/nav/__tests__/navReducer-test.js +++ b/src/nav/__tests__/navReducer-test.js @@ -1,32 +1,9 @@ import deepFreeze from 'deep-freeze'; -import { LOGIN_SUCCESS, INITIAL_FETCH_COMPLETE } from '../../actionConstants'; +import { INITIAL_FETCH_COMPLETE } from '../../actionConstants'; import navReducer, { getStateForRoute } from '../navReducer'; describe('navReducer', () => { - describe('LOGIN_SUCCESS', () => { - test('replaces the existing route stack with "loading" on sign in', () => { - const prevState = deepFreeze({ - index: 2, - routes: [{ key: 'one' }, { key: 'two' }, { key: 'password' }], - }); - - const action = deepFreeze({ - type: LOGIN_SUCCESS, - }); - - const expectedState = { - index: 0, - routes: [{ routeName: 'loading' }], - }; - - const newState = navReducer(prevState, action); - - expect(newState.index).toEqual(expectedState.index); - expect(newState.routes[0].routeName).toEqual(expectedState.routes[0].routeName); - }); - }); - describe('INITIAL_FETCH_COMPLETE', () => { test('do not mutate navigation state if already at the same route', () => { const prevState = getStateForRoute('main'); diff --git a/src/nav/navReducer.js b/src/nav/navReducer.js index e7e67082a49..5375e10bcc4 100644 --- a/src/nav/navReducer.js +++ b/src/nav/navReducer.js @@ -3,7 +3,7 @@ import type { NavigationAction } from 'react-navigation'; import type { NavigationState, Action } from '../types'; import AppNavigator from './AppNavigator'; -import { INITIAL_FETCH_COMPLETE, LOGIN_SUCCESS } from '../actionConstants'; +import { INITIAL_FETCH_COMPLETE } from '../actionConstants'; /** * Get the initial state for the given route. @@ -28,9 +28,6 @@ export const initialState = getStateForRoute('loading'); export default (state: NavigationState = initialState, action: Action): NavigationState => { switch (action.type) { - case LOGIN_SUCCESS: - return getStateForRoute('loading'); - case INITIAL_FETCH_COMPLETE: return state.routes[0].routeName === 'main' ? state : getStateForRoute('main'); diff --git a/src/start/DevAuthScreen.js b/src/start/DevAuthScreen.js index 5ee27a46740..34f7ac7ac69 100644 --- a/src/start/DevAuthScreen.js +++ b/src/start/DevAuthScreen.js @@ -74,13 +74,13 @@ class DevAuthScreen extends PureComponent { }; tryDevLogin = async (email: string) => { - const { partialAuth } = this.props; + const { partialAuth, dispatch } = this.props; this.setState({ progress: true, error: undefined }); try { const { api_key } = await api.devFetchApiKey(partialAuth, email); - this.props.dispatch(loginSuccess(partialAuth.realm, email, api_key)); + dispatch(loginSuccess(partialAuth.realm, email, api_key)); this.setState({ progress: false }); } catch (err) { this.setState({ progress: false, error: err.data && err.data.msg });