From 53ac0fea838a3ad566ac57f3a57106834b23e988 Mon Sep 17 00:00:00 2001 From: hamed <15238-genius.ha@users.noreply.drupalcode.org> Date: Tue, 18 Nov 2025 16:36:08 +0330 Subject: [PATCH] refactor: add doctor, selectedSlot, and selectedDate props to SubmitData component and enhance appointment handling --- components/appointment/detail/SubmitData.js | 132 +++++++++++--------- 1 file changed, 74 insertions(+), 58 deletions(-) diff --git a/components/appointment/detail/SubmitData.js b/components/appointment/detail/SubmitData.js index 9089513..8ea4d32 100644 --- a/components/appointment/detail/SubmitData.js +++ b/components/appointment/detail/SubmitData.js @@ -5,7 +5,7 @@ import ArrowLeftB from "@/components/icons/ArrowLeftB"; import { request } from "@/services/response"; import Cookies from "js-cookie"; -function SubmitData({ setStep, data, prevData, setErrors }) { +function SubmitData({ setStep, data, prevData, setErrors, doctor, selectedSlot, selectedDate }) { const [loading, setLoading] = useState(false); const newStep = () => setStep((prev) => prev + 1); @@ -13,7 +13,7 @@ function SubmitData({ setStep, data, prevData, setErrors }) { try { const refreshToken = Cookies.get("refresh_token"); if (!refreshToken) { - return; + return false; } const formData = new URLSearchParams(); @@ -29,84 +29,100 @@ function SubmitData({ setStep, data, prevData, setErrors }) { if (response.refresh_token) { Cookies.set("refresh_token", response.refresh_token, { expires: 7 }); } + return true; } + return false; } catch (error) { - // خطا در refresh token + return false; } }; const handleSubmit = async () => { const isChanged = JSON.stringify(prevData) !== JSON.stringify(data); - // پاک کردن error های قبلی setErrors({}); + setLoading(true); - if (isChanged) { - setLoading(true); - const { basic_insurance, supplementary_insurance, name, family, national_code, gender, insurance_id, uuid } = data; + try { + if (isChanged) { + const { basic_insurance, supplementary_insurance, name, family, national_code, gender, insurance_id, uuid } = data; - let payload = {}; + 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] : [], + }; + } - // برای 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) { - // اگر uuid داریم، پروفایل وجود داره و باید PATCH کنیم await request.patchUserProfile(payload, uuid); } else { - // اگر uuid نداریم، پروفایل وجود نداره و باید POST کنیم await request.postUserProfile(payload); } - // فوراً بعد از موفقیت، refresh token می‌زنیم - await refreshAccessToken(); + const refreshed = await refreshAccessToken(); - setLoading(false); - newStep(); - } catch (error) { - setLoading(false); - // اگر خطای validation بود، error ها را ست می‌کنیم - if (error?.response?.data) { - setErrors(error.response.data); + if (refreshed) { + await new Promise(resolve => setTimeout(resolve, 1000)); } } - } else { + + if (doctor && selectedSlot) { + const appointmentPayload = { + doctor_id: doctor.id, + slot: { + time: selectedSlot.time, + status: selectedSlot.status, + start_time_timestamp: selectedSlot.start_time_timestamp, + end_time_timestamp: selectedSlot.end_time_timestamp, + duration_per_patient: selectedSlot.duration_per_patient, + location_id: selectedSlot.location_id + }, + info: "" + }; + + await request.postAppointment(appointmentPayload); + } + + setLoading(false); newStep(); + } catch (error) { + setLoading(false); + if (error?.response?.data) { + setErrors(error.response.data); + } } };