Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3407 | Rev 3432 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

import { useEffect } from "react";
import { useDispatch } from "react-redux";

import { api } from "@api";
import { useApi } from "./useApi";
import { asyncLogout } from "@store/auth/auth.actions";
import { addNotification } from "@store/notification/notification.actions";

const getResources = async (url) => {
  return await api.get(url);
};

export function useFetch(url, defaultValue = {}) {
  const { data, loading, error, execute } = useApi(getResources(url), {
    autoFetch: true,
  });
  const dispatch = useDispatch();

  useEffect(() => {
    if (error) {
      dispatch(addNotification({ style: "danger", msg: err.message }));
      if (err.message.includes("sesión")) dispatch(asyncLogout());
    }
  }, [error]);

  return {
    data: data ?? defaultValue,
    loading,
    refetch: execute,
  };
}