/* eslint-disable import/no-unresolved */


import { useEffect, useState } from "react";

import { useDispatch, useSelector } from "react-redux";

import { getClients } from "../features/client/thunk";


export function useClients() {
  const dispatch = useDispatch<any>();
  const clientsStatus = useSelector((state: any) => state.client.status);
  const [loading, setLoading] = useState(false);

  useEffect(() => {
    const load = async () => {
      if (clientsStatus === "idle") {
        setLoading(true);
        await dispatch(getClients()).unwrap();
        setLoading(false);
      }
    };

    load();
  }, [dispatch, clientsStatus]);

  return { loading, clientsStatus };
}
