import type { FunctionComponent } from "react"; import { useEffect } from "react"; import { useMutation } from "blitz"; import { useForm } from "react-hook-form"; import updateUser from "../../mutations/update-user"; import Alert from "../../../core/components/alert"; import Button from "../button"; import SettingsSection from "../settings-section"; import useCurrentUser from "../../../core/hooks/use-current-user"; import appLogger from "../../../../integrations/logger"; type Form = { fullName: string; email: string; }; const logger = appLogger.child({ module: "profile-settings" }); const ProfileInformations: FunctionComponent = () => { const { user } = useCurrentUser(); const [updateUserMutation, { error, isError, isSuccess }] = useMutation(updateUser); const { register, handleSubmit, setValue, formState: { isSubmitting }, } = useForm
(); useEffect(() => { setValue("fullName", user?.fullName ?? ""); setValue("email", user?.email ?? ""); }, [setValue, user]); const onSubmit = handleSubmit(async ({ fullName, email }) => { if (isSubmitting) { return; } await updateUserMutation({ email, fullName }); }); const errorMessage = parseErrorMessage(error as Error | null); return ( } > {isError ? (
) : null} {isSuccess ? (
) : null}
); }; export default ProfileInformations; function parseErrorMessage(error: Error | null): string { if (!error) { return ""; } if (error.name === "ZodError") { return JSON.parse(error.message)[0].message; } return error.message; }