front-hub/src/utils/hooks/useCustomTariffs.ts

46 lines
1.1 KiB
TypeScript
Raw Normal View History

2024-05-29 11:58:54 +00:00
import { useEffect, useLayoutEffect, useRef } from "react";
import { devlog } from "@frontend/kitui";
2023-06-06 13:13:58 +00:00
2024-05-29 11:58:54 +00:00
import { ServiceKeyToPrivilegesMap } from "@root/model/privilege";
import { getCustomTariffs } from "@root/api/tariff";
2023-06-06 13:13:58 +00:00
2023-08-31 10:02:11 +00:00
export function useCustomTariffs({
2024-05-29 11:58:54 +00:00
onError,
onNewUser,
2023-08-31 10:02:11 +00:00
}: {
onNewUser: (response: ServiceKeyToPrivilegesMap) => void;
onError: (error: any) => void;
2023-06-06 13:13:58 +00:00
}) {
2024-05-29 11:58:54 +00:00
const onNewUserRef = useRef(onNewUser);
const onErrorRef = useRef(onError);
useLayoutEffect(() => {
onNewUserRef.current = onNewUser;
onErrorRef.current = onError;
});
useEffect(() => {
const controller = new AbortController();
const getCustomTariffsRequest = async () => {
const [customTariffs, customTariffsError] = await getCustomTariffs(
controller.signal
);
if (customTariffsError) {
devlog("Error fetching custom tariffs", customTariffsError);
onErrorRef.current(customTariffsError);
return;
}
if (customTariffs) {
onNewUserRef.current(customTariffs);
}
};
getCustomTariffsRequest();
return () => controller.abort();
}, []);
}