import { FC, useEffect, useState } from 'react' import Input from '../../../components/Input' import Button from '../../../components/Button' import { Edit } from 'iconsax-react' import { useTranslation } from 'react-i18next' import { useUpdateProfile } from '../hooks/useProfileData' import { UpdateProfileType } from '../types/ProfileTypes' import { toast } from '../../../components/Toast' import { ErrorType } from '../../../helpers/types' type Props = { firstName: string | null | undefined } const FirstName: FC = (props: Props) => { const { t } = useTranslation('global') const [value, setValue] = useState(props.firstName || '') const [isReadOnly, setIsReadOnly] = useState(true) const updateProfile = useUpdateProfile() useEffect(() => { setValue(props.firstName || '') }, [props.firstName]) const handleSave = () => { const params: UpdateProfileType = { firstName: value.trim() } updateProfile.mutate(params, { onSuccess: () => { toast(t('success'), 'success') setIsReadOnly(true) }, onError: (error: ErrorType) => { toast(error.response?.data?.error?.message?.[0], 'error') } }) } const isDisabled = value.trim().length < 2 || value.trim() === (props.firstName || '') return (
setValue(e.target.value)} /> { isReadOnly ? (
setIsReadOnly(false)} className='flex cursor-pointer relative -top-3 gap-1 text-[#0047FF] text-xs items-center'>
{t('edit')}
) : (
) } export default FirstName