refactor: update user data handling in AppointmentPage to improve loading state and editability of fields

This commit is contained in:
hamed
2025-11-17 16:17:50 +03:30
parent 3db4c7e851
commit a3fd984894
+50 -49
View File
@@ -7,9 +7,9 @@ import Container from "./Container";
const defaultData = {
phone: { value: "", isEdit: false },
national_code: { value: "", isEdit: false },
name: { value: "", isEdit: false },
basic_insurance: { value: "", isEdit: false },
national_code: { value: "", isEdit: true },
name: { value: "", isEdit: true },
basic_insurance: { value: "", isEdit: true },
};
function AppointmentPage({ doctor, disabledDates, matchedCity }) {
@@ -30,19 +30,30 @@ function AppointmentPage({ doctor, disabledDates, matchedCity }) {
const [selectedDate, setSelectedDate] = useState(null);
useEffect(() => {
setIsLoading(true);
const fetchUserData = async () => {
setIsLoading(true);
const userInfo = Cookies.get("userInfo");
const parsedData = userInfo && JSON.parse(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);
const userInfo = Cookies.get("userInfo");
const parsedData = userInfo && JSON.parse(userInfo);
const usernameFromCookie = parsedData?.username || "";
if (userInfo && parsedData) {
request
.getUserProfile(parsedData.uuid)
.then((res) => {
if (res) {
const newData = {
uuid: res.uuid,
phone: { value: usernameFromCookie, isEdit: true },
phone: { value: usernameFromCookie, isEdit: false },
national_code: {
value: res.national_code || "",
isEdit: res.national_code ? false : true,
@@ -55,48 +66,32 @@ function AppointmentPage({ doctor, disabledDates, matchedCity }) {
};
setData(newData);
setPrevData(newData);
} else {
const emptyData = {
uuid: res.uuid,
phone: { value: usernameFromCookie, isEdit: false },
national_code: { value: "", isEdit: true },
name: { value: "", isEdit: true },
basic_insurance: {
value: res.basic_insurance,
isEdit: true,
},
};
setData(emptyData);
setPrevData(emptyData);
}
} catch (error) {
// خطا در دریافت پروفایل
} finally {
setIsLoading(false);
})
.catch((error) => {
setIsLoading(false);
});
} else if (usernameFromCookie) {
// اگر فقط Cookie داریم بدون UUID
setData({
...defaultData,
phone: { value: usernameFromCookie, isEdit: false },
});
setIsLoading(false);
} else {
setIsLoading(false);
}
}
} else {
setIsLoading(false);
}
};
fetchUserData();
}, []); // فقط یک بار در mount اولیه اجرا می‌شه
// useEffect جداگانه برای زمانی که step تغییر می‌کنه
useEffect(() => {
if (step === 3) {
const userInfo = Cookies.get("userInfo");
const parsedData = userInfo && JSON.parse(userInfo);
const usernameFromCookie = parsedData?.username || "";
const refetchUserData = async () => {
if (step === 3) {
const userInfo = Cookies.get("userInfo");
const parsedData = userInfo && JSON.parse(userInfo);
const usernameFromCookie = parsedData?.username || "";
if (userInfo && parsedData && !data.uuid) {
try {
const res = await request.getUserProfile(parsedData.uuid);
if (userInfo && parsedData && !data.uuid) {
request
.getUserProfile(parsedData.uuid)
.then((res) => {
if (res) {
const newData = {
uuid: res.uuid,
@@ -114,11 +109,17 @@ function AppointmentPage({ doctor, disabledDates, matchedCity }) {
setData(newData);
setPrevData(newData);
}
});
} catch (error) {
// خطا در دریافت پروفایل
}
}
}
}
};
refetchUserData();
}, [step]);
if (isLoading) {
return (
<div className="flex items-center justify-center min-h-screen">