• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CBIIT / crdc-datahub-ui / 11132827773

01 Oct 2024 08:21PM UTC coverage: 48.343% (+0.3%) from 48.074%
11132827773

push

github

GitHub
Merge pull request #459 from CBIIT/CRDCDH-1443

1996 of 4667 branches covered (42.77%)

Branch coverage included in aggregate %.

2905 of 5471 relevant lines covered (53.1%)

128.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/hooks/useBuildReduxStore.ts
1
import { useState } from "react";
2
import { createStore, applyMiddleware, combineReducers, Store } from "redux";
3
import {
4
  ddgraph,
5
  moduleReducers as submission,
6
  versionInfo,
7
  getModelExploreData,
8
} from "data-model-navigator";
9
import ReduxThunk from "redux-thunk";
10
import { createLogger } from "redux-logger";
11
import { useLazyQuery } from "@apollo/client";
12
import { defaultTo } from "lodash";
13
import { baseConfiguration, defaultReadMeTitle, graphViewConfig } from "../config/ModelNavigator";
14
import {
15
  buildAssetUrls,
16
  buildBaseFilterContainers,
17
  buildFilterOptionsList,
18
  updateEnums,
19
  Logger,
20
} from "../utils";
21
import { RETRIEVE_CDEs, RetrieveCDEsInput, RetrieveCDEsResp } from "../graphql";
22

23
export type Status = "waiting" | "loading" | "error" | "success";
24

25
const makeStore = (): Store => {
×
26
  const reducers = { ddgraph, versionInfo, submission };
×
27
  const loggerMiddleware = createLogger();
×
28

29
  const newStore = createStore(
×
30
    combineReducers(reducers),
31
    applyMiddleware(ReduxThunk, loggerMiddleware)
32
  );
33

34
  // @ts-ignore
35
  newStore.injectReducer = (key, reducer) => {
×
36
    reducers[key] = reducer;
×
37
    newStore.replaceReducer(combineReducers(reducers));
×
38
  };
39

40
  return newStore;
×
41
};
42

43
/**
44
 * A hook to build and populate the Redux store with DMN data
45
 *
46
 * @params {void}
47
 */
48
const useBuildReduxStore = (): [
×
49
  { status: Status; store: Store },
50
  () => void,
51
  (assets: DataCommon) => void,
52
] => {
53
  const [status, setStatus] = useState<Status>("waiting");
×
54
  const [store, setStore] = useState<Store>(makeStore());
×
55

56
  const [retrieveCDEs, { error: retrieveCDEsError }] = useLazyQuery<
×
57
    RetrieveCDEsResp,
58
    RetrieveCDEsInput
59
  >(RETRIEVE_CDEs, {
60
    context: { clientName: "backend" },
61
    fetchPolicy: "cache-and-network",
62
  });
63

64
  /**
65
   * Rebuilds the store from scratch
66
   *
67
   * @params {void}
68
   */
69
  const resetStore = () => {
×
70
    setStatus("loading");
×
71
    setStore(makeStore());
×
72
  };
73

74
  /**
75
   * Injects the Data Model into the store
76
   *
77
   * @param datacommon The Data Model to inject assets from
78
   */
79
  const populateStore = async (datacommon: DataCommon) => {
×
80
    if (
×
81
      !datacommon?.name ||
×
82
      !datacommon?.assets ||
83
      !datacommon?.assets["current-version"] ||
84
      !datacommon.configuration?.pdfConfig
85
    ) {
86
      setStatus("error");
×
87
      return;
×
88
    }
89

90
    setStatus("loading");
×
91

92
    const assets = buildAssetUrls(datacommon);
×
93

94
    const response = await getModelExploreData(assets.model, assets.props)?.catch((e) => {
×
95
      Logger.error(e);
×
96
      return null;
×
97
    });
98
    if (!response?.data || !response?.version) {
×
99
      setStatus("error");
×
100
      return;
×
101
    }
102

103
    let dictionary;
104
    const { cdeMap, data: dataList } = response;
×
105

106
    if (cdeMap) {
×
107
      const cdeInfo: CDEInfo[] = Array.from(response.cdeMap.values());
×
108
      try {
×
109
        const CDEs = await retrieveCDEs({
×
110
          variables: {
111
            cdeInfo,
112
          },
113
        });
114

115
        if (retrieveCDEsError) {
×
116
          dictionary = updateEnums(cdeMap, dataList, [], true);
×
117
        } else {
118
          const retrievedCDEs = defaultTo(CDEs.data.retrieveCDEs, []);
×
119
          dictionary = updateEnums(cdeMap, dataList, retrievedCDEs);
×
120
        }
121
      } catch (error) {
122
        dictionary = updateEnums(cdeMap, dataList, [], true);
×
123
      }
124
    } else {
125
      dictionary = dataList;
×
126
    }
127

128
    store.dispatch({ type: "RECEIVE_VERSION_INFO", data: response.version });
×
129

130
    store.dispatch({
×
131
      type: "REACT_FLOW_GRAPH_DICTIONARY",
132
      dictionary,
133
      pdfDownloadConfig: datacommon.configuration.pdfConfig,
134
      graphViewConfig,
135
    });
136

137
    store.dispatch({
×
138
      type: "RECEIVE_DICTIONARY",
139
      payload: {
140
        data: dictionary,
141
        facetfilterConfig: {
142
          ...baseConfiguration,
143
          facetSearchData: datacommon.configuration.facetFilterSearchData,
144
          facetSectionVariables: datacommon.configuration.facetFilterSectionVariables,
145
          baseFilters: buildBaseFilterContainers(datacommon),
146
          filterSections: datacommon.configuration.facetFilterSearchData.map((s) => s?.datafield),
×
147
          filterOptions: buildFilterOptionsList(datacommon),
148
        },
149
        pageConfig: {
150
          title: datacommon.configuration.pageTitle,
151
          iconSrc: datacommon.configuration?.titleIconSrc,
152
        },
153
        readMeConfig: {
154
          readMeUrl: assets.readme,
155
          readMeTitle: datacommon.configuration?.readMeTitle || defaultReadMeTitle,
×
156
          allowDownload: false,
157
        },
158
        pdfDownloadConfig: datacommon.configuration.pdfConfig,
159
        loadingExampleConfig: {
160
          type: "static",
161
          url: assets.loading_file,
162
        },
163
        graphViewConfig,
164
      },
165
    });
166

167
    // MVP-2 M2 NOTE: This resets the search history to prevent the data models
168
    // from overlapping on searches. A future improvement would be to isolate
169
    // the localStorage history key to the data model based on a config option.
170
    store.dispatch({ type: "SEARCH_CLEAR_HISTORY" });
×
171

172
    setStatus("success");
×
173
  };
174

175
  return [{ status, store }, resetStore, populateStore];
×
176
};
177

178
export default useBuildReduxStore;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc