@@ -11,7 +11,7 @@ import { Typography, } from '@mui/material' import Router, { useRouter } from 'next/router' -import { useSession } from 'next-auth/react' +import { getSession, useSession } from 'next-auth/react' import { useAppSelector } from '#/app/store/store' import { CheckBoxAgreeWithRules, Loader, Select } from '#/shared' @@ -66,27 +66,66 @@ export const Payment: React.FC = ({ device, changeClose }) // Загружаем данные только после того, как данные пользователя загружены useEffect(() => { - if (data?.access && meStatus === 'succeeded') { - getPaymentsHistory(data.access).then((res) => setMessages(res)) + if (meStatus === 'succeeded') { + const fetchHistory = async () => { + // Получаем актуальный токен в момент запроса + const session = await getSession() + if (session?.access) { + getPaymentsHistory(session.access).then((res) => setMessages(res)) + } + } + fetchHistory() } - }, [data?.access, meStatus]) + }, [meStatus]) useEffect(() => { - if (data?.access && meStatus === 'succeeded') { - setLoadingOffers(true) - accountApi.getPaymentsPlans(data?.access).then((res) => { - setOffers(res) - if (res && res.length > 0) { - setSelectedOffer(res[0].uid) + if (meStatus === 'succeeded') { + const fetchOffers = async () => { + // Получаем актуальный токен в момент запроса, а не из замыкания + const session = await getSession() + if (!session?.access) { + setLoadingOffers(false) + return + } + + setLoadingOffers(true) + try { + const res = await accountApi.getPaymentsPlans(session.access) + if (res) { + setOffers(res) + if (res.length > 0) { + setSelectedOffer(res[0].uid) + } + } + } catch (error) { + console.error('Error fetching payment plans:', error) + const updatedSession = await getSession() + if (updatedSession?.access) { + const retryRes = await accountApi.getPaymentsPlans(updatedSession.access) + if (retryRes) { + setOffers(retryRes) + if (retryRes.length > 0) { + setSelectedOffer(retryRes[0].uid) + } + } + } + } finally { + setLoadingOffers(false) } - setLoadingOffers(false) - }) + } + + fetchOffers() } - }, [data?.access, meStatus]) + }, [meStatus]) const pay = async (uid: string) => { - const urlForPay = await accountApi.payProduct(data!.access, uid) + // Получаем актуальный токен в момент запроса + const session = await getSession() + if (!session?.access) { + return + } + const urlForPay = await accountApi.payProduct(session.access, uid) urlForPay ? await Router.push(urlForPay) : null } @@ -110,7 +149,7 @@ export const Payment: React.FC = ({ device, changeClose }) paddingTop: desktop ? '15px' : 0, }} > - {offers?.length && offers?.map((offer) => { + {offers?.length ? offers?.map((offer) => { return ( = ({ device, changeClose }) key={offer.uid} /> ) - })} + }) : null} {/* {messages && (