add usePrivileges hook

This commit is contained in:
nflnkr 2023-08-08 14:23:11 +03:00
parent 45b532415b
commit 96edf676f4

@ -0,0 +1,39 @@
import { useEffect, useLayoutEffect, useRef } from "react";
import { makeRequest } from "../api";
import { PrivilegeWithAmount } from "../model";
export function usePrivilegeFetcher({
onSuccess,
url = process.env.NODE_ENV === "production" ? "/strator/privilege" : "https://admin.pena.digital/strator/privilege",
onError,
}: {
onSuccess: (response: PrivilegeWithAmount[]) => void;
url?: string;
onError?: (error: Error) => void;
}) {
const onSuccessRef = useRef(onSuccess);
const onErrorRef = useRef(onError);
useLayoutEffect(() => {
onSuccessRef.current = onSuccess;
onErrorRef.current = onError;
}, [onSuccess, onError]);
useEffect(function fetchTickets() {
const controller = new AbortController();
makeRequest<never, PrivilegeWithAmount[]>({
url,
method: "get",
useToken: true,
signal: controller.signal,
}).then((result) => {
onSuccessRef.current(result);
}).catch(error => {
onErrorRef.current?.(error);
});
return () => controller.abort();
}, [url]);
}