mirror of
https://github.com/0rangebananaspy/authelia.git
synced 2024-09-14 22:47:21 +07:00
9ae2096d2a
This refactoring simplify the code of the frontend and prepare the portal for receiving a user settings page and an admin page.
31 lines
981 B
TypeScript
31 lines
981 B
TypeScript
import { useState, useCallback, DependencyList } from "react";
|
|
|
|
type PromisifiedFunction<Ret> = (...args: any) => Promise<Ret>
|
|
|
|
export function useRemoteCall<Ret>(fn: PromisifiedFunction<Ret>, deps: DependencyList)
|
|
: [Ret | undefined, PromisifiedFunction<void>, boolean, Error | undefined] {
|
|
const [data, setData] = useState(undefined as Ret | undefined);
|
|
const [inProgress, setInProgress] = useState(false);
|
|
const [error, setError] = useState(undefined as Error | undefined);
|
|
|
|
const fnCallback = useCallback(fn, deps);
|
|
|
|
const triggerCallback = useCallback(async () => {
|
|
try {
|
|
setInProgress(true);
|
|
const res = await fnCallback();
|
|
setInProgress(false);
|
|
setData(res);
|
|
} catch (err) {
|
|
console.error(err);
|
|
setError(err);
|
|
}
|
|
}, [setInProgress, setError, fnCallback]);
|
|
|
|
return [
|
|
data,
|
|
triggerCallback,
|
|
inProgress,
|
|
error,
|
|
]
|
|
} |