From df54983fa3b3c2cff9bc84be15c3747796fb000c Mon Sep 17 00:00:00 2001
From: hamed <15238-genius.ha@users.noreply.drupalcode.org>
Date: Tue, 18 Nov 2025 17:09:37 +0330
Subject: [PATCH] refactor: enhance appointment handling by adding
appointmentId state and updating payment process
---
components/appointment/Container.js | 5 +-
components/appointment/detail/SubmitData.js | 7 +-
components/appointment/detail/index.js | 10 +-
components/appointment/index.js | 3 +
components/appointment/paying/index.js | 121 +++++++++++++++++---
services/response.js | 1 +
6 files changed, 126 insertions(+), 21 deletions(-)
diff --git a/components/appointment/Container.js b/components/appointment/Container.js
index bfa0845..f2e1ab7 100644
--- a/components/appointment/Container.js
+++ b/components/appointment/Container.js
@@ -34,6 +34,8 @@ function Container({
setSelectedSlot,
selectedDate,
setSelectedDate,
+ appointmentId,
+ setAppointmentId,
}) {
const elements = [
,
- ,
+ ,
,
,
];
diff --git a/components/appointment/detail/SubmitData.js b/components/appointment/detail/SubmitData.js
index 8ea4d32..fe8d041 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, doctor, selectedSlot, selectedDate }) {
+function SubmitData({ setStep, data, prevData, setErrors, doctor, selectedSlot, selectedDate, setAppointmentId }) {
const [loading, setLoading] = useState(false);
const newStep = () => setStep((prev) => prev + 1);
@@ -113,7 +113,10 @@ function SubmitData({ setStep, data, prevData, setErrors, doctor, selectedSlot,
info: ""
};
- await request.postAppointment(appointmentPayload);
+ const appointmentResponse = await request.postAppointment(appointmentPayload);
+ if (appointmentResponse?.id) {
+ setAppointmentId(appointmentResponse.id);
+ }
}
setLoading(false);
diff --git a/components/appointment/detail/index.js b/components/appointment/detail/index.js
index 810bf1f..d43854c 100644
--- a/components/appointment/detail/index.js
+++ b/components/appointment/detail/index.js
@@ -18,6 +18,7 @@ function Detail({
doctor,
selectedSlot,
selectedDate,
+ setAppointmentId,
}) {
const [insurance, setInsurance] = useState();
const [supplementaryInsurance, setSupplementaryInsurance] = useState();
@@ -93,14 +94,15 @@ function Detail({
)}
-
diff --git a/components/appointment/index.js b/components/appointment/index.js
index 49b222c..b2c848e 100644
--- a/components/appointment/index.js
+++ b/components/appointment/index.js
@@ -32,6 +32,7 @@ function AppointmentPage({ doctor, disabledDates, matchedCity }) {
// Appointment slot states
const [selectedSlot, setSelectedSlot] = useState(null);
const [selectedDate, setSelectedDate] = useState(null);
+ const [appointmentId, setAppointmentId] = useState(null);
useEffect(() => {
const fetchUserData = async () => {
@@ -201,6 +202,8 @@ function AppointmentPage({ doctor, disabledDates, matchedCity }) {
setSelectedSlot={setSelectedSlot}
selectedDate={selectedDate}
setSelectedDate={setSelectedDate}
+ appointmentId={appointmentId}
+ setAppointmentId={setAppointmentId}
/>
);
}
diff --git a/components/appointment/paying/index.js b/components/appointment/paying/index.js
index 2e23057..8453e1e 100644
--- a/components/appointment/paying/index.js
+++ b/components/appointment/paying/index.js
@@ -1,8 +1,70 @@
+import { useState, useEffect } from "react";
import ArrowLeftB from "@/components/icons/ArrowLeftB";
-import { Button } from "@mui/material";
+import { Button, MenuItem, Select, FormControl, InputLabel } from "@mui/material";
import ButtonFixed from "./ButtonFixed";
+import { request } from "@/services/response";
+
+function Paying({ setStep, appointmentId }) {
+ const [selectedBank, setSelectedBank] = useState("");
+ const [loading, setLoading] = useState(false);
+ const [timeLeft, setTimeLeft] = useState(600); // 10 minutes in seconds
+
+ const banks = [
+ { id: "mellat", title: "بانک ملت" },
+ { id: "saman", title: "سامان کیش" }
+ ];
+
+ useEffect(() => {
+ if (timeLeft === 0) return;
+
+ const timer = setInterval(() => {
+ setTimeLeft((prev) => prev - 1);
+ }, 1000);
+
+ return () => clearInterval(timer);
+ }, [timeLeft]);
+
+ const formatTime = (seconds) => {
+ const minutes = Math.floor(seconds / 60);
+ const secs = seconds % 60;
+ return `${minutes}:${secs.toString().padStart(2, '0')}`;
+ };
+
+ const handlePayment = async () => {
+ if (!selectedBank) {
+ alert("لطفا بانک خود را انتخاب کنید");
+ return;
+ }
+
+ if (!appointmentId) {
+ alert("شناسه نوبت یافت نشد");
+ return;
+ }
+
+ setLoading(true);
+ try {
+ const paymentPayload = {
+ payment_method: selectedBank,
+ bundle: "appointment",
+ entity_reference_id: appointmentId
+ };
+
+ const response = await request.postPayment(paymentPayload);
+
+ // Redirect to payment gateway
+ if (response?.uuid) {
+ const paymentUrl = `${process.env.NEXT_PUBLIC_API_URL}/payment/${response.uuid}`;
+ window.location.href = paymentUrl;
+ } else {
+ setStep((prev) => prev + 1);
+ }
+ } catch (error) {
+ console.error("Payment error:", error);
+ alert("خطا در پرداخت");
+ setLoading(false);
+ }
+ };
-function Paying({ setTypePay, fadeElement, setStep }) {
return (
اطلاعات پرداخت
-
-
- مبلغ پیش پرداخت (بیعانه) :{" "}
-
-
- 20،000 تومان
-
+
+
+
+
+ زمان باقیمانده:
+
+
+ {formatTime(timeLeft)}
+
+
+
+
+ انتخاب بانک
+
+
+
+ {/*
+
+ مبلغ پیش پرداخت (بیعانه) :
+
+
+ 10،000 تومان
+
+
*/}
+
+
- مبلغ قابل پرداخت : 20،000 تومان
+ مبلغ قابل پرداخت : 10،000 تومان
diff --git a/services/response.js b/services/response.js
index 4074b45..c6dc2df 100644
--- a/services/response.js
+++ b/services/response.js
@@ -67,6 +67,7 @@ export const request = {
removeTokenHead
),
postAppointment: (data) => api.post(`api/v1/appointment`, data, { requireAuth: true }),
+ postPayment: (data) => api.post(`api/v1/payment`, data, { requireAuth: true }),
postDoctor: (data) => api.post("api/v1/doctor", data),
postImageUpload: (data) =>
api.post("file/upload/clinic_pro/doctor/field_image", data, {