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 { 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,84 +29,100 @@ 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({});
|
||||||
|
setLoading(true);
|
||||||
|
|
||||||
if (isChanged) {
|
try {
|
||||||
setLoading(true);
|
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 = {};
|
||||||
|
|
||||||
|
// برای 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) {
|
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();
|
|
||||||
|
|
||||||
setLoading(false);
|
if (refreshed) {
|
||||||
newStep();
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||||
} catch (error) {
|
|
||||||
setLoading(false);
|
|
||||||
// اگر خطای validation بود، error ها را ست میکنیم
|
|
||||||
if (error?.response?.data) {
|
|
||||||
setErrors(error.response.data);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} 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();
|
newStep();
|
||||||
|
} catch (error) {
|
||||||
|
setLoading(false);
|
||||||
|
if (error?.response?.data) {
|
||||||
|
setErrors(error.response.data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user