From 523ed19e44a3a009823f74c8953a1295e3df3092 Mon Sep 17 00:00:00 2001 From: "dev.daminik00" Date: Mon, 6 Oct 2025 19:30:26 +0200 Subject: [PATCH] disable auto topup after fail --- src/hooks/chats/useChatSocket.ts | 36 +++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/src/hooks/chats/useChatSocket.ts b/src/hooks/chats/useChatSocket.ts index 29efd5d..47b85c4 100644 --- a/src/hooks/chats/useChatSocket.ts +++ b/src/hooks/chats/useChatSocket.ts @@ -2,7 +2,11 @@ import { useCallback, useEffect, useMemo, useRef, useState } from "react"; -import { fetchChatMessages } from "@/entities/chats/actions"; +import { + fetchChatMessages, + fetchMyChatSettings, + updateChatSettings, +} from "@/entities/chats/actions"; import type { IChatMessage } from "@/entities/chats/types"; import { useSingleCheckout } from "@/hooks/payment/useSingleCheckout"; import { useSocketEvent } from "@/hooks/socket/useSocketEvent"; @@ -126,17 +130,33 @@ export const useChatSocket = ( const { handleSingleCheckout, isLoading: isAutoTopUpLoading } = useSingleCheckout({ onSuccess: fetchBalance, - onError: () => { + onError: async () => { // eslint-disable-next-line no-console - console.error("Auto top-up payment failed"); - // Throttle retries: keep the lock for 30 seconds after an error + console.error("Auto top-up payment failed - disabling auto top-up"); + + // Disable auto top-up on payment failure + try { + // Fetch current settings to preserve topUpAmount + const settingsResponse = await fetchMyChatSettings(); + if (settingsResponse.data?.settings) { + await updateChatSettings({ + ...settingsResponse.data.settings, + autoTopUp: false, + }); + // eslint-disable-next-line no-console + console.info("Auto top-up disabled successfully after payment failure"); + } + } catch (error) { + // eslint-disable-next-line no-console + console.error("Failed to disable auto top-up:", error); + } + + // Release the lock immediately after disabling + autoTopUpInProgressRef.current = false; if (autoTopUpUnlockTimerRef.current) { clearTimeout(autoTopUpUnlockTimerRef.current); - } - autoTopUpUnlockTimerRef.current = setTimeout(() => { - autoTopUpInProgressRef.current = false; autoTopUpUnlockTimerRef.current = null; - }, 30_000); + } }, returnUrl: typeof window !== "undefined" ? window.location.href : "", });