diff --git a/components/dashboard/userAccount/Head.js b/components/dashboard/userAccount/Head.js index 9b67ccf..c470d21 100644 --- a/components/dashboard/userAccount/Head.js +++ b/components/dashboard/userAccount/Head.js @@ -1,11 +1,21 @@ +"use client"; + +import { useEffect, useState } from "react"; import CalendarAdD from "@/components/icons/CalendarAdD"; import CardTickD from "@/components/icons/CardTickD"; import { getParsedUserInfo } from "@/helper"; import Image from "next/image"; function Head({ user }) { - const userInfo = getParsedUserInfo() || {}; - const displayName = userInfo.realName || user?.name || "کاربر"; + // مقدار اولیه از prop سرور تا SSR و کلاینت یکسان باشند (جلوگیری از hydration mismatch)؛ + // سپس در کلاینت نام واقعی از کوکی userInfo خوانده می‌شود. + const [displayName, setDisplayName] = useState(user?.name || "کاربر"); + + useEffect(() => { + const userInfo = getParsedUserInfo(); + const name = userInfo?.realName || user?.name; + if (name) setDisplayName(name); + }, [user?.name]); return (
diff --git a/components/dashboard/userAccount/detailUser/information/component/Field.js b/components/dashboard/userAccount/detailUser/information/component/Field.js index 9e93fae..d11dbd7 100644 --- a/components/dashboard/userAccount/detailUser/information/component/Field.js +++ b/components/dashboard/userAccount/detailUser/information/component/Field.js @@ -8,6 +8,7 @@ function Field({ icon, placeholder, data, + value, name, iconGray, error, @@ -16,12 +17,18 @@ function Field({ props, disable, }) { + // وقتی value پاس داده شود فیلد controlled است (برای مقادیر فقط‌خواندنی مثل موبایل)؛ + // در غیر این صورت رفتار قبلی (uncontrolled با defaultValue) حفظ می‌شود. + const controlled = + value !== undefined + ? { value: value ?? "" } + : { defaultValue: data }; + return (
diff --git a/components/dashboard/userAccount/detailUser/information/index.js b/components/dashboard/userAccount/detailUser/information/index.js index d872004..2459614 100644 --- a/components/dashboard/userAccount/detailUser/information/index.js +++ b/components/dashboard/userAccount/detailUser/information/index.js @@ -1,10 +1,8 @@ import { useEffect, useState } from "react"; -import Image from "next/image"; -import { toast } from "react-toastify"; import Form from "./form"; import { request } from "@/services/response"; import ButtonSendData from "./ButtonSendData"; -import { changeDateType, getParsedUserInfo, imageUrl } from "@/helper"; +import { changeDateType, getParsedUserInfo } from "@/helper"; import { disease } from "./form/disease"; import LoadingComponent from "@/app/component/LoadingComponent"; @@ -13,26 +11,6 @@ function Information({ information, setInformation }) { const [insurance, setInsurance] = useState(); const [errors, setErrors] = useState({}); const [loadingInfo, setLoadingInfo] = useState(true); - const [avatarUploading, setAvatarUploading] = useState(false); - - const handleAvatarChange = async (e) => { - const file = e.target.files?.[0]; - if (!file) return; - setAvatarUploading(true); - try { - const res = await request.uploadUserAvatar(file); - const url = res?.data?.avatar || res?.data?.url; - if (url) { - setInformation((prev) => ({ ...(prev || {}), avatar: url })); - toast.success("عکس پروفایل به‌روزرسانی شد"); - } - } catch { - toast.error("خطا در آپلود عکس"); - } finally { - setAvatarUploading(false); - e.target.value = ""; - } - }; useEffect(() => { const parsedUserInfo = getParsedUserInfo(); @@ -51,11 +29,9 @@ function Information({ information, setInformation }) { setLoadingInfo(false); const profile = res?.data?.data; - const hasProfileData = Boolean( - profile && (profile.label || profile.family || profile.national_code) - ); - - if (hasProfileData) { + // ملاک «پروفایل از قبل وجود دارد» وجود uuid پروفایل است (نه پر بودن فیلدها)؛ + // در غیر این صورت برای پروفایلِ خالیِ موجود، POST می‌زد و 409 می‌گرفت. + if (profile?.uuid) { let changedData = { ...profile }; // backend stores the first name in `label`; the form binds to `name` changedData.name = profile?.label ?? ""; @@ -75,7 +51,6 @@ function Information({ information, setInformation }) { setInformation(changedData); } else { setInformation({ - uuid: profile?.uuid, prev_data: false, other: { disease: disease, @@ -123,25 +98,6 @@ function Information({ information, setInformation }) { return (
-
- avatar - -
{ - setUserInfo(getParsedUserInfo()); + const parsed = getParsedUserInfo(); + setUserInfo(parsed); + if (parsed?.uuid) { + request + .getUserProfile(parsed.uuid) + .then((res) => { + const profile = res?.data?.data ?? res?.data; + if (profile?.avatar) setAvatar(profile.avatar); + }) + .catch(() => {}); + } }, []); + const handleAvatarChange = async (e) => { + const file = e.target.files?.[0]; + if (!file) return; + setUploading(true); + try { + const res = await request.uploadUserAvatar(file); + const url = res?.data?.avatar || res?.data?.url; + if (url) { + setAvatar(url); + toast.success("عکس پروفایل به‌روزرسانی شد"); + } + } catch { + toast.error("خطا در آپلود عکس"); + } finally { + setUploading(false); + if (fileRef.current) fileRef.current.value = ""; + } + }; + return (
- profile +
+ {avatar ? ( + profile + ) : ( +
+ {userInfo?.realName?.trim()?.[0] ?? "؟"} +
+ )} + + +

{userInfo?.realName}