diff --git a/src/api/recentlyPurchasedTariffs.ts b/src/api/recentlyPurchasedTariffs.ts new file mode 100644 index 0000000..b3405af --- /dev/null +++ b/src/api/recentlyPurchasedTariffs.ts @@ -0,0 +1,18 @@ +import { makeRequest } from "@frontend/kitui" +import { parseAxiosError } from "@root/utils/parse-error" + +export async function getRecentlyPurchasedTariffs(): Promise<[any | null, string?]> { + try { + const recentlyPurchased = await makeRequest({ + url: "https://hub.pena.digital/customer/recent", + method: "get", + useToken: true, + }) + console.log(recentlyPurchased) + return [recentlyPurchased] + } catch (nativeError) { + const [error] = parseAxiosError(nativeError) + + return [null, `Не удалось получить историю. ${error}`] + } +} \ No newline at end of file diff --git a/src/pages/Tariffs/TariffsPage.tsx b/src/pages/Tariffs/TariffsPage.tsx index 730a568..eea25dd 100644 --- a/src/pages/Tariffs/TariffsPage.tsx +++ b/src/pages/Tariffs/TariffsPage.tsx @@ -21,6 +21,7 @@ import { usePrevLocation } from "@root/utils/hooks/handleCustomBackNavigation"; import { withErrorBoundary } from "react-error-boundary"; import { handleComponentError } from "@root/utils/handleComponentError"; import { useHistoryStore } from "@root/stores/history"; +import {useRecentlyPurchasedTariffs} from "@utils/hooks/useRecentlyPurchasedTariffs" const subPages = ["Шаблонизатор"/* , "Опросник", "Сокращатель ссылок" */]; @@ -43,6 +44,7 @@ function TariffPage() { const isUserNko = useUserStore(state => state.userAccount?.status) === "nko"; const historyData = useHistoryStore(state => state.history); + const {recentlyPurchased} = useRecentlyPurchasedTariffs() const handleCustomBackNavigation = usePrevLocation(location); const unit: string = String(location.pathname).slice(9); @@ -181,7 +183,7 @@ function TariffPage() { > {createTariffElements(filteredTariffs, true)} - {tariffsFromHistory.length > 0 && ( + {recentlyPurchased.length > 0 && ( <> Ранее вы - + )} diff --git a/src/utils/hooks/useRecentlyPurchasedTariffs.ts b/src/utils/hooks/useRecentlyPurchasedTariffs.ts new file mode 100644 index 0000000..138d6c3 --- /dev/null +++ b/src/utils/hooks/useRecentlyPurchasedTariffs.ts @@ -0,0 +1,32 @@ +import {useEffect, useState} from "react" +import { useTariffStore } from "@root/stores/tariffs"; +import {getRecentlyPurchasedTariffs} from "@root/api/recentlyPurchasedTariffs" +import { getTariffById } from "@root/api/tariff" +import {useHistoryStore} from "@stores/history" + +export const useRecentlyPurchasedTariffs = () => { + const [recentlyPurchased, setRecentlyPurchased] = useState([]) + const tariffs = useTariffStore((state) => state.tariffs); + const historyData = useHistoryStore(state => state.history); + useEffect(() => { + async function fetchData() { + try { + const [response, errorMsg] = await getRecentlyPurchasedTariffs(); + + if (errorMsg) { + console.error("Произошла ошибка при вызове getRecentlyPurchasedTariffs:", errorMsg); + } + if (response) { + const recentlyTariffs = response.map((obj: { id: string })=>obj.id) + setRecentlyPurchased(tariffs.filter((tariffs)=>{ + return recentlyTariffs.includes(tariffs._id)})); + } + } catch (err) { + console.error("Произошла ошибка при вызове getRecentlyPurchasedTariffs:", err); + } + } + + fetchData(); + }, []); + return {recentlyPurchased} +} \ No newline at end of file