refactor password tab

This commit is contained in:
Alex Holliday committed 2026-08-07 10:13:56 -07:00
1 parent 59267ef451
commit eaf93668f5
1 file changed
+61 -82
+61 -82
View File
@@ -1,13 +1,15 @@
import { Stack } from "@mui/material"; import { Stack } from "@mui/material";
import { ConfigBox } from "@/Components/design-elements"; import { ConfigBox } from "@/Components/design-elements";
import { TextField, Button } from "@/Components/inputs"; import { Button } from "@/Components/inputs";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { useTheme } from "@mui/material"; import { useTheme } from "@mui/material";
import { useForm, Controller } from "react-hook-form"; import { useForm, FormProvider } from "react-hook-form";
import { usePasswordForm } from "@/Hooks/usePasswordForm"; import { usePasswordForm } from "@/Hooks/usePasswordForm";
import { usePatch } from "@/Hooks/UseApi"; import { usePatch } from "@/Hooks/UseApi";
import type { PasswordFormData } from "@/Validation/password"; import type { PasswordFormData } from "@/Validation/password";
import { FormTextField } from "@/Components/inputs/forms/FormTextField";
import { LAYOUT } from "@/Utils/Theme/constants";
export const TabPassword = () => { export const TabPassword = () => {
const { t } = useTranslation(); const { t } = useTranslation();
@@ -15,10 +17,11 @@ export const TabPassword = () => {
const { resolver, defaults } = usePasswordForm(); const { resolver, defaults } = usePasswordForm();
const { patch, loading } = usePatch<FormData, void>(); const { patch, loading } = usePatch<FormData, void>();
const { control, handleSubmit, reset } = useForm<PasswordFormData>({ const form = useForm<PasswordFormData>({
resolver, resolver,
defaultValues: defaults, defaultValues: defaults,
}); });
const { handleSubmit, reset } = form;
const onSubmit = async (data: PasswordFormData) => { const onSubmit = async (data: PasswordFormData) => {
const fd = new FormData(); const fd = new FormData();
@@ -32,85 +35,61 @@ export const TabPassword = () => {
}; };
return ( return (
<Stack <FormProvider {...form}>
gap={theme.spacing(8)} <Stack
component="form" gap={theme.spacing(LAYOUT.MD)}
onSubmit={handleSubmit(onSubmit)} component="form"
> onSubmit={handleSubmit(onSubmit)}
<ConfigBox
title={t("pages.account.form.currentPassword.title")}
subtitle={t("pages.account.form.currentPassword.description")}
rightContent={
<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 ?? ""}
/>
)}
/>
}
/>
<ConfigBox
title={t("pages.account.form.newPassword.title")}
subtitle={t("pages.account.form.newPassword.description")}
rightContent={
<Stack gap={theme.spacing(8)}>
<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 ?? ""}
/>
)}
/>
<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 ?? ""}
/>
)}
/>
</Stack>
}
/>
<Button
type="submit"
variant="contained"
color="primary"
loading={loading}
sx={{ alignSelf: "flex-end", minWidth: 100 }}
> >
{t("common.buttons.save")} <ConfigBox
</Button> title={t("pages.account.form.currentPassword.title")}
</Stack> subtitle={t("pages.account.form.currentPassword.description")}
rightContent={
<FormTextField
name="currentPassword"
type="password"
autoComplete="current-password"
fieldLabel={t("pages.account.form.currentPassword.option.label")}
placeholder={t("pages.account.form.currentPassword.option.placeholder")}
/>
}
/>
<ConfigBox
title={t("pages.account.form.newPassword.title")}
subtitle={t("pages.account.form.newPassword.description")}
rightContent={
<Stack gap={theme.spacing(LAYOUT.MD)}>
<FormTextField
name="newPassword"
type="password"
autoComplete="new-password"
fieldLabel={t("pages.account.form.newPassword.option.newPassword.label")}
placeholder={t(
"pages.account.form.newPassword.option.newPassword.placeholder"
)}
/>
<FormTextField
name="confirm"
type="password"
autoComplete="new-password"
fieldLabel={t("pages.account.form.newPassword.option.confirm.label")}
placeholder={t(
"pages.account.form.newPassword.option.confirm.placeholder"
)}
/>
</Stack>
}
/>
<Button
type="submit"
variant="contained"
color="primary"
loading={loading}
sx={{ alignSelf: "flex-end", minWidth: 100 }}
>
{t("common.buttons.save")}
</Button>
</Stack>
</FormProvider>
); );
}; };