Skip to content

Commit

Permalink
feat(user): update user role func add
Browse files Browse the repository at this point in the history
  • Loading branch information
NSUWAL123 committed Jan 30, 2025
1 parent af785bc commit 5f6633c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/frontend/src/api/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AppDispatch } from '@/store/Store';
import { UserActions } from '@/store/slices/UserSlice';
import { userType } from '@/models/user/userModel';
import { paginationType } from '@/store/types/ICommon';
import { CommonActions } from '@/store/slices/CommonSlice';

export const GetUserListService = (url: string, params: { page: number; results_per_page: number; search: string }) => {
return async (dispatch: AppDispatch) => {
Expand All @@ -23,3 +24,36 @@ export const GetUserListService = (url: string, params: { page: number; results_
await getUserList(url);
};
};

export const UpdateUserRole = (url: string, payload: { role: 'READ_ONLY' | 'ADMIN' | 'MAPPER' }) => {
return async (dispatch: AppDispatch) => {
const updateUserRole = async (url: string) => {
dispatch(UserActions.SetUpdateUserRoleLoading(true));
try {
const response: AxiosResponse<userType> = await axios.patch(url, payload);
dispatch(UserActions.UpdateUserList(response.data));
dispatch(
CommonActions.SetSnackBar({
open: true,
message: `Updated ${response.data.username}'s role to ${response.data.role} successfully`,
variant: 'success',
duration: 2000,
}),
);
} catch (error) {
dispatch(
CommonActions.SetSnackBar({
open: true,
message: 'Failed to update user role',
variant: 'error',
duration: 2000,
}),
);
} finally {
dispatch(UserActions.SetUpdateUserRoleLoading(false));
}
};

await updateUserRole(url);
};
};
10 changes: 10 additions & 0 deletions src/frontend/src/store/slices/UserSlice.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { UserStateTypes } from '@/store/types/IUser';
import { userType } from '@/models/user/userModel';

export const initialState: UserStateTypes = {
userList: {
Expand All @@ -16,6 +17,7 @@ export const initialState: UserStateTypes = {
},
},
userListLoading: false,
updateUserRoleLoading: false,
};

const UserSlice = createSlice({
Expand All @@ -28,6 +30,14 @@ const UserSlice = createSlice({
SetUserListLoading: (state, action: PayloadAction<boolean>) => {
state.userListLoading = action.payload;
},
UpdateUserList: (state, action: PayloadAction<userType>) => {
state.userList.results = state.userList.results.map((user) =>
user.id === action.payload.id ? action.payload : user,
);
},
SetUpdateUserRoleLoading: (state, action: PayloadAction<boolean>) => {
state.updateUserRoleLoading = action.payload;
},
},
});

Expand Down
1 change: 1 addition & 0 deletions src/frontend/src/store/types/IUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ import { paginationType } from './ICommon';
export type UserStateTypes = {
userList: { results: userType[]; pagination: paginationType };
userListLoading: boolean;
updateUserRoleLoading: boolean;
};

0 comments on commit 5f6633c

Please sign in to comment.