import { useState, useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import { getAllocationInformation } from "../services/get-allocation-information";

const useGetAllocationInformation = (id) => {
  const dispatch = useDispatch();
  const { allocationInformation } = useSelector((state) => state.fileSlice);
  const [data, setData] = useState(allocationInformation);

  useEffect(() => {
    if (id) {
      dispatch(getAllocationInformation({ key: id }));
    }
  }, [id]);

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

  return data ? data : [];
};

export default useGetAllocationInformation;
