@@ -4,7 +4,7 @@ import { useForm, useFieldArray } from "react-hook-form";
4
4
import Image from "next/image" ;
5
5
import { Plus , Save , Upload , X } from "lucide-react" ;
6
6
import CountryList from "country-list" ;
7
- import { ChangeEvent , useState } from "react" ;
7
+ import { useEffect , useState } from "react" ;
8
8
import Resizer from "react-image-file-resizer" ;
9
9
10
10
interface EditProfileFormProps {
@@ -54,7 +54,6 @@ export default function EditProfileForm({
54
54
register,
55
55
setValue,
56
56
getValues,
57
- resetField,
58
57
watch,
59
58
} = useForm < FormData > ( {
60
59
defaultValues : {
@@ -103,6 +102,7 @@ export default function EditProfileForm({
103
102
maxLength : 3 ,
104
103
} ,
105
104
} ) ;
105
+ const imgFile : FileList | undefined = watch ( "imgFile" ) ;
106
106
107
107
const onSubmit = handleSubmit ( async ( formData ) => {
108
108
const lookingFor = [ ] ;
@@ -146,14 +146,13 @@ export default function EditProfileForm({
146
146
} ) ;
147
147
} ) ;
148
148
149
- function handleProfilePictureUpload ( event : ChangeEvent < HTMLInputElement > ) {
150
- if ( ! event . target . files ) setProfilePicturePreview ( user . img ) ;
151
- else {
152
- const file = event . target . files [ 0 ] ;
149
+ useEffect ( ( ) => {
150
+ const file = imgFile ?. item ( 0 ) ;
151
+ if ( file ) {
153
152
const urlImage = URL . createObjectURL ( file ) ;
154
153
setProfilePicturePreview ( urlImage ) ;
155
154
}
156
- }
155
+ } , [ imgFile ] ) ;
157
156
158
157
return (
159
158
< form className = "flex flex-col gap-y-4 p-4" onSubmit = { onSubmit } >
@@ -178,7 +177,6 @@ export default function EditProfileForm({
178
177
accept = "image/png, image/jpeg"
179
178
className = "absolute z-10 size-[150px] inset-0 mx-auto rounded-full opacity-0"
180
179
{ ...register ( "imgFile" ) }
181
- onChange = { handleProfilePictureUpload }
182
180
/>
183
181
< span className = "text-sm text-red-700" > { errors . imgFile ?. message } </ span >
184
182
</ div >
0 commit comments