diff --git a/src/api/history.ts b/src/api/history.ts index 9d5b152..460fad0 100644 --- a/src/api/history.ts +++ b/src/api/history.ts @@ -1,27 +1,26 @@ -import { makeRequest } from "@frontend/kitui"; +import { Tariff, makeRequest } from "@frontend/kitui"; import { parseAxiosError } from "@root/utils/parse-error"; export interface GetHistoryResponse { totalPages: number; - records: { - id: string; - userId: string; - type: string; - isDeleted: boolean; - createdAt: string; - updatedAt: string; - deletedAt: string; - comment: string; - rawDetails: { - tariffs: string[]; - }; - }; + records: HistoryRecord[]; } +type HistoryRecord = { + comment: string; + createdAt: string; + id: string; + isDeleted: boolean; + key: string; + rawDetails: { Key: string; Value: string | number }[]; + updatedAt: string; + userId: string; +}; + export async function getHistory(): Promise<[GetHistoryResponse | null, string?]> { try { const historyResponse = await makeRequest({ - url: "https://penahub.gitlab.yandexcloud.net/history?page=1&limit=100", + url: "https://hub.pena.digital/customer/history?page=1&limit=100&type=payCart", method: "get", useToken: true, }); diff --git a/src/api/price.ts b/src/api/price.ts index f6d2019..01849cd 100644 --- a/src/api/price.ts +++ b/src/api/price.ts @@ -4,14 +4,9 @@ import { parseAxiosError } from "@root/utils/parse-error"; import type { GetDiscountsResponse } from "@root/model/discount"; -const apiUrl = - process.env.NODE_ENV === "production" - ? "/price" - : "https://hub.pena.digital/price"; +const apiUrl = process.env.NODE_ENV === "production" ? "/price" : "https://hub.pena.digital/price"; -export async function getDiscounts( - signal: AbortSignal | undefined -): Promise<[GetDiscountsResponse | null, string?]> { +export async function getDiscounts(signal: AbortSignal | undefined): Promise<[GetDiscountsResponse | null, string?]> { try { const discountsResponse = await makeRequest({ url: apiUrl + "/discounts", diff --git a/src/components/ProtectedLayout.tsx b/src/components/ProtectedLayout.tsx index 3bdb3c8..dc40a05 100644 --- a/src/components/ProtectedLayout.tsx +++ b/src/components/ProtectedLayout.tsx @@ -1,6 +1,14 @@ import { Outlet } from "react-router-dom"; import Navbar from "./Navbar/Navbar"; -import { Ticket, getMessageFromFetchError, useAllTariffsFetcher, usePrivilegeFetcher, useSSESubscription, useTicketsFetcher, useToken } from "@frontend/kitui"; +import { + Ticket, + getMessageFromFetchError, + useAllTariffsFetcher, + usePrivilegeFetcher, + useSSESubscription, + useTicketsFetcher, + useToken, +} from "@frontend/kitui"; import { updateTickets, setTicketCount, useTicketStore, setTicketsFetchState } from "@root/stores/tickets"; import { enqueueSnackbar } from "notistack"; import { updateTariffs } from "@root/stores/tariffs"; @@ -10,69 +18,68 @@ import { useDiscounts } from "@root/utils/hooks/useDiscounts"; import { setDiscounts } from "@root/stores/discounts"; import { setPrivileges } from "@root/stores/privileges"; - export default function ProtectedLayout() { - const token = useToken(); - const ticketApiPage = useTicketStore((state) => state.apiPage); - const ticketsPerPage = useTicketStore((state) => state.ticketsPerPage); + const token = useToken(); + const ticketApiPage = useTicketStore((state) => state.apiPage); + const ticketsPerPage = useTicketStore((state) => state.ticketsPerPage); - useSSESubscription({ - url: `https://hub.pena.digital/heruvym/subscribe?Authorization=${token}`, - onNewData: data => { - updateTickets(data.filter(d => Boolean(d.id))); - setTicketCount(data.length); - }, - marker: "ticket", - }); + useSSESubscription({ + url: `https://hub.pena.digital/heruvym/subscribe?Authorization=${token}`, + onNewData: (data) => { + updateTickets(data.filter((d) => Boolean(d.id))); + setTicketCount(data.length); + }, + marker: "ticket", + }); - useTicketsFetcher({ - url: "https://hub.pena.digital/heruvym/getTickets", - ticketsPerPage, - ticketApiPage, - onSuccess: (result) => { - if (result.data) updateTickets(result.data); - setTicketCount(result.count); - }, - onError: (error: Error) => { - const message = getMessageFromFetchError(error); - if (message) enqueueSnackbar(message); - }, - onFetchStateChange: setTicketsFetchState, - }); + useTicketsFetcher({ + url: "https://hub.pena.digital/heruvym/getTickets", + ticketsPerPage, + ticketApiPage, + onSuccess: (result) => { + if (result.data) updateTickets(result.data); + setTicketCount(result.count); + }, + onError: (error: Error) => { + const message = getMessageFromFetchError(error); + if (message) enqueueSnackbar(message); + }, + onFetchStateChange: setTicketsFetchState, + }); - useAllTariffsFetcher({ - onSuccess: updateTariffs, - onError: (error) => { - const errorMessage = getMessageFromFetchError(error); - if (errorMessage) enqueueSnackbar(errorMessage); - }, - }); + useAllTariffsFetcher({ + onSuccess: updateTariffs, + onError: (error) => { + const errorMessage = getMessageFromFetchError(error); + if (errorMessage) enqueueSnackbar(errorMessage); + }, + }); - useCustomTariffs({ - onNewUser: setCustomTariffs, - onError: (error) => { - if (error) enqueueSnackbar(error); - }, - }); + useCustomTariffs({ + onNewUser: setCustomTariffs, + onError: (error) => { + if (error) enqueueSnackbar(error); + }, + }); - useDiscounts({ - onNewDiscounts: setDiscounts, - onError: (error) => { - const message = getMessageFromFetchError(error); - if (message) enqueueSnackbar(message); - }, - }); + useDiscounts({ + onNewDiscounts: setDiscounts, + onError: (error) => { + const message = getMessageFromFetchError(error); + if (message) enqueueSnackbar(message); + }, + }); - usePrivilegeFetcher({ - onSuccess: setPrivileges, - onError: (error) => { - console.log("usePrivilegeFetcher error :>> ", error); - }, - }); + usePrivilegeFetcher({ + onSuccess: setPrivileges, + onError: (error) => { + console.log("usePrivilegeFetcher error :>> ", error); + }, + }); - return ( - - - - ); + return ( + + + + ); } diff --git a/src/pages/History/index.tsx b/src/pages/History/index.tsx index 32e7e67..e6ce43e 100644 --- a/src/pages/History/index.tsx +++ b/src/pages/History/index.tsx @@ -9,9 +9,9 @@ import { Tabs } from "@root/components/Tabs"; import AccordionWrapper from "./AccordionWrapper"; import { HISTORY } from "./historyMocks"; import { useHistoryTracker } from "@root/utils/hooks/useHistoryTracker"; -import { makeRequest } from "@frontend/kitui"; +import { makeRequest, useToken } from "@frontend/kitui"; import axios from "axios"; -import { GetHistoryResponse, getHistory } from "@root/api/history"; +import { getHistory } from "@root/api/history"; import { useHistoryData } from "@root/utils/hooks/useHistoryData"; const subPages = ["Платежи", "Покупки тарифов", "Окончания тарифов"]; @@ -23,10 +23,13 @@ export default function History() { const upMd = useMediaQuery(theme.breakpoints.up("md")); const isMobile = useMediaQuery(theme.breakpoints.down(600)); const isTablet = useMediaQuery(theme.breakpoints.down(1000)); + const { historyData, error } = useHistoryData(); const handleCustomBackNavigation = useHistoryTracker(); - const { historyResponse, historyTariffsArray, error } = useHistoryData(); + if (!error && historyData) { + console.log(historyData?.records[0].rawDetails.map((rawDetails) => console.log(rawDetails.Key))); + } return ( { - const [historyResponse, setHistoryResponse] = useState(null); - const [historyTariffsArray, setHistoryTariffsArray] = useState([]); + const [historyData, setHistoryData] = useState(null); const [error, setError] = useState(null); useEffect(() => { async function fetchData() { try { - const [historyData, errorMsg] = await getHistory(); + const [response, errorMsg] = await getHistory(); if (errorMsg) { setError(errorMsg); - } else { - setHistoryResponse(historyData); + } + if (response) { + setHistoryData(response); } } catch (err) { console.error("Произошла ошибка при вызове getHistory:", err); @@ -26,32 +24,5 @@ export const useHistoryData = () => { fetchData(); }, []); - useEffect(() => { - if (historyResponse) { - const tariffPromises = historyResponse.records.rawDetails.tariffs.map(async (tariff) => { - const id = tariff; - - try { - const response = await axios.get(`https://hub.pena.digital/strator/tariff/${id}`); - - return response.data; - } catch (error) { - console.error(`Ошибка при выполнении запроса для id ${id}:`, error); - return null; - } - }); - - Promise.all(tariffPromises) - .then((results) => { - const filteredResults = results.filter((result) => result !== null); - - setHistoryTariffsArray(filteredResults); - }) - .catch((error) => { - console.error("Ошибка при выполнении одного из GET-запросов:", error); - }); - } - }, [historyResponse]); - - return { historyResponse, historyTariffsArray, error }; + return { historyData, error }; };