failed tests btw
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user