UNPKG

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
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 useFetchJobBlocksBulk = (props) => { // selectors const data = useSelector( VineaNovaSelectors.getJobBlocksBulkEntityData, ); const entity = useSelector( VineaNovaSelectors.getJobBlocksBulkEntityMeta, ); 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.jobBlocksBulk.get.request({ queryParams: { ...queryParams, }, }), ); } }, [shouldFetch, JSON.stringify(queryParams)]); const useQuery = (queryParams) => { dispatchAPI( VineaNovaActions.api.v1.jobBlocksBulk.get.request({ queryParams: { ...queryParams, }, }), ); }; return { data, useQuery, isLoading: entity?.isLoading, hasError: entity?.hasError, error: entity?.error, }; };