import { useState, useCallback } from 'react';

export default (expFunc) => {
  const [loading, setLoading] = useState(false);

  const fn = useCallback(
    (...args) => {
      if (typeof expFunc === 'function') {
        setLoading(true);
        expFunc(...args)
          .then(() => {
            setLoading(false);
          })
          .catch(() => {
            setLoading(false);
          });
      }
    },
    [setLoading, expFunc],
  );

  return [loading, fn];
};
