Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3658 | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

import { api } from '@api';
import { useApi } from './useApi';
import { useAlert } from './useAlert';
import { useEffect, useRef } from 'react';

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

export function useFetch(url) {
  const { showError } = useAlert();
  const { loading, data, error, execute } = useApi(getResources);
  const hasFetched = useRef(false);

  useEffect(() => {
    if (!hasFetched.current && url) {
      execute(url);
      hasFetched.current = true;
    } else if (url) {
      execute(url);
    }
  }, [url, execute]);

  useEffect(() => {
    if (error) showError(error);
  }, [error, showError]);

  return {
    data,
    loading,
    refetch: () => execute(url)
  };
}