Files
Checkmate/client/src/Pages/Account/EditUser/index.tsx
T

236 lines
7.2 KiB
TypeScript
Raw Normal View History

2026-02-09 20:38:04 +00:00
import { Trash2 } from "lucide-react";
2026-02-20 18:13:39 +00:00
import { TextField, Button, Autocomplete } from "@/Components/inputs";
2026-02-09 20:38:04 +00:00
import Stack from "@mui/material/Stack";
import Typography from "@mui/material/Typography";
import IconButton from "@mui/material/IconButton";
import Divider from "@mui/material/Divider";
import FormHelperText from "@mui/material/FormHelperText";
import { useForm, Controller } from "react-hook-form";
2026-02-20 18:13:39 +00:00
import { ConfigBox, BasePage } from "@/Components/design-elements";
2026-02-09 20:38:04 +00:00
2026-02-09 19:26:58 +00:00
import { UserRoles } from "@/Types/User";
2026-02-09 20:38:04 +00:00
import { useTranslation } from "react-i18next";
import type { UserRole, User } from "@/Types/User";
import { useParams, useNavigate } from "react-router-dom";
2026-02-09 20:38:04 +00:00
import { useTheme } from "@mui/material";
import { useEditUserForm } from "@/Hooks/useEditUserForm";
2026-03-06 14:18:27 -05:00
import { useGet, usePatch, useDelete } from "@/Hooks/UseApi";
2026-02-09 20:38:04 +00:00
import type { EditUserFormData } from "@/Validation/editUser";
2026-03-06 14:18:27 -05:00
import { useEffect, useState } from "react";
import { DialogInput } from "@/Components/inputs/Dialog";
import { useSelector } from "react-redux";
import type { RootState } from "@/Types/state";
2026-03-06 12:36:26 -08:00
import { LAYOUT } from "@/Utils/Theme/constants";
2026-03-06 15:19:01 -05:00
import { useIsAdmin, useIsSuperAdmin } from "@/Hooks/useIsAdmin";
2026-02-09 20:07:45 +00:00
interface RoleOption {
2026-02-09 20:38:04 +00:00
id: UserRole;
2026-02-09 20:07:45 +00:00
name: string;
}
2026-02-09 19:26:58 +00:00
const EditUserPage = () => {
const theme = useTheme();
const { t } = useTranslation();
2026-02-09 20:38:04 +00:00
const { userId } = useParams<{ userId: string }>();
const { resolver, defaults } = useEditUserForm();
const { patch, loading: isSaving } = usePatch();
2026-03-06 14:18:27 -05:00
const { deleteFn, loading: isDeleting } = useDelete();
const navigate = useNavigate();
const [showDeleteDialog, setShowDeleteDialog] = useState(false);
const currentUser = useSelector((state: RootState) => state.auth.user);
2026-03-06 15:19:01 -05:00
const isSuperAdmin = useIsSuperAdmin();
const isAdmin = useIsAdmin();
2026-02-09 19:26:58 +00:00
2026-02-09 20:38:04 +00:00
const { data: user, isLoading } = useGet<User>(`/auth/users/${userId}`);
2026-02-09 19:26:58 +00:00
2026-02-09 20:38:04 +00:00
const { control, handleSubmit, reset, watch, setValue } = useForm<EditUserFormData>({
resolver,
defaultValues: defaults,
});
useEffect(() => {
if (user) {
reset({
firstName: user.firstName || "",
lastName: user.lastName || "",
role: user.role || [],
});
}
}, [user, reset]);
2026-02-09 19:26:58 +00:00
2026-02-09 20:38:04 +00:00
const editableRoles = UserRoles.filter((role) => role !== "superadmin");
2026-02-09 20:07:45 +00:00
const roleOptions: RoleOption[] = editableRoles.map((role) => ({
id: role,
name: t(`common.auth.roles.${role}`),
}));
2026-02-09 20:38:04 +00:00
const watchedRoles = watch("role");
const selectedRoles = roleOptions.filter((r) => watchedRoles?.includes(r.id));
const handleRemoveRole = (roleToRemove: UserRole) => {
const newRoles = watchedRoles.filter((role) => role !== roleToRemove);
setValue("role", newRoles, { shouldValidate: true });
};
2026-02-09 20:07:45 +00:00
2026-03-06 14:18:27 -05:00
const canDeleteUser =
isAdmin &&
userId !== currentUser?.id &&
!user?.role?.includes("demo") &&
(isSuperAdmin || user?.role?.every((r) => r !== "admin" && r !== "superadmin"));
const handleDeleteUser = async () => {
const result = await deleteFn(`/auth/users/${userId}`);
2026-03-06 15:19:01 -05:00
setShowDeleteDialog(false);
2026-03-06 14:18:27 -05:00
if (result) {
navigate("/account", { state: { tab: "team" } });
}
};
2026-02-09 20:38:04 +00:00
const onSubmit = async (data: EditUserFormData) => {
await patch(`/auth/users/${userId}`, data);
2026-02-09 20:07:45 +00:00
};
2026-02-09 19:26:58 +00:00
return (
2026-02-09 20:38:04 +00:00
<BasePage loading={isLoading}>
<form onSubmit={handleSubmit(onSubmit)}>
<Stack gap={theme.spacing(8)}>
<ConfigBox
title={t("pages.account.form.name.title")}
subtitle={t("pages.account.form.name.description")}
rightContent={
<Stack gap={theme.spacing(8)}>
<Controller
name="firstName"
control={control}
render={({ field, fieldState }) => (
<TextField
{...field}
fieldLabel={t("common.form.name.option.firstName.label")}
placeholder={t("common.form.name.option.firstName.placeholder")}
error={!!fieldState.error}
helperText={fieldState.error?.message}
autoComplete="given-name"
/>
)}
/>
<Controller
name="lastName"
control={control}
render={({ field, fieldState }) => (
<TextField
{...field}
fieldLabel={t("common.form.name.option.lastName.label")}
placeholder={t("common.form.name.option.lastName.placeholder")}
error={!!fieldState.error}
helperText={fieldState.error?.message}
autoComplete="family-name"
/>
)}
/>
<TextField
fieldLabel={t("common.form.email.option.email.label")}
placeholder={t("common.form.email.option.email.placeholder")}
value={user?.email || ""}
disabled
/>
</Stack>
}
/>
<ConfigBox
title={t("pages.editUser.form.roles.title")}
subtitle={t("pages.editUser.form.roles.description")}
rightContent={
<Stack spacing={theme.spacing(4)}>
<Controller
name="role"
control={control}
render={({ field, fieldState }) => (
<>
<Autocomplete
fieldLabel={t("common.form.role.option.role.label")}
multiple
options={roleOptions}
value={selectedRoles}
getOptionLabel={(option) => option.name}
onChange={(_: unknown, newValue: RoleOption[]) => {
field.onChange(newValue.map((r) => r.id));
}}
isOptionEqualToValue={(option, value) => option.id === value.id}
/>
{fieldState.error && (
<FormHelperText error>{fieldState.error.message}</FormHelperText>
)}
</>
)}
/>
{selectedRoles.length > 0 && (
<Stack
flex={1}
width="100%"
>
{selectedRoles.map((role, index) => (
<Stack
direction="row"
alignItems="center"
key={role.id}
width="100%"
2026-02-09 20:07:45 +00:00
>
2026-02-09 20:38:04 +00:00
<Typography flexGrow={1}>{role.name}</Typography>
<IconButton
size="small"
onClick={() => handleRemoveRole(role.id)}
aria-label="Remove role"
>
<Trash2 size={16} />
</IconButton>
{index < selectedRoles.length - 1 && <Divider />}
</Stack>
))}
</Stack>
)}
</Stack>
}
/>
2026-03-06 14:18:27 -05:00
<Stack
gap={LAYOUT.XS}
2026-03-06 14:18:27 -05:00
direction="row"
justifyContent={"flex-end"}
2026-03-06 14:18:27 -05:00
width="100%"
2026-02-09 20:38:04 +00:00
>
2026-03-06 14:18:27 -05:00
{canDeleteUser && (
<Button
variant="contained"
color="error"
onClick={() => setShowDeleteDialog(true)}
sx={{ minWidth: 100 }}
>
{t("common.buttons.removeUser")}
2026-03-06 14:18:27 -05:00
</Button>
)}
<Button
type="submit"
variant="contained"
color="primary"
loading={isSaving}
sx={{ minWidth: 100 }}
>
{t("common.buttons.save")}
</Button>
</Stack>
2026-02-09 20:38:04 +00:00
</Stack>
</form>
2026-03-06 14:18:27 -05:00
<DialogInput
open={showDeleteDialog}
title={t("pages.editUser.dialog.removeUser.title")}
content={t("pages.editUser.dialog.removeUser.content", {
name: `${user?.firstName} ${user?.lastName}`,
})}
onCancel={() => setShowDeleteDialog(false)}
onConfirm={handleDeleteUser}
confirmText={t("common.buttons.removeUser")}
2026-03-06 14:18:27 -05:00
loading={isDeleting}
/>
2026-02-09 19:26:58 +00:00
</BasePage>
);
};
export default EditUserPage;