diff --git a/lib/hooks/index.ts b/lib/hooks/index.ts index 0fbd7bb..25f5af7 100644 --- a/lib/hooks/index.ts +++ b/lib/hooks/index.ts @@ -1,11 +1,12 @@ +export * from "./useAllTariffsFetcher"; export * from "./useDebounce"; export * from "./useEventListener"; export * from "./usePrivilegeFetcher"; export * from "./useSSESubscription"; -export * from "./useTariffs"; +export * from "./usePaginatedTariffsFetcher"; export * from "./useThrottle"; export * from "./useTicketMessages"; export * from "./useTickets"; export * from "./useToken"; -export * from "./useUserFetcher"; export * from "./useUserAccountFetcher"; +export * from "./useUserFetcher"; diff --git a/lib/hooks/useAllTariffsFetcher.ts b/lib/hooks/useAllTariffsFetcher.ts new file mode 100644 index 0000000..3b6a93a --- /dev/null +++ b/lib/hooks/useAllTariffsFetcher.ts @@ -0,0 +1,59 @@ +import { GetTariffsResponse, Tariff, makeRequest } from "@frontend/kitui"; +import { useRef, useLayoutEffect, useEffect } from "react"; + + +export function useAllTariffsFetcher({ + enabled = true, + baseUrl = process.env.NODE_ENV === "production" ? "/strator/tariff" : "https://hub.pena.digital/strator/tariff", + onSuccess, + onError, +}: { + enabled?: boolean; + baseUrl?: string; + onSuccess: (response: Tariff[]) => void; + onError?: (error: Error) => void; +}) { + const onNewTariffsRef = useRef(onSuccess); + const onErrorRef = useRef(onError); + + useLayoutEffect(() => { + onNewTariffsRef.current = onSuccess; + onErrorRef.current = onError; + }, [onError, onSuccess]); + + useEffect(() => { + if (!enabled) return; + + const controller = new AbortController(); + + async function getPaginatedTariffs() { + let apiPage = 1; + const tariffsPerPage = 100; + let isDone = false; + + while (!isDone) { + try { + const result = await makeRequest({ + url: baseUrl + `?page=${apiPage}&limit=${tariffsPerPage}`, + method: "get", + useToken: true, + signal: controller.signal, + }); + if (result.tariffs.length > 0) { + onNewTariffsRef.current(result.tariffs); + apiPage++; + } else { + isDone = true; + } + } catch (error) { + onErrorRef.current?.(error as Error); + isDone = true; + } + } + } + + getPaginatedTariffs(); + + return () => controller.abort(); + }, [baseUrl, enabled]); +} diff --git a/lib/hooks/useTariffs.ts b/lib/hooks/usePaginatedTariffsFetcher.ts similarity index 87% rename from lib/hooks/useTariffs.ts rename to lib/hooks/usePaginatedTariffsFetcher.ts index 442c772..9ebd21a 100644 --- a/lib/hooks/useTariffs.ts +++ b/lib/hooks/usePaginatedTariffsFetcher.ts @@ -5,7 +5,8 @@ import { devlog } from "../utils"; import { FetchState } from "../model/fetchState"; -export function useTariffs({ url, tariffsPerPage, apiPage, onSuccess, onError, onFetchStateChange }: { +export function usePaginatedTariffsFetcher({ enabled = true, url, tariffsPerPage, apiPage, onSuccess, onError, onFetchStateChange }: { + enabled?: boolean; url: string; tariffsPerPage: number; apiPage: number; @@ -24,6 +25,8 @@ export function useTariffs({ url, tariffsPerPage, apiPage, onSuccess, onError, o }, [onSuccess, onError, onFetchStateChange]); useEffect(function fetchTickets() { + if (!enabled) return; + const controller = new AbortController(); onFetchStateChangeRef.current?.("fetching"); @@ -44,5 +47,5 @@ export function useTariffs({ url, tariffsPerPage, apiPage, onSuccess, onError, o }); return () => controller.abort(); - }, [apiPage, tariffsPerPage, url]); + }, [apiPage, enabled, tariffsPerPage, url]); } diff --git a/lib/hooks/useTickets.ts b/lib/hooks/useTickets.ts index a96c499..e683b70 100644 --- a/lib/hooks/useTickets.ts +++ b/lib/hooks/useTickets.ts @@ -5,7 +5,8 @@ import { makeRequest } from "../api"; import { FetchState } from "../model/fetchState"; -export function useTicketsFetcher({ url, ticketsPerPage, ticketApiPage, onSuccess, onError, onFetchStateChange }: { +export function useTicketsFetcher({ enabled = true, url, ticketsPerPage, ticketApiPage, onSuccess, onError, onFetchStateChange }: { + enabled?: boolean; url: string; ticketsPerPage: number; ticketApiPage: number; @@ -24,6 +25,8 @@ export function useTicketsFetcher({ url, ticketsPerPage, ticketApiPage, onSucces }, [onSuccess, onError, onFetchStateChange]); useEffect(function fetchTickets() { + if (!enabled) return; + const controller = new AbortController(); onFetchStateChangeRef.current?.("fetching"); @@ -49,5 +52,5 @@ export function useTicketsFetcher({ url, ticketsPerPage, ticketApiPage, onSucces }); return () => controller.abort(); - }, [ticketApiPage, ticketsPerPage, url]); + }, [enabled, ticketApiPage, ticketsPerPage, url]); } diff --git a/package.json b/package.json index 4f5d76f..43a4613 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@frontend/kitui", - "version": "1.0.44", + "version": "1.0.45", "description": "test", "main": "./dist/index.js", "module": "./dist/index.js",