refactor: add doctor, selectedSlot, and selectedDate props to SubmitData component and enhance appointment handling
This commit is contained in:
@@ -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,20 +29,22 @@ 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({});
|
||||
|
||||
if (isChanged) {
|
||||
setLoading(true);
|
||||
|
||||
try {
|
||||
if (isChanged) {
|
||||
const { basic_insurance, supplementary_insurance, name, family, national_code, gender, insurance_id, uuid } = data;
|
||||
|
||||
let payload = {};
|
||||
@@ -84,30 +86,44 @@ function SubmitData({ setStep, data, prevData, setErrors }) {
|
||||
};
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
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);
|
||||
newStep();
|
||||
} catch (error) {
|
||||
setLoading(false);
|
||||
// اگر خطای validation بود، error ها را ست میکنیم
|
||||
if (error?.response?.data) {
|
||||
setErrors(error.response.data);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
newStep();
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user