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>
This commit is contained in:
hamed
2026-06-15 15:44:12 +03:30
co-authored by Claude Opus 4.8
parent ba32cd4192
commit 61c21136d2
+26 -48
View File
@@ -17,6 +17,28 @@ const defaultData = {
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);
@@ -56,30 +78,8 @@ function AppointmentPage({ doctor, disabledDates, matchedCity }) {
try {
const res = await request.getUserProfile(parsedData.uuid);
if (res) {
const newData = {
uuid: res.uuid,
phone: { value: usernameFromCookie, isEdit: false },
national_code: {
value: res.national_code || "",
isEdit: res.national_code ? false : true, // اگر کد ملی داره، غیرقابل ویرایش
},
name: { value: res.name || "", isEdit: true },
family: { value: res.family || "", isEdit: true },
gender: {
value: res.gender ? { id: res.gender, title: res.gender === "male" ? "مرد" : "زن" } : "",
isEdit: true
},
insurance_id: { value: res.insurance_id || "", isEdit: true },
basic_insurance: {
value: res.basic_insurance,
isEdit: true,
},
supplementary_insurance: {
value: res.supplementary_insurance,
isEdit: true,
},
};
if (res?.data) {
const newData = buildProfileData(res.data, usernameFromCookie);
setData(newData);
setPrevData(newData);
}
@@ -120,30 +120,8 @@ function AppointmentPage({ doctor, disabledDates, matchedCity }) {
try {
const res = await request.getUserProfile(parsedData.uuid);
if (res) {
const newData = {
uuid: res.uuid,
phone: { value: usernameFromCookie, isEdit: false },
national_code: {
value: res.national_code || "",
isEdit: res.national_code ? false : true,
},
name: { value: res.name || "", isEdit: true },
family: { value: res.family || "", isEdit: true },
gender: {
value: res.gender ? { id: res.gender, title: res.gender === "male" ? "مرد" : "زن" } : "",
isEdit: true
},
insurance_id: { value: res.insurance_id || "", isEdit: true },
basic_insurance: {
value: res.basic_insurance,
isEdit: true,
},
supplementary_insurance: {
value: res.supplementary_insurance,
isEdit: true,
},
};
if (res?.data) {
const newData = buildProfileData(res.data, usernameFromCookie);
setData(newData);
setPrevData(newData);
}