22 lines
No EOL
736 B
TypeScript
22 lines
No EOL
736 B
TypeScript
import { useState, useEffect } from "react";
|
|
import {UserType} from "@/types/UserType";
|
|
import {listUsers} from "@/utils/api/usersApi";
|
|
|
|
export const useFetchUsers = () => {
|
|
const [users, setUsers] = useState<UserType[]>([]);
|
|
const [isLoading, setIsLoading] = useState<boolean>(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
const fetchUsers = async () => {
|
|
listUsers()
|
|
.then((users: UserType[]) => setUsers(users))
|
|
.catch((err) => setError((err as Error).message || "An error occurred."))
|
|
.finally(() => setIsLoading(false));
|
|
};
|
|
|
|
fetchUsers();
|
|
}, []);
|
|
|
|
return { users, isLoading, error };
|
|
}; |