Files
kotosoft-id/kotosoft-id-frontend/app/me/items/profile.tsx
T
2026-03-08 14:54:29 +03:00

115 lines
3.9 KiB
TypeScript

"use client"
import { useState } from "react";
import { useTranslations } from "next-intl";
import { useForm, Controller, SubmitHandler } from "react-hook-form";
import type { User } from "@/types";
import { Button } from "@/components/ui/button";
import { Field, FieldError, FieldGroup, FieldLabel } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
import { Spinner } from "@/components/ui/spinner";
import { BirthdatePicker } from "@/components/ui/birthdate-picker";
import { UpdateUser } from "@/actions/auth";
interface Props {
user: User | null;
setUser: (user: User) => void;
}
interface FormData {
first_name: string;
second_name: string;
birth_date: string;
}
export default function Profile({ user, setUser }: Props) {
const t = useTranslations("MePage.items.profile");
const [error, setError] = useState<string | null>(null);
const { register, handleSubmit, control, formState: { errors, isSubmitting } } = useForm<FormData>({
defaultValues: {
first_name: user?.first_name,
second_name: user?.second_name,
birth_date: user?.birth_date ?? "",
},
});
const onSubmit: SubmitHandler<FormData> = async (data) => {
setError(null);
try {
const response = await UpdateUser(data.first_name, data.second_name, data.birth_date);
if (response == null) setError(t("form.updateError"))
else setUser(response)
} catch (error) {
setError(t("form.updateError"))
}
};
return (
<div className="w-full lg:max-w-sm flex flex-col gap-4">
<h1 className="text-xl">{t("title")}</h1>
<form onSubmit={handleSubmit(onSubmit)}>
<FieldGroup>
<Field data-invalid={!!errors.first_name}>
<FieldLabel htmlFor="first_name">{t("form.firstName.title")}</FieldLabel>
<Input
id="first_name"
type="text"
disabled={isSubmitting}
aria-invalid={!!errors.first_name}
{...register("first_name", {
required: t("form.required"),
minLength: { value: 2, message: t("form.firstName.minLength") },
maxLength: { value: 64, message: t("form.firstName.maxLength") },
})}
/>
{errors.first_name && <FieldError>{errors.first_name.message}</FieldError>}
</Field>
<Field data-invalid={!!errors.second_name}>
<FieldLabel htmlFor="second_name">{t("form.secondName.title")}</FieldLabel>
<Input
id="second_name"
type="text"
disabled={isSubmitting}
aria-invalid={!!errors.second_name}
{...register("second_name", {
required: t("form.required"),
minLength: { value: 2, message: t("form.secondName.minLength") },
maxLength: { value: 64, message: t("form.secondName.maxLength") },
})}
/>
{errors.second_name && <FieldError>{errors.second_name.message}</FieldError>}
</Field>
<Field data-invalid={!!errors.birth_date}>
<FieldLabel htmlFor="birth_date">{t("form.birthdate.title")}</FieldLabel>
<Controller
name="birth_date"
control={control}
render={({ field }) => (
<BirthdatePicker
value={field.value}
onChange={field.onChange}
disabled={isSubmitting}
aria-invalid={!!errors.birth_date}
/>
)}
/>
{errors.birth_date && <FieldError>{errors.birth_date.message}</FieldError>}
</Field>
<Field>
<Button type="submit" disabled={isSubmitting}>
{isSubmitting && <Spinner />}
{t("form.save")}
</Button>
{error && <FieldError className="text-center">{error}</FieldError>}
</Field>
</FieldGroup>
</form>
</div>
);
}