import type { FunctionComponent } from "react"; import { useEffect, useState } from "react"; import { useRouter } from "blitz"; import { useForm } from "react-hook-form"; import Alert from "./alert"; import Button from "./button"; import SettingsSection from "./settings-section"; import useCurrentCustomer from "../../core/hooks/use-current-customer"; import appLogger from "../../../integrations/logger"; type Form = { name: string; email: string; }; const logger = appLogger.child({ module: "profile-settings" }); const ProfileInformations: FunctionComponent = () => { const { customer } = useCurrentCustomer(); const router = useRouter(); const { register, handleSubmit, setValue, formState: { isSubmitting, isSubmitSuccessful }, } = useForm
(); const [errorMessage, setErrorMessage] = useState(""); useEffect(() => { setValue("name", customer?.user.name ?? ""); setValue("email", customer?.user.email ?? ""); }, [setValue, customer]); const onSubmit = handleSubmit(async ({ name, email }) => { if (isSubmitting) { return; } try { // TODO // await customer.updateUser({ email, data: { name } }); } catch (error) { logger.error(error.response, "error updating user infos"); if (error.response.status === 401) { logger.error("session expired, redirecting to sign in page"); return router.push("/auth/sign-in"); } setErrorMessage(error.response.data.errorMessage); } }); return ( {errorMessage ? (
) : null} {isSubmitSuccessful ? (
) : null}
); }; export default ProfileInformations;