Skip to content

Commit 785232e

Browse files
committed
navReducer: Better decide if we have server data in rehydrate.
As hinted by the comment above this edit, even an empty array is incomplete server data: "Valid server data must have a user: the self user, at a minimum.". But the initial state of `users` (`initialState` in src/users/usersReducer.js) is an empty array. LOGOUT, LOGIN_SUCCESS, and ACCOUNT_SWITCH all set `users` to that initial state. Those three actions are handled in `navReducer` by navigating to routes that don't depend on complete server data: 'loading' or 'account'. But if we quit the app with `users` having this initial state (e.g., giving up in frustration while trying to connect to a realm for a long time), we should be sure that, on reopening it and rehydrating, we don't try to go to 'main' before that initial state has been replaced with a complete state. Otherwise, we see "No server data found" errors and a white screen. See also discussion [1]. [1]: https://chat.zulip.org/#narrow/stream/243-mobile-team/topic/.23M4166.20Time.20out.20initial.20fetch/near/912017
1 parent 3939420 commit 785232e

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/nav/__tests__/navReducer-test.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ describe('navReducer', () => {
8585
expect(nav.routes).toHaveLength(1);
8686
});
8787

88-
test('if logged in, go to main screen', () => {
88+
test('if logged in and users is empty, go to loading', () => {
8989
const initialState = NULL_OBJECT;
9090

9191
const action = deepFreeze({
@@ -99,6 +99,24 @@ describe('navReducer', () => {
9999

100100
const nav = navReducer(initialState, action);
101101

102+
expect(nav.routes).toHaveLength(1);
103+
expect(nav.routes[0].routeName).toEqual('loading');
104+
});
105+
106+
test('if logged in and users is not empty, go to main', () => {
107+
const initialState = NULL_OBJECT;
108+
109+
const action = deepFreeze({
110+
type: REHYDRATE,
111+
payload: {
112+
accounts: [{ apiKey: '123' }],
113+
users: [{ user_id: 123 }],
114+
realm: {},
115+
},
116+
});
117+
118+
const nav = navReducer(initialState, action);
119+
102120
expect(nav.routes).toHaveLength(1);
103121
expect(nav.routes[0].routeName).toEqual('main');
104122
});

src/nav/navReducer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const rehydrate = (state, action) => {
6161
// will have set `needsInitialFetch`, too, so we really will be loading.
6262
//
6363
// (Valid server data must have a user: the self user, at a minimum.)
64-
if (rehydratedState.users === undefined) {
64+
if (rehydratedState.users === undefined || rehydratedState.users.length === 0) {
6565
return getStateForRoute('loading');
6666
}
6767

0 commit comments

Comments
 (0)