fix hooks
This commit is contained in:
parent
8b469c1b2d
commit
56afbbefe2
@ -1,11 +1,12 @@
|
|||||||
|
export * from "./useAllTariffsFetcher";
|
||||||
export * from "./useDebounce";
|
export * from "./useDebounce";
|
||||||
export * from "./useEventListener";
|
export * from "./useEventListener";
|
||||||
export * from "./usePrivilegeFetcher";
|
export * from "./usePrivilegeFetcher";
|
||||||
export * from "./useSSESubscription";
|
export * from "./useSSESubscription";
|
||||||
export * from "./useTariffs";
|
export * from "./usePaginatedTariffsFetcher";
|
||||||
export * from "./useThrottle";
|
export * from "./useThrottle";
|
||||||
export * from "./useTicketMessages";
|
export * from "./useTicketMessages";
|
||||||
export * from "./useTickets";
|
export * from "./useTickets";
|
||||||
export * from "./useToken";
|
export * from "./useToken";
|
||||||
export * from "./useUserFetcher";
|
|
||||||
export * from "./useUserAccountFetcher";
|
export * from "./useUserAccountFetcher";
|
||||||
|
export * from "./useUserFetcher";
|
||||||
|
59
lib/hooks/useAllTariffsFetcher.ts
Normal file
59
lib/hooks/useAllTariffsFetcher.ts
Normal file
@ -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<never, GetTariffsResponse>({
|
||||||
|
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]);
|
||||||
|
}
|
@ -5,7 +5,8 @@ import { devlog } from "../utils";
|
|||||||
import { FetchState } from "../model/fetchState";
|
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;
|
url: string;
|
||||||
tariffsPerPage: number;
|
tariffsPerPage: number;
|
||||||
apiPage: number;
|
apiPage: number;
|
||||||
@ -24,6 +25,8 @@ export function useTariffs({ url, tariffsPerPage, apiPage, onSuccess, onError, o
|
|||||||
}, [onSuccess, onError, onFetchStateChange]);
|
}, [onSuccess, onError, onFetchStateChange]);
|
||||||
|
|
||||||
useEffect(function fetchTickets() {
|
useEffect(function fetchTickets() {
|
||||||
|
if (!enabled) return;
|
||||||
|
|
||||||
const controller = new AbortController();
|
const controller = new AbortController();
|
||||||
|
|
||||||
onFetchStateChangeRef.current?.("fetching");
|
onFetchStateChangeRef.current?.("fetching");
|
||||||
@ -44,5 +47,5 @@ export function useTariffs({ url, tariffsPerPage, apiPage, onSuccess, onError, o
|
|||||||
});
|
});
|
||||||
|
|
||||||
return () => controller.abort();
|
return () => controller.abort();
|
||||||
}, [apiPage, tariffsPerPage, url]);
|
}, [apiPage, enabled, tariffsPerPage, url]);
|
||||||
}
|
}
|
@ -5,7 +5,8 @@ import { makeRequest } from "../api";
|
|||||||
import { FetchState } from "../model/fetchState";
|
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;
|
url: string;
|
||||||
ticketsPerPage: number;
|
ticketsPerPage: number;
|
||||||
ticketApiPage: number;
|
ticketApiPage: number;
|
||||||
@ -24,6 +25,8 @@ export function useTicketsFetcher({ url, ticketsPerPage, ticketApiPage, onSucces
|
|||||||
}, [onSuccess, onError, onFetchStateChange]);
|
}, [onSuccess, onError, onFetchStateChange]);
|
||||||
|
|
||||||
useEffect(function fetchTickets() {
|
useEffect(function fetchTickets() {
|
||||||
|
if (!enabled) return;
|
||||||
|
|
||||||
const controller = new AbortController();
|
const controller = new AbortController();
|
||||||
|
|
||||||
onFetchStateChangeRef.current?.("fetching");
|
onFetchStateChangeRef.current?.("fetching");
|
||||||
@ -49,5 +52,5 @@ export function useTicketsFetcher({ url, ticketsPerPage, ticketApiPage, onSucces
|
|||||||
});
|
});
|
||||||
|
|
||||||
return () => controller.abort();
|
return () => controller.abort();
|
||||||
}, [ticketApiPage, ticketsPerPage, url]);
|
}, [enabled, ticketApiPage, ticketsPerPage, url]);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@frontend/kitui",
|
"name": "@frontend/kitui",
|
||||||
"version": "1.0.44",
|
"version": "1.0.45",
|
||||||
"description": "test",
|
"description": "test",
|
||||||
"main": "./dist/index.js",
|
"main": "./dist/index.js",
|
||||||
"module": "./dist/index.js",
|
"module": "./dist/index.js",
|
||||||
|
Loading…
Reference in New Issue
Block a user