Skip to content

Commit bc8db9b

Browse files
authored
Prevent self deletion in users page - issue 309 (#241)
* Prevent self deletion in users page * Fixed review comments * Fixed review comments
1 parent 960414a commit bc8db9b

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

src/components/UserList/ActionButtons.tsx

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const ActionButtons: React.FunctionComponent = () => {
1010
const props = useGridSlotComponentProps();
1111
const { enqueueSnackbar } = useSnackbar();
1212
const userDispatch = useUserDispatch();
13-
const { userList } = useUserState();
13+
const { userList, user } = useUserState();
1414

1515
const ids: GridRowId[] = React.useMemo(
1616
() => Object.values(props.state.selection),
@@ -23,22 +23,29 @@ export const ActionButtons: React.FunctionComponent = () => {
2323
<IconButton
2424
disabled={!ids.length}
2525
onClick={() => {
26-
usersService
27-
.remove(ids)
28-
.then(() => {
29-
enqueueSnackbar(`Removed`, {
30-
variant: "success",
31-
});
32-
userDispatch({
33-
type: "getAll",
34-
payload: userList.filter((user) => !ids.includes(user.id)),
35-
});
36-
})
37-
.catch((err) =>
38-
enqueueSnackbar(err, {
39-
variant: "error",
26+
const currentUserId = user?.id;
27+
if (currentUserId && ids.includes(currentUserId)) {
28+
enqueueSnackbar(`You cannot delete yourself.`, {
29+
variant: "error",
30+
});
31+
} else {
32+
usersService
33+
.remove(ids)
34+
.then(() => {
35+
enqueueSnackbar(`Removed`, {
36+
variant: "success",
37+
});
38+
userDispatch({
39+
type: "getAll",
40+
payload: userList.filter((user) => !ids.includes(user.id)),
41+
});
4042
})
41-
);
43+
.catch((err) =>
44+
enqueueSnackbar(err, {
45+
variant: "error",
46+
})
47+
);
48+
}
4249
}}
4350
>
4451
<Delete />

0 commit comments

Comments
 (0)