Merge branch 'dev' into 'main'

запрос на ранее купленные тарифы

See merge request frontend/marketplace!70
This commit is contained in:
Mikhail 2023-11-26 20:58:56 +00:00
commit 48dcc57a47
3 changed files with 54 additions and 2 deletions

@ -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<never, any>({
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}`]
}
}

@ -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)}
</Box>
{tariffsFromHistory.length > 0 && (
{recentlyPurchased.length > 0 && (
<>
<Typography
sx={{
@ -192,7 +194,7 @@ function TariffPage() {
>
Ранее вы
</Typography>
<Slider items={createTariffElements(tariffsFromHistory)} />
<Slider items={createTariffElements(recentlyPurchased)} />
</>
)}
</SectionWrapper>

@ -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<any>([])
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}
}