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

45 lines (39 loc) 1.38 kB
import { useEffect } from "react"; import { useDispatch, useSelector } from "react-redux"; import { VineaNovaActions } from "../actions"; import * as VineaNovaSelectors from "../selectors"; import { lookupNames } from "../lookups"; /** * useFetchAllLookups - React hook to fetch all lookups at once and store in Redux * * Usage: * const { isLoading, hasError, errors } = useFetchAllLookups(); */ export const useFetchAllLookups = () => { const dispatchAPI = useDispatch(); const lookupNameWithPrefix = lookupNames.map((name) => `lookup${name}`); // Collect all lookup entity states const lookupStates = useSelector((state) => { const result = {}; lookupNameWithPrefix.forEach((name) => { const selectorName = `getlookup${name}EntityMeta`; if (VineaNovaSelectors[selectorName]) { result[name] = VineaNovaSelectors[selectorName](state); } }); return result; }); useEffect(() => { // Determine which lookups need to be fetched const lookupsToFetch = lookupNameWithPrefix.filter((name) => { const entity = lookupStates[name]; return !entity?.isLoaded; }); lookupsToFetch.forEach((name) => { if (VineaNovaActions.api.v1[name] && VineaNovaActions.api.v1[name].get) { dispatchAPI(VineaNovaActions.api.v1[name].get.request({})); } }); }, [lookupStates]); return {}; };