refactor: add doctor, selectedSlot, and selectedDate props to SubmitData component and enhance appointment handling

This commit is contained in:
hamed
2025-11-18 16:36:08 +03:30
parent 6d6d27bcc9
commit 53ac0fea83
+31 -15
View File
@@ -5,7 +5,7 @@ import ArrowLeftB from "@/components/icons/ArrowLeftB";
import { request } from "@/services/response"; import { request } from "@/services/response";
import Cookies from "js-cookie"; 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 [loading, setLoading] = useState(false);
const newStep = () => setStep((prev) => prev + 1); const newStep = () => setStep((prev) => prev + 1);
@@ -13,7 +13,7 @@ function SubmitData({ setStep, data, prevData, setErrors }) {
try { try {
const refreshToken = Cookies.get("refresh_token"); const refreshToken = Cookies.get("refresh_token");
if (!refreshToken) { if (!refreshToken) {
return; return false;
} }
const formData = new URLSearchParams(); const formData = new URLSearchParams();
@@ -29,20 +29,22 @@ function SubmitData({ setStep, data, prevData, setErrors }) {
if (response.refresh_token) { if (response.refresh_token) {
Cookies.set("refresh_token", response.refresh_token, { expires: 7 }); Cookies.set("refresh_token", response.refresh_token, { expires: 7 });
} }
return true;
} }
return false;
} catch (error) { } catch (error) {
// خطا در refresh token return false;
} }
}; };
const handleSubmit = async () => { const handleSubmit = async () => {
const isChanged = JSON.stringify(prevData) !== JSON.stringify(data); const isChanged = JSON.stringify(prevData) !== JSON.stringify(data);
// پاک کردن error های قبلی
setErrors({}); setErrors({});
if (isChanged) {
setLoading(true); setLoading(true);
try {
if (isChanged) {
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;
let payload = {}; let payload = {};
@@ -84,30 +86,44 @@ function SubmitData({ setStep, data, prevData, setErrors }) {
}; };
} }
try {
if (uuid) { if (uuid) {
// اگر uuid داریم، پروفایل وجود داره و باید PATCH کنیم
await request.patchUserProfile(payload, uuid); await request.patchUserProfile(payload, uuid);
} else { } else {
// اگر uuid نداریم، پروفایل وجود نداره و باید POST کنیم
await request.postUserProfile(payload); await request.postUserProfile(payload);
} }
// فوراً بعد از موفقیت، refresh token می‌زنیم const refreshed = await refreshAccessToken();
await refreshAccessToken();
if (refreshed) {
await new Promise(resolve => setTimeout(resolve, 1000));
}
}
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); setLoading(false);
newStep(); newStep();
} catch (error) { } catch (error) {
setLoading(false); setLoading(false);
// اگر خطای validation بود، error ها را ست می‌کنیم
if (error?.response?.data) { if (error?.response?.data) {
setErrors(error.response.data); setErrors(error.response.data);
} }
} }
} else {
newStep();
}
}; };
return ( return (