From 22433bac5e9acc132ae3f4ade2380cff15e95a9a Mon Sep 17 00:00:00 2001 From: hamid zarghami Date: Sun, 14 Dec 2025 11:51:37 +0330 Subject: [PATCH] avatar profile --- .../[name]/(Profile)/profile/edit/page.tsx | 73 +++++++++++++++---- .../profile/hooks/userProfileData.ts | 6 ++ src/app/[name]/(Profile)/profile/page.tsx | 8 +- .../profile/service/ProfileService.ts | 23 +++++- .../[name]/(Profile)/profile/types/Types.ts | 17 +++++ 5 files changed, 108 insertions(+), 19 deletions(-) diff --git a/src/app/[name]/(Profile)/profile/edit/page.tsx b/src/app/[name]/(Profile)/profile/edit/page.tsx index 1f84c4a..60c231e 100644 --- a/src/app/[name]/(Profile)/profile/edit/page.tsx +++ b/src/app/[name]/(Profile)/profile/edit/page.tsx @@ -12,7 +12,7 @@ import React, { useEffect, useMemo } from 'react' import Calendar from '@/components/ui/calendar'; import { getDateLib } from "react-day-picker/persian"; import { useRouter } from 'next/navigation'; -import { useGetProfile, useUpdateProfile } from '../hooks/userProfileData'; +import { useGetProfile, useSingleUpload, useUpdateProfile } from '../hooks/userProfileData'; import { useFormik } from 'formik'; import * as Yup from 'yup'; import { UpdateProfileType } from '../types/Types'; @@ -23,9 +23,12 @@ type Props = object function ProfileIndex({ }: Props) { + const { mutateAsync: singleUpload, isPending: isUploading } = useSingleUpload(); const { data, isPending } = useGetProfile(); const { mutate: updateProfile, isPending: isUpdating } = useUpdateProfile(); const [showCalendar, setShowCalendar] = React.useState(false); + const [uploadedImageUrl, setUploadedImageUrl] = React.useState(null); + const fileInputRef = React.useRef(null); const dateLib = getDateLib() const router = useRouter(); @@ -117,6 +120,7 @@ function ProfileIndex({ }: Props) { lastName: submitValues.lastName, birthDate: birthDate ? formatDateToString(birthDate) : undefined, gender: submitValues.gender, + avatarUrl: uploadedImageUrl || undefined, }; updateProfile(payload, { @@ -141,6 +145,10 @@ function ProfileIndex({ }: Props) { gender: userData.gender !== undefined ? userData.gender : undefined, }; formik.setValues(values); + // اگر کاربر تصویر آواتار دارد، آن را نمایش بده + if (userData.avatarUrl) { + setUploadedImageUrl(userData.avatarUrl); + } } // eslint-disable-next-line react-hooks/exhaustive-deps }, [userData]); @@ -155,6 +163,25 @@ function ProfileIndex({ }: Props) { formik.setFieldValue('gender', index === 0); } + const handleImageUpload = async (e: React.ChangeEvent) => { + const file = e.target.files?.[0]; + if (!file) return; + + try { + const response = await singleUpload(file); + if (response?.data?.url) { + setUploadedImageUrl(response.data.url); + toast('تصویر با موفقیت آپلود شد', 'success'); + } + } catch (error) { + toast(extractErrorMessage(error), 'error'); + } + }; + + const handleImageClick = () => { + fileInputRef.current?.click(); + }; + if (isPending) { return (
@@ -173,20 +200,38 @@ function ProfileIndex({ }: Props) {
-
@@ -258,7 +303,7 @@ function ProfileIndex({ }: Props) {
-