Files
nobat724_front/components/appointment/index.js
T
hamedandClaude Opus 4.8 61c21136d2 fix(appointment): read user profile from response envelope
getUserProfile returns { success, data } (interceptor unwraps once), so
the profile fields live under res.data, not res directly — the form was
always populated empty. Extract a buildProfileData helper and use it in
both the mount and step-3 effects, removing the duplicated mapping.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-15 15:44:12 +03:30

191 lines
6.3 KiB
JavaScript

"use client";
import { useEffect, useState } from "react";
import { request } from "@/services/response";
import Cookies from "js-cookie";
import Container from "./Container";
import { safeJsonParse } from "@/lib/sanitize";
const defaultData = {
phone: { value: "", isEdit: false },
national_code: { value: "", isEdit: true },
name: { value: "", isEdit: true },
family: { value: "", isEdit: true },
gender: { value: "", isEdit: true },
insurance_id: { value: "", isEdit: true },
basic_insurance: { value: "", isEdit: true },
supplementary_insurance: { value: "", isEdit: true },
};
function buildProfileData(profile, phone) {
return {
uuid: profile.uuid,
phone: { value: phone, isEdit: false },
national_code: {
value: profile.national_code || "",
isEdit: profile.national_code ? false : true,
},
name: { value: profile.name || "", isEdit: true },
family: { value: profile.family || "", isEdit: true },
gender: {
value: profile.gender
? { id: profile.gender, title: profile.gender === "male" ? "مرد" : "زن" }
: "",
isEdit: true,
},
insurance_id: { value: profile.insurance_id || "", isEdit: true },
basic_insurance: { value: profile.basic_insurance, isEdit: true },
supplementary_insurance: { value: profile.supplementary_insurance, isEdit: true },
};
}
function AppointmentPage({ doctor, disabledDates, matchedCity }) {
const [step, setStep] = useState(0);
const [isForAnother, setIsForAnother] = useState(false);
const [isLoading, setIsLoading] = useState(true);
const [prevData, setPrevData] = useState(defaultData);
const [data, setData] = useState(defaultData);
// Login states
const [num, setNum] = useState("");
const [uuid, setUuid] = useState("");
const [isSendMsg, setIsSendMsg] = useState(false);
// Appointment slot states
const [selectedSlot, setSelectedSlot] = useState(null);
const [selectedDate, setSelectedDate] = useState(null);
const [appointmentId, setAppointmentId] = useState(null);
useEffect(() => {
const fetchUserData = async () => {
setIsLoading(true);
const userInfo = Cookies.get("userInfo");
const parsedData = safeJsonParse(userInfo);
const usernameFromCookie = parsedData?.username || "";
// ابتدا شماره موبایل را از Cookie ست می‌کنیم
if (usernameFromCookie) {
setData(prev => ({
...prev,
phone: { value: usernameFromCookie, isEdit: false },
}));
}
// سپس اگر uuid داریم، بقیه اطلاعات را از API می‌گیریم
if (userInfo && parsedData) {
try {
const res = await request.getUserProfile(parsedData.uuid);
if (res?.data) {
const newData = buildProfileData(res.data, usernameFromCookie);
setData(newData);
setPrevData(newData);
}
} catch (error) {
// اگر 404 بود، فقط شماره موبایل را نگه می‌داریم و بقیه قابل ویرایش می‌شوند
if (error?.response?.status === 404) {
setData(prev => ({
...prev,
national_code: { value: "", isEdit: true },
name: { value: "", isEdit: true },
family: { value: "", isEdit: true },
gender: { value: "", isEdit: true },
insurance_id: { value: "", isEdit: true },
basic_insurance: { value: "", isEdit: true },
supplementary_insurance: { value: "", isEdit: true },
}));
}
} finally {
setIsLoading(false);
}
} else {
setIsLoading(false);
}
};
fetchUserData();
}, []); // فقط یک بار در mount اولیه اجرا می‌شه
// useEffect جداگانه برای زمانی که step تغییر می‌کنه
useEffect(() => {
const refetchUserData = async () => {
if (step === 3) {
const userInfo = Cookies.get("userInfo");
const parsedData = safeJsonParse(userInfo);
const usernameFromCookie = parsedData?.username || "";
if (userInfo && parsedData && !data.uuid) {
try {
const res = await request.getUserProfile(parsedData.uuid);
if (res?.data) {
const newData = buildProfileData(res.data, usernameFromCookie);
setData(newData);
setPrevData(newData);
}
} catch (error) {
// اگر 404 بود، فقط شماره موبایل را نگه می‌داریم و بقیه قابل ویرایش می‌شوند
if (error?.response?.status === 404) {
setData(prev => ({
...prev,
national_code: { value: "", isEdit: true },
name: { value: "", isEdit: true },
family: { value: "", isEdit: true },
gender: { value: "", isEdit: true },
insurance_id: { value: "", isEdit: true },
basic_insurance: { value: "", isEdit: true },
supplementary_insurance: { value: "", isEdit: true },
}));
}
}
}
}
};
refetchUserData();
}, [step]);
if (isLoading) {
return (
<div className="flex items-center justify-center min-h-screen">
<div className="flex flex-col items-center gap-4">
<div className="w-12 h-12 border-4 border-[#5559CE] border-t-transparent rounded-full animate-spin"></div>
<p className="text-[#3B3B3B] text-[16px]">در حال بارگذاری...</p>
</div>
</div>
);
}
return (
<Container
step={step}
data={data}
doctor={doctor}
setData={setData}
setStep={setStep}
prevData={prevData}
matchedCity={matchedCity}
isForAnother={isForAnother}
setIsForAnother={setIsForAnother}
disabledDates={disabledDates}
num={num}
setNum={setNum}
uuid={uuid}
setUuid={setUuid}
isSendMsg={isSendMsg}
setIsSendMsg={setIsSendMsg}
selectedSlot={selectedSlot}
setSelectedSlot={setSelectedSlot}
selectedDate={selectedDate}
setSelectedDate={setSelectedDate}
appointmentId={appointmentId}
setAppointmentId={setAppointmentId}
/>
);
}
export default AppointmentPage;