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