From 372157136111bb744b1b27a0bdbda84b9e05ddcd Mon Sep 17 00:00:00 2001 From: Luke Morris Date: Tue, 5 Dec 2017 15:37:39 -0800 Subject: [PATCH] Combine actions & reducers for me & auth & logoutLink to synchronize state update Signed-off-by: Jared Scheib --- ui/src/shared/actions/auth.js | 36 +++++++------------ ui/src/shared/reducers/auth.js | 63 ++++++++++++++++++++-------------- 2 files changed, 50 insertions(+), 49 deletions(-) diff --git a/ui/src/shared/actions/auth.js b/ui/src/shared/actions/auth.js index f3ee67b137..f2d3eee276 100644 --- a/ui/src/shared/actions/auth.js +++ b/ui/src/shared/actions/auth.js @@ -27,17 +27,12 @@ export const meGetRequested = () => ({ type: 'ME_GET_REQUESTED', }) -export const meGetCompletedNotUsingAuth = me => ({ - type: 'ME_GET_COMPLETED__NON_AUTH', - payload: { - me, - }, -}) - -export const meGetCompletedUsingAuth = me => ({ - type: 'ME_GET_COMPLETED__AUTH', +export const meGetCompleted = ({me, auth, logoutLink}) => ({ + type: 'ME_GET_COMPLETED', payload: { me, + auth, + logoutLink, }, }) @@ -57,13 +52,6 @@ export const meChangeOrganizationFailed = () => ({ type: 'ME_CHANGE_ORGANIZATION_FAILED', }) -export const logoutLinkReceived = logoutLink => ({ - type: 'LOGOUT_LINK_RECEIVED', - payload: { - logoutLink, - }, -}) - // shouldResetMe protects against `me` being nullified in Redux temporarily, // which currently causes the app to show a loading spinner until me is // re-hydrated. if `getMeAsync` is only being used to refresh me after creating @@ -84,13 +72,15 @@ export const getMeAsync = ({shouldResetMe = false} = {}) => async dispatch => { organizations, meLink, } = await getMeAJAX() - const isUsingAuth = !!logoutLink - dispatch( - isUsingAuth ? meGetCompletedUsingAuth(me) : meGetCompletedNotUsingAuth(me) - ) - dispatch(authReceived(auth)) - dispatch(logoutLinkReceived(logoutLink)) + dispatch(linksReceived({external, users, organizations, me: meLink})) + dispatch( + meGetCompleted({ + me, + auth, + logoutLink, + }) + ) } catch (error) { dispatch(errorThrown(error)) dispatch(meGetFailed()) @@ -111,7 +101,7 @@ export const meChangeOrganizationAsync = ( ) ) dispatch(meChangeOrganizationCompleted()) - dispatch(meGetCompletedUsingAuth(data)) + dispatch(meGetCompleted(data)) // TODO: reload sources upon me change org if non-refresh behavior preferred // instead of current behavior on both invocations of meChangeOrganization, // which is to refresh index via router.push('') diff --git a/ui/src/shared/reducers/auth.js b/ui/src/shared/reducers/auth.js index f11948b8ff..4bb1281181 100644 --- a/ui/src/shared/reducers/auth.js +++ b/ui/src/shared/reducers/auth.js @@ -10,6 +10,36 @@ import {getMeRole} from 'shared/reducers/helpers/auth' export const initialState = getInitialState() +const meGetCompleted = (state, {me}, isUsingAuth) => { + let newMe = me + + if (isUsingAuth) { + newMe = { + ...newMe, + role: getMeRole(me), + currentOrganization: me.currentOrganization, + } + } + + return { + ...state, + me: {...newMe}, + isMeLoading: false, + } +} + +const authReceived = (state, {auth: {links}}) => ({ + ...state, + links, + isAuthLoading: false, +}) + +const logoutLinkReceived = (state, {logoutLink}, isUsingAuth) => ({ + ...state, + logoutLink, + isUsingAuth, +}) + const authReducer = (state = initialState, action) => { switch (action.type) { case 'AUTH_EXPIRED': { @@ -19,37 +49,18 @@ const authReducer = (state = initialState, action) => { case 'AUTH_REQUESTED': { return {...state, isAuthLoading: true} } - case 'AUTH_RECEIVED': { - const {auth: {links}} = action.payload - return {...state, links, isAuthLoading: false} - } case 'ME_GET_REQUESTED': { return {...state, isMeLoading: true} } - case 'ME_GET_COMPLETED__NON_AUTH': { - const {me} = action.payload - return { - ...state, - me: {...me}, - isMeLoading: false, - } - } - case 'ME_GET_COMPLETED__AUTH': { - const {me, me: {currentOrganization}} = action.payload - return { - ...state, - me: { - ...me, - role: getMeRole(me), - currentOrganization, - }, - isMeLoading: false, - } - } - case 'LOGOUT_LINK_RECEIVED': { + case 'ME_GET_COMPLETED': { const {logoutLink} = action.payload const isUsingAuth = !!logoutLink - return {...state, logoutLink, isUsingAuth} + + let newState = meGetCompleted(state, action.payload, isUsingAuth) + newState = authReceived(newState, action.payload) + newState = logoutLinkReceived(newState, action.payload, isUsingAuth) + + return newState } }