import type { FunctionComponent } from "react"; import { useEffect, useState } 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 = useMutation(updateUser)[0]; const { register, handleSubmit, setValue, formState: { isSubmitting, isSubmitSuccessful }, } = useForm
(); const [errorMessage, setErrorMessage] = useState(""); useEffect(() => { setValue("fullName", user?.fullName ?? ""); setValue("email", user?.email ?? ""); }, [setValue, user]); const onSubmit = handleSubmit(async ({ fullName, email }) => { if (isSubmitting) { return; } try { await updateUserMutation({ email, fullName }); } catch (error: any) { logger.error(error.response, "error updating user infos"); setErrorMessage(error.response.data.errorMessage); } }); return ( {errorMessage ? (
) : null} {isSubmitSuccessful ? (
) : null}
); }; export default ProfileInformations;