Files
nobat724_front/components/appointment/index.js
T
hamedandClaude Opus 4.8 59d29cd7c7 fix(appointment): correct payment flow and account-info prefill
- Booking response is double-nested: read appointment uuid/expires_at from
  res.data.data so the payment countdown and gateway redirect actually fire.
- Payment result page (/payment/[uuid]): unwrap res.data.data, use real
  backend fields (amount_rials, gateway, created_at, type) and statuses
  (pending/success/failed/canceled/refunded); the "pay" button now re-initiates
  via postAppointmentPayment instead of building a URL on the API origin.
- Add /payment/result interstitial that reads payment_uuid from the gateway
  callback and forwards to /payment/[uuid].
- Prefill account info correctly in the booking form: name from profile.label,
  insurances from *_id, gender as string, national_code editable unless approved.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-16 10:06:56 +03:30

175 lines
5.5 KiB
JavaScript

"use client";
import { useEffect, useState } from "react";
import { request } from "@/services/response";
import Cookies from "js-cookie";
import Container from "./Container";
import { safeJsonParse } from "@/lib/sanitize";
const defaultData = {
phone: { value: "", isEdit: false },
national_code: { value: "", isEdit: true },
name: { value: "", isEdit: true },
family: { value: "", isEdit: true },
gender: { value: "", isEdit: true },
insurance_id: { value: "", isEdit: true },
basic_insurance: { value: "", isEdit: true },
supplementary_insurance: { value: "", isEdit: true },
};
function buildProfileData(profile, phone) {
return {
uuid: profile.uuid,
phone: { value: phone, isEdit: false },
national_code: {
value: profile.national_code || "",
isEdit: !profile.national_code_approved,
},
name: { value: profile.label || "", isEdit: true },
family: { value: profile.family || "", isEdit: true },
gender: { value: profile.gender || "", isEdit: true },
insurance_id: { value: profile.insurance_id || "", isEdit: true },
basic_insurance: {
value: profile.basic_insurance_id ? { id: profile.basic_insurance_id } : "",
isEdit: true,
},
supplementary_insurance: {
value: profile.supplementary_insurance_id
? { id: profile.supplementary_insurance_id }
: "",
isEdit: true,
},
};
}
function AppointmentPage({ doctor, disabledDates, matchedCity }) {
const [step, setStep] = useState(0);
const [isForAnother, setIsForAnother] = useState(false);
const [isLoading, setIsLoading] = useState(true);
const [prevData, setPrevData] = useState(defaultData);
const [data, setData] = useState(defaultData);
// Login states
const [num, setNum] = useState("");
const [uuid, setUuid] = useState("");
const [isSendMsg, setIsSendMsg] = useState(false);
// Appointment slot states
const [selectedSlot, setSelectedSlot] = useState(null);
const [selectedDate, setSelectedDate] = useState(null);
const [appointmentId, setAppointmentId] = useState(null);
const [appointmentExpiresAt, setAppointmentExpiresAt] = useState(null);
useEffect(() => {
const fetchUserData = async () => {
setIsLoading(true);
const userInfo = Cookies.get("userInfo");
const parsedData = safeJsonParse(userInfo);
const usernameFromCookie = parsedData?.username || "";
// ابتدا شماره موبایل را از Cookie ست می‌کنیم
if (usernameFromCookie) {
setData(prev => ({
...prev,
phone: { value: usernameFromCookie, isEdit: false },
}));
}
// سپس اگر uuid داریم، بقیه اطلاعات را از API می‌گیریم
if (userInfo && parsedData) {
try {
const res = await request.getUserProfile(parsedData.uuid);
const profile = res?.data?.data;
if (profile) {
const newData = buildProfileData(profile, usernameFromCookie);
setData(newData);
setPrevData(newData);
}
} catch (error) {
// در صورت خطا، شماره موبایل حفظ می‌شود و بقیه فیلدها قابل ویرایش می‌مانند
} finally {
setIsLoading(false);
}
} else {
setIsLoading(false);
}
};
fetchUserData();
}, []); // فقط یک بار در mount اولیه اجرا می‌شه
// useEffect جداگانه برای زمانی که step تغییر می‌کنه
useEffect(() => {
const refetchUserData = async () => {
if (step === 3) {
const userInfo = Cookies.get("userInfo");
const parsedData = safeJsonParse(userInfo);
const usernameFromCookie = parsedData?.username || "";
if (userInfo && parsedData && !data.uuid) {
try {
const res = await request.getUserProfile(parsedData.uuid);
const profile = res?.data?.data;
if (profile) {
const newData = buildProfileData(profile, usernameFromCookie);
setData(newData);
setPrevData(newData);
}
} catch (error) {
// در صورت خطا، فرم خالی قابل ویرایش باقی می‌ماند
}
}
}
};
refetchUserData();
}, [step]);
if (isLoading) {
return (
<div className="flex items-center justify-center min-h-screen">
<div className="flex flex-col items-center gap-4">
<div className="w-12 h-12 border-4 border-[#5559CE] border-t-transparent rounded-full animate-spin"></div>
<p className="text-[#3B3B3B] text-[16px]">در حال بارگذاری...</p>
</div>
</div>
);
}
return (
<Container
step={step}
data={data}
doctor={doctor}
setData={setData}
setStep={setStep}
prevData={prevData}
matchedCity={matchedCity}
isForAnother={isForAnother}
setIsForAnother={setIsForAnother}
disabledDates={disabledDates}
num={num}
setNum={setNum}
uuid={uuid}
setUuid={setUuid}
isSendMsg={isSendMsg}
setIsSendMsg={setIsSendMsg}
selectedSlot={selectedSlot}
setSelectedSlot={setSelectedSlot}
selectedDate={selectedDate}
setSelectedDate={setSelectedDate}
appointmentId={appointmentId}
setAppointmentId={setAppointmentId}
appointmentExpiresAt={appointmentExpiresAt}
setAppointmentExpiresAt={setAppointmentExpiresAt}
/>
);
}
export default AppointmentPage;