import React, { useCallback, useEffect } from 'react';
import { useDispatch } from 'react-redux';
import { asyncAction, syncAction } from 'store/utils/actions';

const useAction = (action, sync?: boolean): any => {
  const dispatch = useDispatch();

  const callback = useCallback(
    (data: any, status?: string, requestData: any) => {
      if (sync) return syncAction(dispatch, action)(data, status, requestData);
      return asyncAction(dispatch, action)(data, status, requestData);
    },
    [action, dispatch, sync],
  );

  return callback;
};

export default useAction;
