history
This commit is contained in:
parent
aec01d65c3
commit
fe5be21e18
@ -1,27 +1,26 @@
|
|||||||
import { makeRequest } from "@frontend/kitui";
|
import { Tariff, makeRequest } from "@frontend/kitui";
|
||||||
import { parseAxiosError } from "@root/utils/parse-error";
|
import { parseAxiosError } from "@root/utils/parse-error";
|
||||||
|
|
||||||
export interface GetHistoryResponse {
|
export interface GetHistoryResponse {
|
||||||
totalPages: number;
|
totalPages: number;
|
||||||
records: {
|
records: HistoryRecord[];
|
||||||
id: string;
|
|
||||||
userId: string;
|
|
||||||
type: string;
|
|
||||||
isDeleted: boolean;
|
|
||||||
createdAt: string;
|
|
||||||
updatedAt: string;
|
|
||||||
deletedAt: string;
|
|
||||||
comment: string;
|
|
||||||
rawDetails: {
|
|
||||||
tariffs: string[];
|
|
||||||
};
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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?]> {
|
export async function getHistory(): Promise<[GetHistoryResponse | null, string?]> {
|
||||||
try {
|
try {
|
||||||
const historyResponse = await makeRequest<never, GetHistoryResponse>({
|
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",
|
method: "get",
|
||||||
useToken: true,
|
useToken: true,
|
||||||
});
|
});
|
||||||
|
@ -4,14 +4,9 @@ import { parseAxiosError } from "@root/utils/parse-error";
|
|||||||
|
|
||||||
import type { GetDiscountsResponse } from "@root/model/discount";
|
import type { GetDiscountsResponse } from "@root/model/discount";
|
||||||
|
|
||||||
const apiUrl =
|
const apiUrl = process.env.NODE_ENV === "production" ? "/price" : "https://hub.pena.digital/price";
|
||||||
process.env.NODE_ENV === "production"
|
|
||||||
? "/price"
|
|
||||||
: "https://hub.pena.digital/price";
|
|
||||||
|
|
||||||
export async function getDiscounts(
|
export async function getDiscounts(signal: AbortSignal | undefined): Promise<[GetDiscountsResponse | null, string?]> {
|
||||||
signal: AbortSignal | undefined
|
|
||||||
): Promise<[GetDiscountsResponse | null, string?]> {
|
|
||||||
try {
|
try {
|
||||||
const discountsResponse = await makeRequest<never, GetDiscountsResponse>({
|
const discountsResponse = await makeRequest<never, GetDiscountsResponse>({
|
||||||
url: apiUrl + "/discounts",
|
url: apiUrl + "/discounts",
|
||||||
|
@ -1,6 +1,14 @@
|
|||||||
import { Outlet } from "react-router-dom";
|
import { Outlet } from "react-router-dom";
|
||||||
import Navbar from "./Navbar/Navbar";
|
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 { updateTickets, setTicketCount, useTicketStore, setTicketsFetchState } from "@root/stores/tickets";
|
||||||
import { enqueueSnackbar } from "notistack";
|
import { enqueueSnackbar } from "notistack";
|
||||||
import { updateTariffs } from "@root/stores/tariffs";
|
import { updateTariffs } from "@root/stores/tariffs";
|
||||||
@ -10,69 +18,68 @@ import { useDiscounts } from "@root/utils/hooks/useDiscounts";
|
|||||||
import { setDiscounts } from "@root/stores/discounts";
|
import { setDiscounts } from "@root/stores/discounts";
|
||||||
import { setPrivileges } from "@root/stores/privileges";
|
import { setPrivileges } from "@root/stores/privileges";
|
||||||
|
|
||||||
|
|
||||||
export default function ProtectedLayout() {
|
export default function ProtectedLayout() {
|
||||||
const token = useToken();
|
const token = useToken();
|
||||||
const ticketApiPage = useTicketStore((state) => state.apiPage);
|
const ticketApiPage = useTicketStore((state) => state.apiPage);
|
||||||
const ticketsPerPage = useTicketStore((state) => state.ticketsPerPage);
|
const ticketsPerPage = useTicketStore((state) => state.ticketsPerPage);
|
||||||
|
|
||||||
useSSESubscription<Ticket>({
|
useSSESubscription<Ticket>({
|
||||||
url: `https://hub.pena.digital/heruvym/subscribe?Authorization=${token}`,
|
url: `https://hub.pena.digital/heruvym/subscribe?Authorization=${token}`,
|
||||||
onNewData: data => {
|
onNewData: (data) => {
|
||||||
updateTickets(data.filter(d => Boolean(d.id)));
|
updateTickets(data.filter((d) => Boolean(d.id)));
|
||||||
setTicketCount(data.length);
|
setTicketCount(data.length);
|
||||||
},
|
},
|
||||||
marker: "ticket",
|
marker: "ticket",
|
||||||
});
|
});
|
||||||
|
|
||||||
useTicketsFetcher({
|
useTicketsFetcher({
|
||||||
url: "https://hub.pena.digital/heruvym/getTickets",
|
url: "https://hub.pena.digital/heruvym/getTickets",
|
||||||
ticketsPerPage,
|
ticketsPerPage,
|
||||||
ticketApiPage,
|
ticketApiPage,
|
||||||
onSuccess: (result) => {
|
onSuccess: (result) => {
|
||||||
if (result.data) updateTickets(result.data);
|
if (result.data) updateTickets(result.data);
|
||||||
setTicketCount(result.count);
|
setTicketCount(result.count);
|
||||||
},
|
},
|
||||||
onError: (error: Error) => {
|
onError: (error: Error) => {
|
||||||
const message = getMessageFromFetchError(error);
|
const message = getMessageFromFetchError(error);
|
||||||
if (message) enqueueSnackbar(message);
|
if (message) enqueueSnackbar(message);
|
||||||
},
|
},
|
||||||
onFetchStateChange: setTicketsFetchState,
|
onFetchStateChange: setTicketsFetchState,
|
||||||
});
|
});
|
||||||
|
|
||||||
useAllTariffsFetcher({
|
useAllTariffsFetcher({
|
||||||
onSuccess: updateTariffs,
|
onSuccess: updateTariffs,
|
||||||
onError: (error) => {
|
onError: (error) => {
|
||||||
const errorMessage = getMessageFromFetchError(error);
|
const errorMessage = getMessageFromFetchError(error);
|
||||||
if (errorMessage) enqueueSnackbar(errorMessage);
|
if (errorMessage) enqueueSnackbar(errorMessage);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
useCustomTariffs({
|
useCustomTariffs({
|
||||||
onNewUser: setCustomTariffs,
|
onNewUser: setCustomTariffs,
|
||||||
onError: (error) => {
|
onError: (error) => {
|
||||||
if (error) enqueueSnackbar(error);
|
if (error) enqueueSnackbar(error);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
useDiscounts({
|
useDiscounts({
|
||||||
onNewDiscounts: setDiscounts,
|
onNewDiscounts: setDiscounts,
|
||||||
onError: (error) => {
|
onError: (error) => {
|
||||||
const message = getMessageFromFetchError(error);
|
const message = getMessageFromFetchError(error);
|
||||||
if (message) enqueueSnackbar(message);
|
if (message) enqueueSnackbar(message);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
usePrivilegeFetcher({
|
usePrivilegeFetcher({
|
||||||
onSuccess: setPrivileges,
|
onSuccess: setPrivileges,
|
||||||
onError: (error) => {
|
onError: (error) => {
|
||||||
console.log("usePrivilegeFetcher error :>> ", error);
|
console.log("usePrivilegeFetcher error :>> ", error);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Navbar>
|
<Navbar>
|
||||||
<Outlet />
|
<Outlet />
|
||||||
</Navbar>
|
</Navbar>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -9,9 +9,9 @@ import { Tabs } from "@root/components/Tabs";
|
|||||||
import AccordionWrapper from "./AccordionWrapper";
|
import AccordionWrapper from "./AccordionWrapper";
|
||||||
import { HISTORY } from "./historyMocks";
|
import { HISTORY } from "./historyMocks";
|
||||||
import { useHistoryTracker } from "@root/utils/hooks/useHistoryTracker";
|
import { useHistoryTracker } from "@root/utils/hooks/useHistoryTracker";
|
||||||
import { makeRequest } from "@frontend/kitui";
|
import { makeRequest, useToken } from "@frontend/kitui";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import { GetHistoryResponse, getHistory } from "@root/api/history";
|
import { getHistory } from "@root/api/history";
|
||||||
import { useHistoryData } from "@root/utils/hooks/useHistoryData";
|
import { useHistoryData } from "@root/utils/hooks/useHistoryData";
|
||||||
|
|
||||||
const subPages = ["Платежи", "Покупки тарифов", "Окончания тарифов"];
|
const subPages = ["Платежи", "Покупки тарифов", "Окончания тарифов"];
|
||||||
@ -23,10 +23,13 @@ export default function History() {
|
|||||||
const upMd = useMediaQuery(theme.breakpoints.up("md"));
|
const upMd = useMediaQuery(theme.breakpoints.up("md"));
|
||||||
const isMobile = useMediaQuery(theme.breakpoints.down(600));
|
const isMobile = useMediaQuery(theme.breakpoints.down(600));
|
||||||
const isTablet = useMediaQuery(theme.breakpoints.down(1000));
|
const isTablet = useMediaQuery(theme.breakpoints.down(1000));
|
||||||
|
const { historyData, error } = useHistoryData();
|
||||||
|
|
||||||
const handleCustomBackNavigation = useHistoryTracker();
|
const handleCustomBackNavigation = useHistoryTracker();
|
||||||
|
|
||||||
const { historyResponse, historyTariffsArray, error } = useHistoryData();
|
if (!error && historyData) {
|
||||||
|
console.log(historyData?.records[0].rawDetails.map((rawDetails) => console.log(rawDetails.Key)));
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<SectionWrapper
|
<SectionWrapper
|
||||||
|
@ -1,22 +1,20 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import axios from "axios";
|
|
||||||
import { GetHistoryResponse, getHistory } from "@root/api/history";
|
import { GetHistoryResponse, getHistory } from "@root/api/history";
|
||||||
import { Tariff } from "@frontend/kitui";
|
|
||||||
|
|
||||||
export const useHistoryData = () => {
|
export const useHistoryData = () => {
|
||||||
const [historyResponse, setHistoryResponse] = useState<null | GetHistoryResponse>(null);
|
const [historyData, setHistoryData] = useState<GetHistoryResponse | null>(null);
|
||||||
const [historyTariffsArray, setHistoryTariffsArray] = useState<Tariff[]>([]);
|
|
||||||
const [error, setError] = useState<null | string>(null);
|
const [error, setError] = useState<null | string>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function fetchData() {
|
async function fetchData() {
|
||||||
try {
|
try {
|
||||||
const [historyData, errorMsg] = await getHistory();
|
const [response, errorMsg] = await getHistory();
|
||||||
|
|
||||||
if (errorMsg) {
|
if (errorMsg) {
|
||||||
setError(errorMsg);
|
setError(errorMsg);
|
||||||
} else {
|
}
|
||||||
setHistoryResponse(historyData);
|
if (response) {
|
||||||
|
setHistoryData(response);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("Произошла ошибка при вызове getHistory:", err);
|
console.error("Произошла ошибка при вызове getHistory:", err);
|
||||||
@ -26,32 +24,5 @@ export const useHistoryData = () => {
|
|||||||
fetchData();
|
fetchData();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
return { historyData, error };
|
||||||
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 };
|
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user