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


import { useEffect, useState } from "react";

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

import { getArticles } from "../features/article/thunk";


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

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

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

  return { loading, status };
}
