add usePrivileges hook
This commit is contained in:
parent
45b532415b
commit
96edf676f4
39
src/hooks/usePrivileges.ts
Normal file
39
src/hooks/usePrivileges.ts
Normal file
@ -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]);
|
||||
}
|
Loading…
Reference in New Issue
Block a user