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

CBIIT / crdc-datahub-ui / 14134335479

28 Mar 2025 05:16PM UTC coverage: 60.279% (+6.1%) from 54.222%
14134335479

push

github

web-flow
Merge pull request #655 from CBIIT/3.2.0

3.2.0 Release

3137 of 5624 branches covered (55.78%)

Branch coverage included in aggregate %.

991 of 1319 new or added lines in 98 files covered. (75.13%)

33 existing lines in 14 files now uncovered.

4372 of 6833 relevant lines covered (63.98%)

147.13 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, combineReducers, Store } from "redux";
3
import {
4
  ddgraph,
5
  moduleReducers as submission,
6
  versionInfo,
7
  changelogInfo,
8
  getModelExploreData,
9
  getChangelog,
10
} from "data-model-navigator";
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 ReduxStoreStatus = "waiting" | "loading" | "error" | "success";
24

25
export type ReduxStoreResult = [
26
  { status: ReduxStoreStatus; store: Store },
27
  () => void,
28
  (assets: DataCommon, modelVersion: string) => void,
29
];
30

31
const makeStore = (): Store => {
×
NEW
32
  const reducers = { ddgraph, versionInfo, submission, changelogInfo };
×
33
  const newStore = createStore(combineReducers(reducers));
×
34

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

41
  return newStore;
×
42
};
43

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

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

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

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

88
    setStatus("loading");
×
89

NEW
90
    const assets = buildAssetUrls(datacommon, modelVersion);
×
91

NEW
92
    const changelogMD = await getChangelog(assets?.changelog)?.catch((e) => {
×
NEW
93
      Logger.error(e);
×
NEW
94
      return null;
×
95
    });
96

97
    const response = await getModelExploreData(...assets.model_files)?.catch((e) => {
×
98
      Logger.error(e);
×
99
      return null;
×
100
    });
101
    if (!response?.data || !response?.version) {
×
102
      setStatus("error");
×
103
      return;
×
104
    }
105

106
    let dictionary;
107
    const { cdeMap, data: dataList } = response;
×
108

109
    if (cdeMap) {
×
110
      const cdeInfo: CDEInfo[] = Array.from(response.cdeMap.values());
×
111
      try {
×
112
        const CDEs = await retrieveCDEs({
×
113
          variables: {
114
            cdeInfo: cdeInfo.map(({ CDECode, CDEVersion }) => ({ CDECode, CDEVersion })),
×
115
          },
116
        });
117

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

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

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

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

NEW
170
    store.dispatch({
×
171
      type: "RECEIVE_CHANGELOG_INFO",
172
      data: {
173
        changelogMD,
174
        changelogTabName: "Version History",
175
      },
176
    });
177

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

183
    setStatus("success");
×
184
  };
185

186
  return [{ status, store }, resetStore, populateStore];
×
187
};
188

189
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