From c00b9e6a061be25a4673ea564b0133a2e80ad329 Mon Sep 17 00:00:00 2001 From: Chris Bobbe Date: Thu, 20 Aug 2020 17:43:25 -0700 Subject: [PATCH] redux: In REALM_ADD and LOGIN_SUCCESS, make `realm` be a URL object. As in the preceding handful of commits, stringify the realm exactly where we need it as a string. These should be the last sites in which it's not totally clear that the realm is well-formed data. --- src/__tests__/lib/exampleData.js | 2 +- src/account/__tests__/accountsReducer-test.js | 10 +++++----- src/account/accountActions.js | 4 ++-- src/account/accountsReducer.js | 13 ++++++------- src/actionTypes.js | 4 ++-- src/start/AuthScreen.js | 2 +- src/start/DevAuthScreen.js | 2 +- src/start/PasswordAuthScreen.js | 2 +- src/start/RealmScreen.js | 2 +- 9 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/__tests__/lib/exampleData.js b/src/__tests__/lib/exampleData.js index 8a8fa34d5db..27f907b8ec3 100644 --- a/src/__tests__/lib/exampleData.js +++ b/src/__tests__/lib/exampleData.js @@ -418,7 +418,7 @@ export const action = deepFreeze({ }, login_success: { type: LOGIN_SUCCESS, - realm: selfAccount.realm.toString(), + realm: selfAccount.realm, email: selfAccount.email, apiKey: selfAccount.apiKey, }, diff --git a/src/account/__tests__/accountsReducer-test.js b/src/account/__tests__/accountsReducer-test.js index ec35d7fdf49..75b111aa14a 100644 --- a/src/account/__tests__/accountsReducer-test.js +++ b/src/account/__tests__/accountsReducer-test.js @@ -36,7 +36,7 @@ describe('accountsReducer', () => { const action = deepFreeze({ type: REALM_ADD, - realm: newAccount.realm.toString(), + realm: newAccount.realm, zulipVersion: eg.zulipVersion, }); @@ -58,7 +58,7 @@ describe('accountsReducer', () => { const action = deepFreeze({ type: REALM_ADD, - realm: newAccount.realm.toString(), + realm: newAccount.realm, zulipVersion: eg.zulipVersion, }); @@ -139,7 +139,7 @@ describe('accountsReducer', () => { type: LOGIN_SUCCESS, apiKey: newAccount.apiKey, email: newAccount.email, - realm: newAccount.realm.toString(), + realm: newAccount.realm, }); const expectedState = [{ ...newAccount, zulipVersion: account1.zulipVersion }, account2]; @@ -160,7 +160,7 @@ describe('accountsReducer', () => { type: LOGIN_SUCCESS, apiKey: newAccount.apiKey, email: newAccount.email, - realm: newAccount.realm.toString(), + realm: newAccount.realm, }); const expectedState = [newAccount, account1, account2]; @@ -179,7 +179,7 @@ describe('accountsReducer', () => { const action = deepFreeze({ type: LOGIN_SUCCESS, apiKey: newAccount.apiKey, - realm: newAccount.realm.toString(), + realm: newAccount.realm, email: newAccount.email, }); diff --git a/src/account/accountActions.js b/src/account/accountActions.js index 4ba460eacf2..bc39c07f144 100644 --- a/src/account/accountActions.js +++ b/src/account/accountActions.js @@ -14,7 +14,7 @@ export const switchAccount = (index: number): Action => ({ index, }); -export const realmAdd = (realm: string, zulipVersion: ZulipVersion): Action => ({ +export const realmAdd = (realm: URL, zulipVersion: ZulipVersion): Action => ({ type: REALM_ADD, realm, zulipVersion, @@ -25,7 +25,7 @@ export const removeAccount = (index: number): Action => ({ index, }); -export const loginSuccess = (realm: string, email: string, apiKey: string): Action => ({ +export const loginSuccess = (realm: URL, email: string, apiKey: string): Action => ({ type: LOGIN_SUCCESS, realm, email, diff --git a/src/account/accountsReducer.js b/src/account/accountsReducer.js index c70efad3ae1..7f6fbb051f3 100644 --- a/src/account/accountsReducer.js +++ b/src/account/accountsReducer.js @@ -16,7 +16,9 @@ import { NULL_ARRAY } from '../nullObjects'; const initialState = NULL_ARRAY; const realmAdd = (state, action) => { - const accountIndex = state.findIndex(account => account.realm.toString() === action.realm); + const accountIndex = state.findIndex( + account => account.realm.toString() === action.realm.toString(), + ); if (accountIndex !== -1) { const newAccount = { @@ -28,7 +30,7 @@ const realmAdd = (state, action) => { return [ { - realm: new URL(action.realm), + realm: action.realm, apiKey: '', email: '', ackedPushToken: null, @@ -64,12 +66,9 @@ const findAccount = (state: AccountsState, identity: Identity): number => { const loginSuccess = (state, action) => { const { realm, email, apiKey } = action; - const accountIndex = findAccount(state, { realm: new URL(realm), email }); + const accountIndex = findAccount(state, { realm, email }); if (accountIndex === -1) { - return [ - { realm: new URL(realm), email, apiKey, ackedPushToken: null, zulipVersion: null }, - ...state, - ]; + return [{ realm, email, apiKey, ackedPushToken: null, zulipVersion: null }, ...state]; } return [ { ...state[accountIndex], email, apiKey, ackedPushToken: null }, diff --git a/src/actionTypes.js b/src/actionTypes.js index ef4e707859b..4840a039663 100644 --- a/src/actionTypes.js +++ b/src/actionTypes.js @@ -142,7 +142,7 @@ type AccountSwitchAction = {| type RealmAddAction = {| type: typeof REALM_ADD, - realm: string, + realm: URL, zulipVersion: ZulipVersion, |}; @@ -153,7 +153,7 @@ type AccountRemoveAction = {| type LoginSuccessAction = {| type: typeof LOGIN_SUCCESS, - realm: string, + realm: URL, email: string, apiKey: string, |}; diff --git a/src/start/AuthScreen.js b/src/start/AuthScreen.js index 985f182ba50..a8a4a718d7a 100644 --- a/src/start/AuthScreen.js +++ b/src/start/AuthScreen.js @@ -226,7 +226,7 @@ class AuthScreen extends PureComponent { const { dispatch, realm } = this.props; const auth = webAuth.authFromCallbackUrl(event.url, otp, realm); if (auth) { - dispatch(loginSuccess(auth.realm.toString(), auth.email, auth.apiKey)); + dispatch(loginSuccess(auth.realm, auth.email, auth.apiKey)); } }; diff --git a/src/start/DevAuthScreen.js b/src/start/DevAuthScreen.js index 3b2cf1fd50d..10259c570a8 100644 --- a/src/start/DevAuthScreen.js +++ b/src/start/DevAuthScreen.js @@ -73,7 +73,7 @@ class DevAuthScreen extends PureComponent { try { const { api_key } = await api.devFetchApiKey(partialAuth, email); - this.props.dispatch(loginSuccess(partialAuth.realm.toString(), email, api_key)); + this.props.dispatch(loginSuccess(partialAuth.realm, email, api_key)); this.setState({ progress: false }); } catch (err) { this.setState({ progress: false, error: err.data && err.data.msg }); diff --git a/src/start/PasswordAuthScreen.js b/src/start/PasswordAuthScreen.js index 6d90687f27d..21a4c7d4b52 100644 --- a/src/start/PasswordAuthScreen.js +++ b/src/start/PasswordAuthScreen.js @@ -57,7 +57,7 @@ class PasswordAuthScreen extends PureComponent { try { const fetchedKey = await api.fetchApiKey(partialAuth, email, password); this.setState({ progress: false }); - dispatch(loginSuccess(partialAuth.realm.toString(), fetchedKey.email, fetchedKey.api_key)); + dispatch(loginSuccess(partialAuth.realm, fetchedKey.email, fetchedKey.api_key)); } catch (err) { this.setState({ progress: false, diff --git a/src/start/RealmScreen.js b/src/start/RealmScreen.js index 3f08aefcd50..fa0013f6f2f 100644 --- a/src/start/RealmScreen.js +++ b/src/start/RealmScreen.js @@ -62,7 +62,7 @@ class RealmScreen extends PureComponent { }); try { const serverSettings: ApiResponseServerSettings = await api.getServerSettings(parsedRealm); - dispatch(realmAdd(realmInputValue, new ZulipVersion(serverSettings.zulip_version))); + dispatch(realmAdd(parsedRealm, new ZulipVersion(serverSettings.zulip_version))); dispatch(navigateToAuth(serverSettings)); Keyboard.dismiss(); } catch (err) {