Skip to content

Commit

Permalink
navReducer: Use getNavigationRoutes selector, for simplicity.
Browse files Browse the repository at this point in the history
Also add a comment, with Greg's description [1] of what's going on here.

[1] zulip#4274 (comment)
  • Loading branch information
chrisbobbe committed Oct 13, 2020
1 parent 67ca157 commit 21c64e7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/nav/__tests__/navReducer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ import deepFreeze from 'deep-freeze';

import { INITIAL_FETCH_COMPLETE } from '../../actionConstants';
import navReducer, { getStateForRoute } from '../navReducer';
import NavigationService from '../NavigationService';

describe('navReducer', () => {
describe('INITIAL_FETCH_COMPLETE', () => {
test('do not mutate navigation state if already at the same route', () => {
NavigationService.getState = jest.fn().mockReturnValue(
deepFreeze({
index: 0,
routes: [{ routeName: 'main' }],
}),
);
const prevState = getStateForRoute('main');

const action = deepFreeze({
Expand Down
6 changes: 5 additions & 1 deletion src/nav/navReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import type { NavigationAction } from 'react-navigation';

import type { NavigationState, Action } from '../types';
import { getNavigationRoutes } from './navSelectors';
import AppNavigator from './AppNavigator';
import { INITIAL_FETCH_COMPLETE } from '../actionConstants';

Expand Down Expand Up @@ -29,7 +30,10 @@ export const initialState = getStateForRoute('loading');
export default (state: NavigationState = initialState, action: Action): NavigationState => {
switch (action.type) {
case INITIAL_FETCH_COMPLETE:
return state.routes[0].routeName === 'main' ? state : getStateForRoute('main');
// If we're anywhere in the normal UI of the app, then remain
// where we are. Only reset the nav state if we're elsewhere,
// and in that case, go to the main screen.
return getNavigationRoutes()[0].routeName === 'main' ? state : getStateForRoute('main');

default: {
// The `react-navigation` libdef says this only takes a NavigationAction,
Expand Down
2 changes: 1 addition & 1 deletion src/nav/navSelectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import NavigationService from './NavigationService';

export const getNavState = (): NavigationState => NavigationService.getState();

const getNavigationRoutes = () => getNavState().routes;
export const getNavigationRoutes = () => getNavState().routes;

const getNavigationIndex = () => getNavState().index;

Expand Down

0 comments on commit 21c64e7

Please sign in to comment.