fix(user-profile): consume profile from double-nested 200 response
With the backend now returning 200 (lazy-created profile) instead of 404, align the consumers: the profile lives at res.data.data (the endpoint double-nests), so the booking detail and dashboard read that instead of res.data / the raw envelope. Drop the obsolete 404 special handling (empty editable form now comes from the 200 payload), seed the dashboard empty state when the profile has no real data yet, and make the profile POST fall back to PATCH on 409 (already lazy-created). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -86,7 +86,17 @@ function SubmitData({ setStep, data, prevData, setErrors, doctor, isForAnother,
|
||||
if (uuid) {
|
||||
await request.patchUserProfile(payload, uuid);
|
||||
} else {
|
||||
await request.postUserProfile(payload);
|
||||
const userUuid = data?.uuid || Cookies.get("uuid");
|
||||
try {
|
||||
await request.postUserProfile(payload);
|
||||
} catch (err) {
|
||||
// پروفایل از قبل ساخته شده (lazy-create در سمت backend) → بهجای ساخت، ویرایش
|
||||
if (err?.response?.status === 409 && userUuid) {
|
||||
await request.patchUserProfile(payload, userUuid);
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const refreshed = await refreshAccessToken();
|
||||
|
||||
@@ -78,26 +78,15 @@ function AppointmentPage({ doctor, disabledDates, matchedCity }) {
|
||||
if (userInfo && parsedData) {
|
||||
try {
|
||||
const res = await request.getUserProfile(parsedData.uuid);
|
||||
const profile = res?.data?.data;
|
||||
|
||||
if (res?.data) {
|
||||
const newData = buildProfileData(res.data, usernameFromCookie);
|
||||
if (profile) {
|
||||
const newData = buildProfileData(profile, 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);
|
||||
}
|
||||
@@ -120,26 +109,15 @@ function AppointmentPage({ doctor, disabledDates, matchedCity }) {
|
||||
if (userInfo && parsedData && !data.uuid) {
|
||||
try {
|
||||
const res = await request.getUserProfile(parsedData.uuid);
|
||||
const profile = res?.data?.data;
|
||||
|
||||
if (res?.data) {
|
||||
const newData = buildProfileData(res.data, usernameFromCookie);
|
||||
if (profile) {
|
||||
const newData = buildProfileData(profile, 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 },
|
||||
}));
|
||||
}
|
||||
// در صورت خطا، فرم خالی قابل ویرایش باقی میماند
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,17 +27,19 @@ function Information({ information, setInformation }) {
|
||||
.getUserProfile(parsedUserInfo.uuid)
|
||||
.then((res) => {
|
||||
setLoadingInfo(false);
|
||||
let changedData = res;
|
||||
const profile = res?.data?.data;
|
||||
|
||||
changedData.basic_insurance = [Number(res?.basic_insurance?.id)];
|
||||
changedData.prev_data = true;
|
||||
changedData = changeDateType(changedData, false);
|
||||
const hasProfileData = Boolean(
|
||||
profile && (profile.label || profile.family || profile.national_code)
|
||||
);
|
||||
|
||||
setInformation(changedData);
|
||||
})
|
||||
.catch((err) => {
|
||||
setLoadingInfo(false);
|
||||
if (err?.response?.data === "Entity not found") {
|
||||
if (hasProfileData) {
|
||||
let changedData = { ...profile };
|
||||
changedData.basic_insurance = [Number(profile?.basic_insurance?.id)];
|
||||
changedData.prev_data = true;
|
||||
changedData = changeDateType(changedData, false);
|
||||
setInformation(changedData);
|
||||
} else {
|
||||
setInformation({
|
||||
prev_data: false,
|
||||
other: {
|
||||
@@ -50,6 +52,20 @@ function Information({ information, setInformation }) {
|
||||
},
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
setLoadingInfo(false);
|
||||
setInformation({
|
||||
prev_data: false,
|
||||
other: {
|
||||
disease: disease,
|
||||
allergies: [],
|
||||
medications: [],
|
||||
surgeries: [],
|
||||
family_history: [],
|
||||
relatives: [],
|
||||
},
|
||||
});
|
||||
});
|
||||
} else {
|
||||
setLoadingInfo(false);
|
||||
|
||||
Reference in New Issue
Block a user