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

CBIIT / crdc-datahub-ui / 11131910758

01 Oct 2024 07:14PM UTC coverage: 48.074% (+0.6%) from 47.497%
11131910758

push

github

GitHub
Merge pull request #474 from CBIIT/CRDCDH-1612

1962 of 4624 branches covered (42.43%)

Branch coverage included in aggregate %.

2867 of 5421 relevant lines covered (52.89%)

128.89 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 { baseConfiguration, defaultReadMeTitle, graphViewConfig } from "../config/ModelNavigator";
12
import {
13
  buildAssetUrls,
14
  buildBaseFilterContainers,
15
  buildFilterOptionsList,
16
  Logger,
17
} from "../utils";
18

19
export type Status = "waiting" | "loading" | "error" | "success";
20

21
const makeStore = (): Store => {
×
22
  const reducers = { ddgraph, versionInfo, submission };
×
23
  const loggerMiddleware = createLogger();
×
24

25
  const newStore = createStore(
×
26
    combineReducers(reducers),
27
    applyMiddleware(ReduxThunk, loggerMiddleware)
28
  );
29

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

36
  return newStore;
×
37
};
38

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

52
  /**
53
   * Rebuilds the store from scratch
54
   *
55
   * @params {void}
56
   */
57
  const resetStore = () => {
×
58
    setStatus("loading");
×
59
    setStore(makeStore());
×
60
  };
61

62
  /**
63
   * Injects the Data Model into the store
64
   *
65
   * @param datacommon The Data Model to inject assets from
66
   */
67
  const populateStore = async (datacommon: DataCommon) => {
×
68
    if (
×
69
      !datacommon?.name ||
×
70
      !datacommon?.assets ||
71
      !datacommon?.assets["current-version"] ||
72
      !datacommon.configuration?.pdfConfig
73
    ) {
74
      setStatus("error");
×
75
      return;
×
76
    }
77

78
    setStatus("loading");
×
79

80
    const assets = buildAssetUrls(datacommon);
×
81
    const response = await getModelExploreData(assets.model, assets.props)?.catch((e) => {
×
82
      Logger.error(e);
×
83
      return null;
×
84
    });
85
    if (!response?.data || !response?.version) {
×
86
      setStatus("error");
×
87
      return;
×
88
    }
89

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

92
    store.dispatch({
×
93
      type: "REACT_FLOW_GRAPH_DICTIONARY",
94
      dictionary: response.data,
95
      pdfDownloadConfig: datacommon.configuration.pdfConfig,
96
      graphViewConfig,
97
    });
98

99
    store.dispatch({
×
100
      type: "RECEIVE_DICTIONARY",
101
      payload: {
102
        data: response.data,
103
        facetfilterConfig: {
104
          ...baseConfiguration,
105
          facetSearchData: datacommon.configuration.facetFilterSearchData,
106
          facetSectionVariables: datacommon.configuration.facetFilterSectionVariables,
107
          baseFilters: buildBaseFilterContainers(datacommon),
108
          filterSections: datacommon.configuration.facetFilterSearchData.map((s) => s?.datafield),
×
109
          filterOptions: buildFilterOptionsList(datacommon),
110
        },
111
        pageConfig: {
112
          title: datacommon.configuration.pageTitle,
113
          iconSrc: datacommon.configuration?.titleIconSrc,
114
        },
115
        readMeConfig: {
116
          readMeUrl: assets.readme,
117
          readMeTitle: datacommon.configuration?.readMeTitle || defaultReadMeTitle,
×
118
          allowDownload: false,
119
        },
120
        pdfDownloadConfig: datacommon.configuration.pdfConfig,
121
        loadingExampleConfig: {
122
          type: "static",
123
          url: assets.loading_file,
124
        },
125
        graphViewConfig,
126
      },
127
    });
128

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

134
    setStatus("success");
×
135
  };
136

137
  return [{ status, store }, resetStore, populateStore];
×
138
};
139

140
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