Files
Checkmate/client/src/pages/profile/Profile.tsx
T

50 lines
1.4 KiB
TypeScript
Raw Normal View History

2025-11-13 13:47:26 -08:00
import { BasePage, InfoBox } from "@/components/design-elements";
import { ProfileForm } from "@/pages/profile/ProfileForm";
import { useState, useEffect } from "react";
import type { IUser } from "@/types/user";
import type { ApiResponse } from "@/hooks/UseApi";
import { useGet, usePatch } from "@/hooks/UseApi";
import { z } from "zod";
import { profileSchema } from "@/validation/zod";
2025-12-12 12:17:18 -08:00
import { useTranslation } from "react-i18next";
2025-11-13 13:47:26 -08:00
type FormValues = z.infer<typeof profileSchema>;
const Profile = () => {
2025-12-12 12:17:18 -08:00
const { t } = useTranslation();
2025-11-13 13:47:26 -08:00
const [user, setUser] = useState<Partial<IUser> | null>(null);
const { response, loading } = useGet<ApiResponse<any>>(`/profile`, {}, {});
useEffect(() => {
if (response?.data) setUser(response.data);
}, [response]);
const { patch, loading: isPatching } = usePatch<FormValues, Partial<IUser>>();
const onSubmit = async (data: FormValues) => {
const res = await patch(`/profile`, data);
if (res) {
setUser(res.data);
}
};
const initialData = {
firstName: user?.firstName || "",
lastName: user?.lastName || "",
};
return (
<BasePage>
<InfoBox
2025-12-12 12:17:18 -08:00
title={t("profile.infoBox.title")}
description={t("profile.infoBox.description")}
2025-11-13 13:47:26 -08:00
/>
<ProfileForm
user={user}
onSubmit={onSubmit}
loading={loading || isPatching}
initialData={initialData}
/>
</BasePage>
);
};
export default Profile;