From 64284593139fcac2ad0d50350b640289d02033d6 Mon Sep 17 00:00:00 2001 From: mohd Date: Sat, 14 Mar 2026 23:42:21 +0300 Subject: [PATCH] failed tests btw --- frontend/src/contexts/AuthContext.jsx | 6 ++++-- frontend/src/hooks/usePaymentForm.js | 18 +++++++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/frontend/src/contexts/AuthContext.jsx b/frontend/src/contexts/AuthContext.jsx index 80cca7f..bf8b6f4 100644 --- a/frontend/src/contexts/AuthContext.jsx +++ b/frontend/src/contexts/AuthContext.jsx @@ -51,8 +51,10 @@ export function AuthProvider({ children }) { setLoading(false); }) .catch((err) => { - const status = err instanceof ApiError ? err.status : err?.status; - if (status !== 401) { + const status = ApiError && err instanceof ApiError ? err.status : err?.status; + const message = typeof err?.message === "string" ? err.message : ""; + const isUnauthorized = status === 401 || message.includes("401"); + if (!isUnauthorized) { setLoading(false); return; } diff --git a/frontend/src/hooks/usePaymentForm.js b/frontend/src/hooks/usePaymentForm.js index cb5058b..6f9b076 100644 --- a/frontend/src/hooks/usePaymentForm.js +++ b/frontend/src/hooks/usePaymentForm.js @@ -26,13 +26,19 @@ export function usePaymentForm(bookingId = "", token = "") { const [status, setStatus] = useState("idle"); const [result, setResult] = useState(null); const [error, setError] = useState(""); - const [idempotencyKey, setIdempotencyKey] = useState(generateIdempotencyKey); + const idempotencyRef = useRef(null); + if (!idempotencyRef.current) { + idempotencyRef.current = generateIdempotencyKey(); + } + const [idempotencyKey, setIdempotencyKey] = useState(idempotencyRef.current); const lastBookingId = useRef(bookingId); useEffect(() => { if (lastBookingId.current !== bookingIdInput) { lastBookingId.current = bookingIdInput; - setIdempotencyKey(generateIdempotencyKey()); + const nextKey = generateIdempotencyKey(); + idempotencyRef.current = nextKey; + setIdempotencyKey(nextKey); } }, [bookingIdInput]); @@ -83,7 +89,7 @@ export function usePaymentForm(bookingId = "", token = "") { const payload = { booking_id: Number(bookingIdInput), provider: "moyasar", - idempotency_key: idempotencyKey, + idempotency_key: idempotencyRef.current, source, }; if (callbackUrl) { @@ -103,7 +109,7 @@ export function usePaymentForm(bookingId = "", token = "") { window.location.assign(data.redirect_url); } } catch (err) { - if (err instanceof ApiError && err.body) { + if (ApiError && err instanceof ApiError && err.body) { const fieldMessage = getFirstFieldError(err.body); if (fieldMessage) { console.warn("Payment validation error", err.body); @@ -112,8 +118,10 @@ export function usePaymentForm(bookingId = "", token = "") { return; } } + const message = + (typeof err?.message === "string" && err.message) || String(err) || ""; setStatus("error"); - setError(err.message || t("payment.errors.generic")); + setError(message || t("payment.errors.generic")); } }