refactor: enhance handling of supplementary insurance in Form and AppointmentPage components
This commit is contained in:
@@ -14,7 +14,8 @@ function DefaultSelect({ list, value, updateData, label, error, name }) {
|
||||
}}
|
||||
className="!w-full !rounded-lg auto-complete-selector"
|
||||
onChange={(_, newValue) => {
|
||||
updateData(name, newValue?.id || newValue?.label || newValue || "");
|
||||
// اگر آیتم انتخاب شده باشد، کل شیء را برمیگردانیم، در غیر این صورت خالی
|
||||
updateData(name, newValue || "");
|
||||
}}
|
||||
sx={{
|
||||
...styleTextSelectRight,
|
||||
|
||||
@@ -58,7 +58,7 @@ function Form({ changeData, insurance, supplementaryInsurance, data, isForAnothe
|
||||
error={errors?.gender}
|
||||
/>
|
||||
</Content>
|
||||
<Content title="شماره بیمه">
|
||||
<Content title="شماره بیمه (اختیاری)">
|
||||
<EditField changeData={changeData} data={data?.insurance_id} name="insurance_id" error={errors?.insurance_id} />
|
||||
</Content>
|
||||
<Content title="نوع بیمه">
|
||||
@@ -71,7 +71,7 @@ function Form({ changeData, insurance, supplementaryInsurance, data, isForAnothe
|
||||
error={errors?.basic_insurance}
|
||||
/>
|
||||
</Content>
|
||||
<Content title="بیمه تکمیلی">
|
||||
<Content title="بیمه تکمیلی (اختیاری)">
|
||||
<DefaultSelect
|
||||
updateData={(name, val) => changeData(val, "value", name)}
|
||||
label="بیمه تکمیلی"
|
||||
|
||||
@@ -3,11 +3,41 @@ import ButtonFixed from "../paying/ButtonFixed";
|
||||
import { Button } from "@mui/material";
|
||||
import ArrowLeftB from "@/components/icons/ArrowLeftB";
|
||||
import { request } from "@/services/response";
|
||||
import Cookies from "js-cookie";
|
||||
|
||||
function SubmitData({ setStep, data, prevData, setErrors }) {
|
||||
const [loading, setLoading] = useState(false);
|
||||
const newStep = () => setStep((prev) => prev + 1);
|
||||
|
||||
const refreshAccessToken = async () => {
|
||||
try {
|
||||
const refreshToken = Cookies.get("refresh_token");
|
||||
if (!refreshToken) {
|
||||
console.log("⚠️ refresh_token موجود نیست");
|
||||
return;
|
||||
}
|
||||
|
||||
const formData = new URLSearchParams();
|
||||
formData.append("grant_type", "refresh_token");
|
||||
formData.append("client_id", process.env.NEXT_PUBLIC_CLIENT_ID || "4gSZTqkcM-13yfCHptFjsIoEzwA996bjGuSEFy02Dkc");
|
||||
formData.append("client_secret", process.env.NEXT_PUBLIC_CLIENT_SECRET || "13yfCHptFjsIoEzwA996bjGuSEFy02Dkc");
|
||||
formData.append("refresh_token", refreshToken);
|
||||
|
||||
console.log("🔄 در حال refresh کردن access token...");
|
||||
const response = await request.postRefreshToken(formData);
|
||||
|
||||
if (response?.access_token) {
|
||||
Cookies.set("access_token", response.access_token, { expires: 7 });
|
||||
if (response.refresh_token) {
|
||||
Cookies.set("refresh_token", response.refresh_token, { expires: 7 });
|
||||
}
|
||||
console.log("✅ access token با موفقیت refresh شد");
|
||||
}
|
||||
} catch (error) {
|
||||
console.log("❌ خطا در refresh token:", error);
|
||||
}
|
||||
};
|
||||
|
||||
const handleSubmit = async () => {
|
||||
const isChanged = JSON.stringify(prevData) !== JSON.stringify(data);
|
||||
|
||||
@@ -18,15 +48,44 @@ function SubmitData({ setStep, data, prevData, setErrors }) {
|
||||
setLoading(true);
|
||||
const { basic_insurance, supplementary_insurance, name, family, national_code, gender, insurance_id, uuid } = data;
|
||||
|
||||
const payload = {
|
||||
name: name?.value,
|
||||
family: family?.value,
|
||||
national_code: national_code?.value,
|
||||
gender: gender?.value,
|
||||
insurance_id: insurance_id?.value,
|
||||
basic_insurance: basic_insurance?.value?.id ? [basic_insurance.value.id] : [],
|
||||
supplementary_insurance: supplementary_insurance?.value?.id ? [supplementary_insurance.value.id] : [],
|
||||
};
|
||||
let payload = {};
|
||||
|
||||
// برای PATCH فقط فیلدهای تغییر یافته را ارسال میکنیم
|
||||
if (uuid) {
|
||||
// بررسی تغییرات فیلد به فیلد
|
||||
if (JSON.stringify(prevData.name) !== JSON.stringify(name)) {
|
||||
payload.name = name?.value;
|
||||
}
|
||||
if (JSON.stringify(prevData.family) !== JSON.stringify(family)) {
|
||||
payload.family = family?.value;
|
||||
}
|
||||
if (JSON.stringify(prevData.national_code) !== JSON.stringify(national_code)) {
|
||||
payload.national_code = national_code?.value;
|
||||
}
|
||||
if (JSON.stringify(prevData.gender) !== JSON.stringify(gender)) {
|
||||
payload.gender = gender?.value?.id || gender?.value;
|
||||
}
|
||||
if (JSON.stringify(prevData.insurance_id) !== JSON.stringify(insurance_id)) {
|
||||
payload.insurance_id = insurance_id?.value;
|
||||
}
|
||||
if (JSON.stringify(prevData.basic_insurance) !== JSON.stringify(basic_insurance)) {
|
||||
payload.basic_insurance = basic_insurance?.value?.id ? [basic_insurance.value.id] : [];
|
||||
}
|
||||
if (JSON.stringify(prevData.supplementary_insurance) !== JSON.stringify(supplementary_insurance)) {
|
||||
payload.supplementary_insurance = supplementary_insurance?.value?.id ? [supplementary_insurance.value.id] : [];
|
||||
}
|
||||
} else {
|
||||
// برای POST همه فیلدها را ارسال میکنیم
|
||||
payload = {
|
||||
name: name?.value,
|
||||
family: family?.value,
|
||||
national_code: national_code?.value,
|
||||
gender: gender?.value?.id || gender?.value,
|
||||
insurance_id: insurance_id?.value,
|
||||
basic_insurance: basic_insurance?.value?.id ? [basic_insurance.value.id] : [],
|
||||
supplementary_insurance: supplementary_insurance?.value?.id ? [supplementary_insurance.value.id] : [],
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
if (uuid) {
|
||||
@@ -39,6 +98,10 @@ function SubmitData({ setStep, data, prevData, setErrors }) {
|
||||
await request.postUserProfile(payload);
|
||||
}
|
||||
console.log('✅ پروفایل با موفقیت ذخیره شد');
|
||||
|
||||
// فوراً بعد از موفقیت، refresh token میزنیم
|
||||
await refreshAccessToken();
|
||||
|
||||
setLoading(false);
|
||||
newStep();
|
||||
} catch (error) {
|
||||
|
||||
@@ -13,6 +13,7 @@ const defaultData = {
|
||||
gender: { value: "", isEdit: true },
|
||||
insurance_id: { value: "", isEdit: true },
|
||||
basic_insurance: { value: "", isEdit: true },
|
||||
supplementary_insurance: { value: "", isEdit: true },
|
||||
};
|
||||
|
||||
function AppointmentPage({ doctor, disabledDates, matchedCity }) {
|
||||
@@ -53,23 +54,33 @@ function AppointmentPage({ doctor, disabledDates, matchedCity }) {
|
||||
try {
|
||||
console.log('📤 درخواست دریافت پروفایل کاربر - UUID:', parsedData.uuid);
|
||||
const res = await request.getUserProfile(parsedData.uuid);
|
||||
console.log('📥 پاسخ دریافتی از API:', res);
|
||||
|
||||
if (res) {
|
||||
console.log('🏥 basic_insurance از API:', res.basic_insurance);
|
||||
console.log('🏥 supplementary_insurance از API:', res.supplementary_insurance);
|
||||
const newData = {
|
||||
uuid: res.uuid,
|
||||
phone: { value: usernameFromCookie, isEdit: false },
|
||||
national_code: {
|
||||
value: res.national_code || "",
|
||||
isEdit: res.national_code ? false : true,
|
||||
isEdit: res.national_code ? false : true, // اگر کد ملی داره، غیرقابل ویرایش
|
||||
},
|
||||
name: { value: res.name || "", isEdit: true },
|
||||
family: { value: res.family || "", isEdit: true },
|
||||
gender: { value: res.gender || "", 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,
|
||||
},
|
||||
};
|
||||
setData(newData);
|
||||
setPrevData(newData);
|
||||
@@ -85,6 +96,7 @@ function AppointmentPage({ doctor, disabledDates, matchedCity }) {
|
||||
gender: { value: "", isEdit: true },
|
||||
insurance_id: { value: "", isEdit: true },
|
||||
basic_insurance: { value: "", isEdit: true },
|
||||
supplementary_insurance: { value: "", isEdit: true },
|
||||
}));
|
||||
}
|
||||
} finally {
|
||||
@@ -121,12 +133,19 @@ function AppointmentPage({ doctor, disabledDates, matchedCity }) {
|
||||
},
|
||||
name: { value: res.name || "", isEdit: true },
|
||||
family: { value: res.family || "", isEdit: true },
|
||||
gender: { value: res.gender || "", 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,
|
||||
},
|
||||
};
|
||||
setData(newData);
|
||||
setPrevData(newData);
|
||||
@@ -142,6 +161,7 @@ function AppointmentPage({ doctor, disabledDates, matchedCity }) {
|
||||
gender: { value: "", isEdit: true },
|
||||
insurance_id: { value: "", isEdit: true },
|
||||
basic_insurance: { value: "", isEdit: true },
|
||||
supplementary_insurance: { value: "", isEdit: true },
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user