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
parent
5f851863a3
commit
410d0beb56
|
@ -2,7 +2,7 @@ import reducer from 'src/admin/reducers/admin'
|
|||
|
||||
import {
|
||||
addUser,
|
||||
createUserSuccess,
|
||||
syncUser,
|
||||
editUser,
|
||||
loadRoles,
|
||||
loadPermissions,
|
||||
|
@ -66,8 +66,8 @@ describe('Admin.Reducers', () => {
|
|||
expect(actual.users).to.deep.equal(expected.users)
|
||||
})
|
||||
|
||||
it('it can confirm a created user', () => {
|
||||
const addedUser = {
|
||||
it('it can sync a stale user', () => {
|
||||
const staleUser = {
|
||||
name: 'acidburn',
|
||||
password: 'pass1',
|
||||
roles: [],
|
||||
|
@ -75,9 +75,9 @@ describe('Admin.Reducers', () => {
|
|||
links: {self: ''},
|
||||
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 = {
|
||||
users: [u2, u1],
|
||||
}
|
||||
|
|
|
@ -43,19 +43,19 @@ export const addRole = () => ({
|
|||
type: 'ADD_ROLE',
|
||||
})
|
||||
|
||||
export const createUserSuccess = (user, createdUser) => ({
|
||||
type: 'CREATE_USER_SUCCESS',
|
||||
export const syncUser = (staleUser, syncedUser) => ({
|
||||
type: 'SYNC_USER',
|
||||
payload: {
|
||||
user,
|
||||
createdUser,
|
||||
staleUser,
|
||||
syncedUser,
|
||||
},
|
||||
})
|
||||
|
||||
export const createRoleSuccess = (role, createdRole) => ({
|
||||
type: 'CREATE_ROLE_SUCCESS',
|
||||
export const syncRole = (staleRole, syncedRole) => ({
|
||||
type: 'SYNC_ROLE',
|
||||
payload: {
|
||||
role,
|
||||
createdRole,
|
||||
staleRole,
|
||||
syncedRole,
|
||||
},
|
||||
})
|
||||
|
||||
|
@ -144,7 +144,7 @@ export const createUserAsync = (url, user) => async (dispatch) => {
|
|||
try {
|
||||
const {data} = await createUserAJAX(url, user)
|
||||
dispatch(publishNotification('success', 'User created successfully'))
|
||||
dispatch(createUserSuccess(user, data))
|
||||
dispatch(syncUser(user, data))
|
||||
} catch (error) {
|
||||
// undo optimistic update
|
||||
dispatch(publishNotification('error', `Failed to create user: ${error.data.message}`))
|
||||
|
@ -156,7 +156,7 @@ export const createRoleAsync = (url, role) => async (dispatch) => {
|
|||
try {
|
||||
const {data} = await createRoleAJAX(url, role)
|
||||
dispatch(publishNotification('success', 'Role created successfully'))
|
||||
dispatch(createRoleSuccess(role, data))
|
||||
dispatch(syncRole(role, data))
|
||||
} catch (error) {
|
||||
// undo optimistic update
|
||||
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) => {
|
||||
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(syncRole(role, data))
|
||||
} catch (error) {
|
||||
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) => {
|
||||
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(syncRole(role, data))
|
||||
} catch (error) {
|
||||
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) => {
|
||||
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(syncUser(user, data))
|
||||
} catch (error) {
|
||||
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) => {
|
||||
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(syncUser(user, data))
|
||||
} catch (error) {
|
||||
dispatch(publishNotification('error', `Failed to updated user: ${error.data.message}`))
|
||||
}
|
||||
|
|
|
@ -99,7 +99,7 @@ export const deleteUser = async (url, addFlashMessage, username) => {
|
|||
|
||||
export const updateRole = async (url, users, permissions) => {
|
||||
try {
|
||||
await AJAX({
|
||||
return await AJAX({
|
||||
method: 'PATCH',
|
||||
url,
|
||||
data: {
|
||||
|
@ -109,12 +109,13 @@ export const updateRole = async (url, users, permissions) => {
|
|||
})
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
export const updateUser = async (url, roles, permissions) => {
|
||||
try {
|
||||
await AJAX({
|
||||
return await AJAX({
|
||||
method: 'PATCH',
|
||||
url,
|
||||
data: {
|
||||
|
@ -124,5 +125,6 @@ export const updateUser = async (url, roles, permissions) => {
|
|||
})
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
|
|
@ -60,18 +60,18 @@ export default function admin(state = initialState, action) {
|
|||
}
|
||||
}
|
||||
|
||||
case 'CREATE_USER_SUCCESS': {
|
||||
const {user, createdUser} = action.payload
|
||||
case 'SYNC_USER': {
|
||||
const {staleUser, syncedUser} = action.payload
|
||||
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}
|
||||
}
|
||||
|
||||
case 'CREATE_ROLE_SUCCESS': {
|
||||
const {role, createdRole} = action.payload
|
||||
case 'SYNC_ROLE': {
|
||||
const {staleRole, syncedRole} = action.payload
|
||||
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}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue