Skip to content

Commit 07ab261

Browse files
committed
Fix image upload on edit profile page
1 parent 02d080a commit 07ab261

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

src/app/(authenticated)/profile/edit/EditProfileForm.tsx

+6-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useForm, useFieldArray } from "react-hook-form";
44
import Image from "next/image";
55
import { Plus, Save, Upload, X } from "lucide-react";
66
import CountryList from "country-list";
7-
import { ChangeEvent, useState } from "react";
7+
import { useEffect, useState } from "react";
88
import Resizer from "react-image-file-resizer";
99

1010
interface EditProfileFormProps {
@@ -54,7 +54,6 @@ export default function EditProfileForm({
5454
register,
5555
setValue,
5656
getValues,
57-
resetField,
5857
watch,
5958
} = useForm<FormData>({
6059
defaultValues: {
@@ -103,6 +102,7 @@ export default function EditProfileForm({
103102
maxLength: 3,
104103
},
105104
});
105+
const imgFile: FileList | undefined = watch("imgFile");
106106

107107
const onSubmit = handleSubmit(async (formData) => {
108108
const lookingFor = [];
@@ -146,14 +146,13 @@ export default function EditProfileForm({
146146
});
147147
});
148148

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) {
153152
const urlImage = URL.createObjectURL(file);
154153
setProfilePicturePreview(urlImage);
155154
}
156-
}
155+
}, [imgFile]);
157156

158157
return (
159158
<form className="flex flex-col gap-y-4 p-4" onSubmit={onSubmit}>
@@ -178,7 +177,6 @@ export default function EditProfileForm({
178177
accept="image/png, image/jpeg"
179178
className="absolute z-10 size-[150px] inset-0 mx-auto rounded-full opacity-0"
180179
{...register("imgFile")}
181-
onChange={handleProfilePictureUpload}
182180
/>
183181
<span className="text-sm text-red-700">{errors.imgFile?.message}</span>
184182
</div>

0 commit comments

Comments
 (0)