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

CBIIT / crdc-datahub-ui / 12936221948

23 Jan 2025 07:03PM UTC coverage: 57.932% (-0.02%) from 57.952%
12936221948

Pull #605

github

web-flow
Merge 9c041f8e4 into bc7a90325
Pull Request #605: CRDCDH-2266 Support Viewing Older Data Model Versions

2807 of 5284 branches covered (53.12%)

Branch coverage included in aggregate %.

6 of 17 new or added lines in 6 files covered. (35.29%)

45 existing lines in 1 file now uncovered.

4033 of 6523 relevant lines covered (61.83%)

144.5 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, modelVersion: string) => void,
27
];
28

29
const makeStore = (): Store => {
×
30
  const reducers = { ddgraph, versionInfo, submission };
×
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
 */
47
const useBuildReduxStore = (): ReduxStoreResult => {
×
48
  const [status, setStatus] = useState<ReduxStoreStatus>("waiting");
×
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
   * @param modelVersion The version of the Data Model to inject
74
   */
NEW
75
  const populateStore = async (datacommon: DataCommon, modelVersion: string) => {
×
76
    if (
×
77
      !datacommon?.name ||
×
78
      !datacommon?.assets ||
79
      !datacommon?.assets["current-version"] ||
80
      !datacommon.configuration?.pdfConfig
81
    ) {
82
      setStatus("error");
×
83
      return;
×
84
    }
85

86
    setStatus("loading");
×
87

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

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

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

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

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

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

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

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

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

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

173
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

© 2025 Coveralls, Inc