import { zodResolver } from '@hookform/resolvers/zod' import { SubmitHandler, useForm } from 'react-hook-form' import { toast } from 'sonner' import { FormControl_Shadcn_, FormField_Shadcn_, FormItem_Shadcn_, FormLabel_Shadcn_, FormMessage_Shadcn_, Form_Shadcn_, Input_Shadcn_, } from 'ui' import z from 'zod' import { FormActions } from 'components/ui/Forms/FormActions' import Panel from 'components/ui/Panel' import { useProfileUpdateMutation } from 'data/profile/profile-update-mutation' import type { Profile } from 'data/profile/types' import type { FormSchema } from 'types' const FormSchema = z.object({ first_name: z.string().optional(), last_name: z.string().optional(), }) const formId = 'profile-information-form' export const ProfileInformation = ({ profile }: { profile: Profile }) => { const form = useForm({ resolver: zodResolver(FormSchema), defaultValues: { first_name: profile.first_name ?? '', last_name: profile.last_name ?? '' }, }) const { mutate: updateProfile, isLoading } = useProfileUpdateMutation({ onSuccess: (data) => { toast.success('Successfully saved profile') form.reset({ first_name: data.first_name, last_name: data.last_name }) }, onError: (error) => toast.error(`Failed to update profile: ${error.message}`), }) const onSubmit: SubmitHandler> = async (data) => { updateProfile({ firstName: data.first_name || '', lastName: data.last_name || '' }) } return ( <> Profile Information} footer={ form.reset()} /> } >
( First name )} /> ( Last name )} />
) }