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

CBIIT / crdc-datahub-ui / 11462692480

22 Oct 2024 02:45PM UTC coverage: 52.006% (-0.01%) from 52.016%
11462692480

Pull #502

github

web-flow
Merge 318c769d5 into ae763d952
Pull Request #502: Support `model-files` array

2400 of 5115 branches covered (46.92%)

Branch coverage included in aggregate %.

1 of 4 new or added lines in 2 files covered. (25.0%)

28 existing lines in 1 file now uncovered.

3459 of 6151 relevant lines covered (56.23%)

130.45 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;
×
UNCOV
37
    newStore.replaceReducer(combineReducers(reducers));
×
38
  };
39

UNCOV
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
] => {
UNCOV
53
  const [status, setStatus] = useState<Status>("waiting");
×
UNCOV
54
  const [store, setStore] = useState<Store>(makeStore());
×
55

UNCOV
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
   */
UNCOV
69
  const resetStore = () => {
×
UNCOV
70
    setStatus("loading");
×
UNCOV
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
   */
UNCOV
79
  const populateStore = async (datacommon: DataCommon) => {
×
UNCOV
80
    if (
×
81
      !datacommon?.name ||
×
82
      !datacommon?.assets ||
83
      !datacommon?.assets["current-version"] ||
84
      !datacommon.configuration?.pdfConfig
85
    ) {
UNCOV
86
      setStatus("error");
×
87
      return;
×
88
    }
89

90
    setStatus("loading");
×
91

92
    const assets = buildAssetUrls(datacommon);
×
93

94
    // TODO: This is a very temporary work-around until DMN supports N != 2 files
NEW
UNCOV
95
    const modelFile = assets.model_files[0];
×
NEW
UNCOV
96
    const propFile = assets.model_files.length > 1 ? assets.model_files[1] : assets.model_files[0];
×
97

NEW
98
    const response = await getModelExploreData(modelFile, propFile)?.catch((e) => {
×
UNCOV
99
      Logger.error(e);
×
100
      return null;
×
101
    });
102
    if (!response?.data || !response?.version) {
×
103
      setStatus("error");
×
UNCOV
104
      return;
×
105
    }
106

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

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

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

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

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

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

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

UNCOV
176
    setStatus("success");
×
177
  };
178

UNCOV
179
  return [{ status, store }, resetStore, populateStore];
×
180
};
181

182
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