Files
Checkmate/client/src/Pages/Account/TabPassword.tsx
T

116 lines
3.2 KiB
TypeScript
Raw Normal View History

2026-02-07 20:54:17 +00:00
import { Stack } from "@mui/material";
import { ConfigBox } from "@/Components/v2/design-elements";
2026-02-07 20:57:03 +00:00
import { TextField, Button } from "@/Components/v2/inputs";
2026-02-07 20:54:17 +00:00
import { useTranslation } from "react-i18next";
import { useTheme } from "@mui/material";
2026-02-07 20:57:03 +00:00
import { useForm, Controller } from "react-hook-form";
import { usePasswordForm } from "@/Hooks/usePasswordForm";
2026-02-07 21:05:45 +00:00
import { usePatch } from "@/Hooks/UseApi";
2026-02-07 20:57:03 +00:00
import type { PasswordFormData } from "@/Validation/password";
2026-02-07 00:49:49 +00:00
export const TabPassword = () => {
2026-02-07 20:54:17 +00:00
const { t } = useTranslation();
const theme = useTheme();
2026-02-07 20:57:03 +00:00
const { resolver, defaults } = usePasswordForm();
2026-02-07 21:05:45 +00:00
const { patch, loading } = usePatch<FormData, void>();
2026-02-07 20:57:03 +00:00
2026-02-08 16:14:35 +00:00
const { control, handleSubmit, reset } = useForm<PasswordFormData>({
2026-02-07 20:57:03 +00:00
resolver,
defaultValues: defaults,
});
2026-02-07 21:05:45 +00:00
const onSubmit = async (data: PasswordFormData) => {
const fd = new FormData();
fd.append("password", data.currentPassword);
fd.append("newPassword", data.newPassword);
const result = await patch("/auth/user", fd);
if (result?.success) {
reset();
}
2026-02-07 20:57:03 +00:00
};
2026-02-07 20:54:17 +00:00
return (
2026-02-07 20:57:03 +00:00
<Stack
gap={theme.spacing(8)}
component="form"
onSubmit={handleSubmit(onSubmit)}
>
2026-02-07 20:54:17 +00:00
<ConfigBox
title={t("pages.account.form.currentPassword.title")}
subtitle={t("pages.account.form.currentPassword.description")}
rightContent={
2026-02-07 20:57:03 +00:00
<Controller
name="currentPassword"
control={control}
render={({ field, fieldState }) => (
<TextField
{...field}
type="password"
fieldLabel={t("pages.account.form.currentPassword.option.label")}
placeholder={t("pages.account.form.currentPassword.option.placeholder")}
autoComplete="current-password"
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/>
)}
2026-02-07 20:54:17 +00:00
/>
}
/>
<ConfigBox
title={t("pages.account.form.newPassword.title")}
subtitle={t("pages.account.form.newPassword.description")}
rightContent={
<Stack gap={theme.spacing(8)}>
2026-02-07 20:57:03 +00:00
<Controller
name="newPassword"
control={control}
render={({ field, fieldState }) => (
<TextField
{...field}
type="password"
fieldLabel={t(
"pages.account.form.newPassword.option.newPassword.label"
)}
placeholder={t(
"pages.account.form.newPassword.option.newPassword.placeholder"
)}
autoComplete="new-password"
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/>
2026-02-07 20:54:17 +00:00
)}
/>
2026-02-07 20:57:03 +00:00
<Controller
name="confirm"
control={control}
render={({ field, fieldState }) => (
<TextField
{...field}
type="password"
fieldLabel={t("pages.account.form.newPassword.option.confirm.label")}
placeholder={t(
"pages.account.form.newPassword.option.confirm.placeholder"
)}
autoComplete="new-password"
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/>
)}
2026-02-07 20:54:17 +00:00
/>
</Stack>
}
/>
2026-02-07 20:57:03 +00:00
<Button
type="submit"
variant="contained"
color="primary"
2026-02-07 21:05:45 +00:00
loading={loading}
2026-02-07 20:57:03 +00:00
sx={{ alignSelf: "flex-end", minWidth: 100 }}
>
{t("common.buttons.save")}
</Button>
2026-02-07 20:54:17 +00:00
</Stack>
);
2026-02-07 00:49:49 +00:00
};