All files / hooks useAllUsers.ts

100% Statements 21/21
100% Branches 11/11
100% Functions 4/4
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 361x 1x               1x 6x 6x 6x   6x 3x 3x 3x   3x 1x 1x 2x 1x 1x     3x     3x     6x    
import { useEffect, useState } from "react";
import { getAllUsers } from "../api/user";
import { UserData } from "../api/types/user";
 
/**
 * Custom hook to fetch all users.
 * @module Hooks
 * @returns { users: UserData[], isLoading: boolean, error: string | null }
 */
export const useAllUsers = () => {
  const [users, setUsers] = useState<UserData[]>([]);
  const [isLoading, setIsLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);
 
  useEffect(() => {
    const fetchUsers = async () => {
      setIsLoading(true);
      const result = await getAllUsers();
 
      if (result?.error) {
        setError(result.error);
        setUsers([]);
      } else if (result?.data) {
        setUsers(result.data);
        setError(null);
      }
 
      setIsLoading(false);
    };
 
    fetchUsers();
  }, []);
 
  return { users, isLoading, error };
};