diff --git a/scm-ui/src/users/modules/users.js b/scm-ui/src/users/modules/users.js index c76d0d4455..73b82f781e 100644 --- a/scm-ui/src/users/modules/users.js +++ b/scm-ui/src/users/modules/users.js @@ -158,37 +158,40 @@ function updateUserFailure(user: User, error: Error) { }; } -function requestDeleteUser(url: string) { +export function requestDeleteUser(user: User) { return { type: DELETE_USER, - url + payload: user }; } -function deleteUserSuccess() { +function deleteUserSuccess(user: User) { return { - type: DELETE_USER_SUCCESS + type: DELETE_USER_SUCCESS, + payload: user }; } -function deleteUserFailure(url: string, err: Error) { +export function deleteUserFailure(user: User, error: Error) { return { type: DELETE_USER_FAILURE, - payload: err, - url + payload: { + error, + user + } }; } -export function deleteUser(link: string) { - return function (dispatch: ThunkDispatch) { - dispatch(requestDeleteUser(link)); +export function deleteUser(user: User) { + return function (dispatch: any) { + dispatch(requestDeleteUser(user)); return apiClient - .delete(link) + .delete(user._links.delete.href) .then(() => { - dispatch(deleteUserSuccess()); + dispatch(deleteUserSuccess(user)); dispatch(fetchUsers()); }) - .catch(err => dispatch(deleteUserFailure(link, err))); + .catch(err => dispatch(deleteUserFailure(user, err))); }; } @@ -235,6 +238,18 @@ export function editUser(user: User) { }; } +const reduceUsersByNames = (state: any, username: string, newUserState: any) => { + const newUsersByNames = { + ...state.usersByNames, + [username] : newUserState + }; + + return { + ...state, + usersByNames: newUsersByNames + }; +}; + export default function reducer(state: any = {}, action: any = {}) { switch (action.type) { case FETCH_USERS: @@ -247,10 +262,11 @@ export default function reducer(state: any = {}, action: any = {}) { } }; case DELETE_USER: - return { - ...state, - users: null - }; + return reduceUsersByNames(state, action.payload.name, { + loading: true, + error: null, + entry: action.payload + }); case FETCH_USERS_SUCCESS: const users = action.payload._embedded.users; const userNames = users.map(user => user.name); @@ -269,6 +285,7 @@ export default function reducer(state: any = {}, action: any = {}) { }, usersByNames }; + case FETCH_USERS_FAILURE: return { ...state, @@ -276,11 +293,11 @@ export default function reducer(state: any = {}, action: any = {}) { loading: false }; case DELETE_USER_FAILURE: - return { - ...state, - error: action.payload, - loading: false - }; + return reduceUsersByNames(state, action.payload.user.name, { + loading: false, + error: action.payload.error, + entry: action.payload.user + }); case EDIT_USER: return { ...state, diff --git a/scm-ui/src/users/modules/users.test.js b/scm-ui/src/users/modules/users.test.js index a246ac1e83..8104ea9051 100644 --- a/scm-ui/src/users/modules/users.test.js +++ b/scm-ui/src/users/modules/users.test.js @@ -15,7 +15,9 @@ import { UPDATE_USER, UPDATE_USER_FAILURE, UPDATE_USER_SUCCESS, - EDIT_USER + EDIT_USER, + requestDeleteUser, + deleteUserFailure } from "./users"; import reducer from "./users"; @@ -215,6 +217,81 @@ describe("reducer tests", () => { }); }); + test("delete user requested", () => { + const state = { + usersByNames : { + "zaphod": { + loading: false, + error: null, + entry: userZaphod + } + } + }; + + const newState = reducer(state, requestDeleteUser(userZaphod)); + const zaphod = newState.usersByNames["zaphod"]; + expect(zaphod.loading).toBeTruthy(); + expect(zaphod.entry).toBe(userZaphod); + }) + + it("should not effect other users if one user will be deleted", () => { + const state = { + usersByNames : { + "zaphod": { + loading: false, + error: null, + entry: userZaphod + }, + "ford": { + loading: false + } + } + }; + + const newState = reducer(state, requestDeleteUser(userZaphod)); + const ford = newState.usersByNames["ford"]; + expect(ford.loading).toBeFalsy(); + }); + + it("should set the error of user which could not be deleted", () => { + const state = { + usersByNames : { + "zaphod": { + loading: false, + error: null, + entry: userZaphod + } + } + }; + + const error = new Error("error"); + const newState = reducer(state, deleteUserFailure(userZaphod, error)); + const zaphod = newState.usersByNames["zaphod"]; + expect(zaphod.loading).toBeFalsy(); + expect(zaphod.error).toBe(error); + }); + + it("should not effect other users if one user could not be deleted", () => { + const state = { + usersByNames : { + "zaphod": { + loading: false, + error: null, + entry: userZaphod + }, + "ford": { + loading: false + } + } + }; + + const error = new Error("error"); + const newState = reducer(state, deleteUserFailure(userZaphod, error)); + const ford = newState.usersByNames["ford"]; + expect(ford.loading).toBeFalsy(); + }); + + test("reducer does not replace whole usersByNames map", () => { const oldState = { usersByNames: { @@ -231,6 +308,7 @@ describe("reducer tests", () => { expect(newState.usersByNames["zaphod"]).toBeDefined(); expect(newState.usersByNames["ford"]).toBeDefined(); }); + test("edit user", () => { const newState = reducer(