Combine actions & reducers for me & auth & logoutLink to synchronize state update
Signed-off-by: Jared Scheib <jared.scheib@gmail.com>pull/10616/head
parent
195e8ed57d
commit
3721571361
|
@ -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('')
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue