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

CBIIT / crdc-datahub-ui / 11562402071

28 Oct 2024 08:48PM UTC coverage: 52.297% (+0.01%) from 52.284%
11562402071

push

github

web-flow
Merge pull request #505 from CBIIT/CRDCDH-1744

CRDCDH-1744 Support arbitrary number of Model Files

2432 of 5148 branches covered (47.24%)

Branch coverage included in aggregate %.

1 of 5 new or added lines in 2 files covered. (20.0%)

1 existing line in 1 file now uncovered.

3488 of 6172 relevant lines covered (56.51%)

130.79 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
  getModelExploreData,
8
} from "data-model-navigator";
9
import { useLazyQuery } from "@apollo/client";
10
import { defaultTo } from "lodash";
11
import { baseConfiguration, defaultReadMeTitle, graphViewConfig } from "../config/ModelNavigator";
12
import {
13
  buildAssetUrls,
14
  buildBaseFilterContainers,
15
  buildFilterOptionsList,
16
  updateEnums,
17
  Logger,
18
} from "../utils";
19
import { RETRIEVE_CDEs, RetrieveCDEsInput, RetrieveCDEsResp } from "../graphql";
20

21
export type ReduxStoreStatus = "waiting" | "loading" | "error" | "success";
22

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

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

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

39
  return newStore;
×
40
};
41

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

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

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

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

85
    setStatus("loading");
×
86

87
    const assets = buildAssetUrls(datacommon);
×
NEW
88
    const response = await getModelExploreData(...assets.model_files)?.catch((e) => {
×
89
      Logger.error(e);
×
90
      return null;
×
91
    });
92
    if (!response?.data || !response?.version) {
×
93
      setStatus("error");
×
94
      return;
×
95
    }
96

97
    let dictionary;
98
    const { cdeMap, data: dataList } = response;
×
99

100
    if (cdeMap) {
×
101
      const cdeInfo: CDEInfo[] = Array.from(response.cdeMap.values());
×
102
      try {
×
103
        const CDEs = await retrieveCDEs({
×
104
          variables: {
105
            cdeInfo,
106
          },
107
        });
108

109
        if (retrieveCDEsError) {
×
110
          dictionary = updateEnums(cdeMap, dataList, [], true);
×
111
        } else {
112
          const retrievedCDEs = defaultTo(CDEs.data.retrieveCDEs, []);
×
113
          dictionary = updateEnums(cdeMap, dataList, retrievedCDEs);
×
114
        }
115
      } catch (error) {
116
        dictionary = updateEnums(cdeMap, dataList, [], true);
×
117
      }
118
    } else {
119
      dictionary = dataList;
×
120
    }
121

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

124
    store.dispatch({
×
125
      type: "REACT_FLOW_GRAPH_DICTIONARY",
126
      dictionary,
127
      pdfDownloadConfig: datacommon.configuration.pdfConfig,
128
      graphViewConfig,
129
    });
130

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

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

166
    setStatus("success");
×
167
  };
168

169
  return [{ status, store }, resetStore, populateStore];
×
170
};
171

172
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