Refactor components to remove Layout wrapper and pass it in the App component , implemented a skeleton feedback while loading page and improved API calls related to Dashboard/User Profile
23 lines
743 B
TypeScript
23 lines
743 B
TypeScript
import { EntityWithRoles } from "@/interfaces/entity";
|
|
import axios from "axios";
|
|
import { useCallback, useEffect, useState } from "react";
|
|
|
|
export default function useEntities(shouldNot?: boolean) {
|
|
const [entities, setEntities] = useState<EntityWithRoles[]>([]);
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
const [isError, setIsError] = useState(false);
|
|
|
|
const getData = useCallback(() => {
|
|
if (shouldNot) return;
|
|
setIsLoading(true);
|
|
axios
|
|
.get<EntityWithRoles[]>("/api/entities?showRoles=true")
|
|
.then((response) => setEntities(response.data))
|
|
.finally(() => setIsLoading(false));
|
|
}, [shouldNot]);
|
|
|
|
useEffect(getData, [getData])
|
|
|
|
return { entities, isLoading, isError, reload: getData };
|
|
}
|