vineanova-redux-artifacts
Version:
VineaOpenApiReduxArtifacts is a powerful npm library designed to simplify the integration of Swagger APIs into Redux architecture. By leveraging templates (Mustache files), this library automatically generates Redux ducks, actions, sagas, selectors, and A
53 lines (46 loc) • 1.3 kB
JavaScript
import { useEffect } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import isEmpty from 'lodash/isEmpty';
import { VineaNovaActions } from '../actions';
import * as VineaNovaSelectors from '../selectors';
export const useFetchlookupTrellis = (props) => {
// selectors
const data = useSelector(
VineaNovaSelectors.getlookupTrellisEntityData,
);
const entity = useSelector(
VineaNovaSelectors.getlookupTrellisEntityMeta,
);
const dispatchAPI = useDispatch();
const { queryParams = {} } = props;
// Determine if we need to fetch: not loaded, has error, or empty data
const shouldFetch =
!entity?.isLoaded || entity?.hasError || isEmpty(entity.data);
useEffect(() => {
if (shouldFetch) {
dispatchAPI(
VineaNovaActions.api.v1.lookupTrellis.get.request({
queryParams: {
...queryParams,
},
}),
);
}
}, [shouldFetch, JSON.stringify(queryParams)]);
const useQuery = (queryParams) => {
dispatchAPI(
VineaNovaActions.api.v1.lookupTrellis.get.request({
queryParams: {
...queryParams,
},
}),
);
};
return {
data,
useQuery,
isLoading: entity?.isLoading,
hasError: entity?.hasError,
error: entity?.error,
};
};