Fix update dropdown when adding users, roles, and permissions (except users/roles), rename success actions for use across AJAX update requests, pass through error on AJAX update failure

pull/10616/head
Jared Scheib 2017-03-10 16:22:03 -08:00
parent 5f851863a3
commit 410d0beb56
4 changed files with 33 additions and 27 deletions

View File

@ -2,7 +2,7 @@ import reducer from 'src/admin/reducers/admin'
import { import {
addUser, addUser,
createUserSuccess, syncUser,
editUser, editUser,
loadRoles, loadRoles,
loadPermissions, loadPermissions,
@ -66,8 +66,8 @@ describe('Admin.Reducers', () => {
expect(actual.users).to.deep.equal(expected.users) expect(actual.users).to.deep.equal(expected.users)
}) })
it('it can confirm a created user', () => { it('it can sync a stale user', () => {
const addedUser = { const staleUser = {
name: 'acidburn', name: 'acidburn',
password: 'pass1', password: 'pass1',
roles: [], roles: [],
@ -75,9 +75,9 @@ describe('Admin.Reducers', () => {
links: {self: ''}, links: {self: ''},
isNew: true, isNew: true,
} }
state = {users: [u2, addedUser]} state = {users: [u2, staleUser]}
const actual = reducer(state, createUserSuccess(addedUser, u1)) const actual = reducer(state, syncUser(staleUser, u1))
const expected = { const expected = {
users: [u2, u1], users: [u2, u1],
} }

View File

@ -43,19 +43,19 @@ export const addRole = () => ({
type: 'ADD_ROLE', type: 'ADD_ROLE',
}) })
export const createUserSuccess = (user, createdUser) => ({ export const syncUser = (staleUser, syncedUser) => ({
type: 'CREATE_USER_SUCCESS', type: 'SYNC_USER',
payload: { payload: {
user, staleUser,
createdUser, syncedUser,
}, },
}) })
export const createRoleSuccess = (role, createdRole) => ({ export const syncRole = (staleRole, syncedRole) => ({
type: 'CREATE_ROLE_SUCCESS', type: 'SYNC_ROLE',
payload: { payload: {
role, staleRole,
createdRole, syncedRole,
}, },
}) })
@ -144,7 +144,7 @@ export const createUserAsync = (url, user) => async (dispatch) => {
try { try {
const {data} = await createUserAJAX(url, user) const {data} = await createUserAJAX(url, user)
dispatch(publishNotification('success', 'User created successfully')) dispatch(publishNotification('success', 'User created successfully'))
dispatch(createUserSuccess(user, data)) dispatch(syncUser(user, data))
} catch (error) { } catch (error) {
// undo optimistic update // undo optimistic update
dispatch(publishNotification('error', `Failed to create user: ${error.data.message}`)) dispatch(publishNotification('error', `Failed to create user: ${error.data.message}`))
@ -156,7 +156,7 @@ export const createRoleAsync = (url, role) => async (dispatch) => {
try { try {
const {data} = await createRoleAJAX(url, role) const {data} = await createRoleAJAX(url, role)
dispatch(publishNotification('success', 'Role created successfully')) dispatch(publishNotification('success', 'Role created successfully'))
dispatch(createRoleSuccess(role, data)) dispatch(syncRole(role, data))
} catch (error) { } catch (error) {
// undo optimistic update // undo optimistic update
dispatch(publishNotification('error', `Failed to create role: ${error.data.message}`)) dispatch(publishNotification('error', `Failed to create role: ${error.data.message}`))
@ -191,8 +191,9 @@ export const deleteUserAsync = (user, addFlashMessage) => (dispatch) => {
export const updateRoleUsersAsync = (role, users) => async (dispatch) => { export const updateRoleUsersAsync = (role, users) => async (dispatch) => {
try { try {
await updateRoleAJAX(role.links.self, users, role.permissions) const {data} = await updateRoleAJAX(role.links.self, users, role.permissions)
dispatch(publishNotification('success', 'Role users updated')) dispatch(publishNotification('success', 'Role users updated'))
dispatch(syncRole(role, data))
} catch (error) { } catch (error) {
dispatch(publishNotification('error', `Failed to update role: ${error.data.message}`)) dispatch(publishNotification('error', `Failed to update role: ${error.data.message}`))
} }
@ -200,8 +201,9 @@ export const updateRoleUsersAsync = (role, users) => async (dispatch) => {
export const updateRolePermissionsAsync = (role, permissions) => async (dispatch) => { export const updateRolePermissionsAsync = (role, permissions) => async (dispatch) => {
try { try {
await updateRoleAJAX(role.links.self, role.users, permissions) const {data} = await updateRoleAJAX(role.links.self, role.users, permissions)
dispatch(publishNotification('success', 'Role permissions updated')) dispatch(publishNotification('success', 'Role permissions updated'))
dispatch(syncRole(role, data))
} catch (error) { } catch (error) {
dispatch(publishNotification('error', `Failed to updated role: ${error.data.message}`)) dispatch(publishNotification('error', `Failed to updated role: ${error.data.message}`))
} }
@ -209,8 +211,9 @@ export const updateRolePermissionsAsync = (role, permissions) => async (dispatch
export const updateUserPermissionsAsync = (user, permissions) => async (dispatch) => { export const updateUserPermissionsAsync = (user, permissions) => async (dispatch) => {
try { try {
await updateUserAJAX(user.links.self, user.roles, permissions) const {data} = await updateUserAJAX(user.links.self, user.roles, permissions)
dispatch(publishNotification('success', 'User permissions updated')) dispatch(publishNotification('success', 'User permissions updated'))
dispatch(syncUser(user, data))
} catch (error) { } catch (error) {
dispatch(publishNotification('error', `Failed to updated user: ${error.data.message}`)) dispatch(publishNotification('error', `Failed to updated user: ${error.data.message}`))
} }
@ -218,8 +221,9 @@ export const updateUserPermissionsAsync = (user, permissions) => async (dispatch
export const updateUserRolesAsync = (user, roles) => async (dispatch) => { export const updateUserRolesAsync = (user, roles) => async (dispatch) => {
try { try {
await updateUserAJAX(user.links.self, roles, user.permissions) const {data} = await updateUserAJAX(user.links.self, roles, user.permissions)
dispatch(publishNotification('success', 'User roles updated')) dispatch(publishNotification('success', 'User roles updated'))
dispatch(syncUser(user, data))
} catch (error) { } catch (error) {
dispatch(publishNotification('error', `Failed to updated user: ${error.data.message}`)) dispatch(publishNotification('error', `Failed to updated user: ${error.data.message}`))
} }

View File

@ -99,7 +99,7 @@ export const deleteUser = async (url, addFlashMessage, username) => {
export const updateRole = async (url, users, permissions) => { export const updateRole = async (url, users, permissions) => {
try { try {
await AJAX({ return await AJAX({
method: 'PATCH', method: 'PATCH',
url, url,
data: { data: {
@ -109,12 +109,13 @@ export const updateRole = async (url, users, permissions) => {
}) })
} catch (error) { } catch (error) {
console.error(error) console.error(error)
throw error
} }
} }
export const updateUser = async (url, roles, permissions) => { export const updateUser = async (url, roles, permissions) => {
try { try {
await AJAX({ return await AJAX({
method: 'PATCH', method: 'PATCH',
url, url,
data: { data: {
@ -124,5 +125,6 @@ export const updateUser = async (url, roles, permissions) => {
}) })
} catch (error) { } catch (error) {
console.error(error) console.error(error)
throw error
} }
} }

View File

@ -60,18 +60,18 @@ export default function admin(state = initialState, action) {
} }
} }
case 'CREATE_USER_SUCCESS': { case 'SYNC_USER': {
const {user, createdUser} = action.payload const {staleUser, syncedUser} = action.payload
const newState = { const newState = {
users: state.users.map(u => u.links.self === user.links.self ? {...createdUser} : u), users: state.users.map(u => u.links.self === staleUser.links.self ? {...syncedUser} : u),
} }
return {...state, ...newState} return {...state, ...newState}
} }
case 'CREATE_ROLE_SUCCESS': { case 'SYNC_ROLE': {
const {role, createdRole} = action.payload const {staleRole, syncedRole} = action.payload
const newState = { const newState = {
roles: state.roles.map(r => r.links.self === role.links.self ? {...createdRole} : r), roles: state.roles.map(r => r.links.self === staleRole.links.self ? {...syncedRole} : r),
} }
return {...state, ...newState} return {...state, ...newState}
} }