This commit is contained in:
ArtChaos189 2023-09-16 00:00:02 +03:00
parent aec01d65c3
commit fe5be21e18
5 changed files with 93 additions and 118 deletions

@ -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<never, GetHistoryResponse>({
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,
});

@ -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<never, GetDiscountsResponse>({
url: apiUrl + "/discounts",

@ -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<Ticket>({
url: `https://hub.pena.digital/heruvym/subscribe?Authorization=${token}`,
onNewData: data => {
updateTickets(data.filter(d => Boolean(d.id)));
setTicketCount(data.length);
},
marker: "ticket",
});
useSSESubscription<Ticket>({
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 (
<Navbar>
<Outlet />
</Navbar>
);
return (
<Navbar>
<Outlet />
</Navbar>
);
}

@ -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 (
<SectionWrapper

@ -1,22 +1,20 @@
import { useEffect, useState } from "react";
import axios from "axios";
import { GetHistoryResponse, getHistory } from "@root/api/history";
import { Tariff } from "@frontend/kitui";
export const useHistoryData = () => {
const [historyResponse, setHistoryResponse] = useState<null | GetHistoryResponse>(null);
const [historyTariffsArray, setHistoryTariffsArray] = useState<Tariff[]>([]);
const [historyData, setHistoryData] = useState<GetHistoryResponse | null>(null);
const [error, setError] = useState<null | string>(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 };
};