diff --git a/lib/manager/actions/user.js b/lib/manager/actions/user.js index dab9e7baf..66b0a67d6 100644 --- a/lib/manager/actions/user.js +++ b/lib/manager/actions/user.js @@ -4,6 +4,7 @@ import objectPath from 'object-path' import {browserHistory} from 'react-router' import {createAction, type ActionType} from 'redux-actions' +import {fetchUsers} from '../../admin/actions/admin' import {createVoidPayloadAction, secureFetch} from '../../common/actions' import auth0 from '../../common/user/Auth0Manager' import UserPermissions from '../../common/user/UserPermissions' @@ -222,7 +223,10 @@ export function updateTargetForSubscription (profile: UserProfile, target: strin } /** - * Update application/client ID specific datatools object with provided user data. + * Update application/client ID specific datatools object with provided user + * data. This is used in the user admin module to update some user's permissions + * as well as throughout the application to update attributes for the logged in + * user (e.g., when a user subscribes/watches a feed source or project). */ export function updateUserData (user: any, userData: any) { return function (dispatch: dispatchFn, getState: getStateFn) { @@ -242,20 +246,32 @@ export function updateUserData (user: any, userData: any) { appMetadata.datatools[dtIndex][key] = userData[key] } const url = `/api/manager/secure/user/${user.user_id}` + let status // Make request to server return dispatch(secureFetch(url, 'put', { user_id: user.user_id, data: appMetadata.datatools })) - .then(response => response.json()) - .then(updatedProfile => { + .then(response => { + status = response.status + return response.json() + }) + .then(responseJson => { + if (status >= 400) { + return dispatch(setErrorMessage({ + message: `Could not update user metadata\n${JSON.stringify(responseJson)}` + })) + } const {profile} = getState().user if (!profile) throw new Error('Could not find user profile in state.') - const currentUserId = profile.user_id - if (updatedProfile.user_id === currentUserId) { + if (responseJson.user_id === profile.user_id) { // If user being updated matches the logged in user, update their - // profile in the application state. - dispatch(userProfileUpdated(updatedProfile)) + // profile in the application state as the change in user_metdata can + // change various items in the application such as user subscriptions + // or even permissions a user has available to them. + dispatch(userProfileUpdated(responseJson)) + } else { + dispatch(fetchUsers()) } }) }