- Fix GPS map links always sending literal "latitude"/"longitude" strings instead of actual coordinates in openLocation/Content.js - Add api.clinic-pro.ir to next.config.js remotePatterns so production images load correctly - Fix appointment page: await params and getStateInfo (Next.js 15 pattern) - Enable 401 handling in api.js: clear cookies and redirect to /login - Move OAuth client_secret to server-side API routes (/api/auth/token, /api/auth/refresh) so it is never bundled into client-side JavaScript - Update SendReq, SubmitData, ButtonSendData to call API routes instead of directly sending client_secret from the browser - Update docker-compose.yml to use server-only CLIENT_SECRET env var - Remove debug console.log from clinic doctors list component Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
152 lines
5.1 KiB
JavaScript
152 lines
5.1 KiB
JavaScript
import { useState } from "react";
|
|
import ButtonFixed from "../paying/ButtonFixed";
|
|
import { Button } from "@mui/material";
|
|
import ArrowLeftB from "@/components/icons/ArrowLeftB";
|
|
import { request } from "@/services/response";
|
|
import Cookies from "js-cookie";
|
|
|
|
function SubmitData({ setStep, data, prevData, setErrors, doctor, selectedSlot, selectedDate, setAppointmentId }) {
|
|
const [loading, setLoading] = useState(false);
|
|
const newStep = () => setStep((prev) => prev + 1);
|
|
|
|
const refreshAccessToken = async () => {
|
|
try {
|
|
const refreshToken = Cookies.get("refresh_token");
|
|
if (!refreshToken) return false;
|
|
|
|
const res = await fetch("/api/auth/refresh", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ refresh_token: refreshToken }),
|
|
});
|
|
const response = await res.json();
|
|
|
|
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 });
|
|
}
|
|
return true;
|
|
}
|
|
return false;
|
|
} catch {
|
|
return false;
|
|
}
|
|
};
|
|
|
|
const handleSubmit = async () => {
|
|
const isChanged = JSON.stringify(prevData) !== JSON.stringify(data);
|
|
|
|
setErrors({});
|
|
setLoading(true);
|
|
|
|
try {
|
|
if (isChanged) {
|
|
const { basic_insurance, supplementary_insurance, name, family, national_code, gender, insurance_id, uuid } = data;
|
|
|
|
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] : [],
|
|
};
|
|
}
|
|
|
|
if (uuid) {
|
|
await request.patchUserProfile(payload, uuid);
|
|
} else {
|
|
await request.postUserProfile(payload);
|
|
}
|
|
|
|
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: ""
|
|
};
|
|
|
|
const appointmentResponse = await request.postAppointment(appointmentPayload);
|
|
if (appointmentResponse?.id) {
|
|
setAppointmentId(appointmentResponse.id);
|
|
}
|
|
}
|
|
|
|
setLoading(false);
|
|
newStep();
|
|
} catch (error) {
|
|
setLoading(false);
|
|
if (error?.response?.data) {
|
|
setErrors(error.response.data);
|
|
}
|
|
}
|
|
};
|
|
|
|
return (
|
|
<ButtonFixed>
|
|
<Button
|
|
loading={loading}
|
|
className={`
|
|
!flex !w-full md:!w-fit !mt-0 md:!mt-[32px]
|
|
!gap-[4px] md:!mr-auto !px-[12px] !py-[8px] md:!py-[9px] !min-w-[150px]
|
|
`}
|
|
variant="contained"
|
|
onClick={handleSubmit}
|
|
>
|
|
<p
|
|
className={`${loading && "opacity-0"} text-[#EFEFEF] text-[16px] font-medium`}
|
|
>
|
|
ثبت اطلاعات
|
|
</p>
|
|
<ArrowLeftB />
|
|
</Button>
|
|
</ButtonFixed>
|
|
);
|
|
}
|
|
|
|
export default SubmitData;
|