Skip to content

Commit

Permalink
navReducer: Transplant away LOGIN_SUCCESS logic.
Browse files Browse the repository at this point in the history
Soon, we'll dismantle `navReducer`, as its purpose is incompatible
with a design where we manage navigation state separately from our
Redux-stored data (zulip#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.
  • Loading branch information
chrisbobbe committed Oct 20, 2020
1 parent 48ae49c commit 2075651
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 29 deletions.
10 changes: 9 additions & 1 deletion src/account/accountActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down
25 changes: 1 addition & 24 deletions src/nav/__tests__/navReducer-test.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
5 changes: 1 addition & 4 deletions src/nav/navReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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');

Expand Down

0 comments on commit 2075651

Please sign in to comment.