import { useState, useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import { LOADING_END, LOADING_START } from "../../../lib/redux/slices/appSlice";
import { avicultureGetRequests } from "../services/aviculture-requests";

const useAvicultureRequests = () => {
  const dispatch = useDispatch();
  const { avicultureRequests } = useSelector((state) => state.avicultureSlice);

  const [data, setData] = useState(avicultureRequests);

  useEffect(() => {
    dispatch(LOADING_START());
    dispatch(avicultureGetRequests()).then(() => {
      dispatch(LOADING_END());
    });
  }, []);

  useEffect(() => {
    setData(avicultureRequests);
  }, [avicultureRequests]);

  return data ? data : [];
};

export default useAvicultureRequests;
