PromucFlow_constructor/app/client/src/selectors/dataTreeSelectors.ts

93 lines
2.7 KiB
TypeScript
Raw Normal View History

2020-02-18 10:41:52 +00:00
import { createSelector } from "reselect";
import { getActionsForCurrentPage } from "./entitiesSelector";
2020-02-18 10:41:52 +00:00
import { ActionDataState } from "reducers/entityReducers/actionsReducer";
import { getEvaluatedDataTree } from "utils/DynamicBindingUtils";
import { extraLibraries } from "jsExecution/JSExecutionManagerSingleton";
2020-04-01 08:09:57 +00:00
import {
DataTree,
DataTreeFactory,
DataTreeUrl,
} from "entities/DataTree/dataTreeFactory";
import _ from "lodash";
import { getWidgets, getWidgetsMeta } from "sagas/selectors";
2020-03-23 12:40:17 +00:00
import * as log from "loglevel";
2020-04-01 08:09:57 +00:00
import "url-search-params-polyfill";
function getQueryParams() {
const urlParams = new URLSearchParams(window.location.search);
const keys = urlParams.keys();
let key = keys.next().value;
const queryParams: Record<string, string> = {};
while (key) {
queryParams[key] = urlParams.get(key) as string;
key = keys.next().value;
}
return queryParams;
}
const getUrlParams = createSelector(
getQueryParams,
(queryParams: Record<string, string>): DataTreeUrl => {
return {
host: window.location.host,
hostname: window.location.hostname,
queryParams: queryParams,
protocol: window.location.protocol,
pathname: window.location.pathname,
port: window.location.port,
href: window.location.href,
hash: window.location.hash,
};
},
);
2020-02-18 10:41:52 +00:00
export const getUnevaluatedDataTree = createSelector(
getActionsForCurrentPage,
getWidgets,
getWidgetsMeta,
2020-04-01 08:09:57 +00:00
getUrlParams,
(actions, widgets, widgetsMeta, url) => {
return DataTreeFactory.create({
actions,
widgets,
widgetsMeta,
url,
});
},
);
2020-02-18 10:41:52 +00:00
export const evaluateDataTree = createSelector(
2020-02-18 10:41:52 +00:00
getUnevaluatedDataTree,
(dataTree: DataTree): DataTree => {
return getEvaluatedDataTree(dataTree);
2020-02-18 10:41:52 +00:00
},
);
// For autocomplete. Use actions cached responses if
// there isn't a response already
export const getDataTreeForAutocomplete = createSelector(
evaluateDataTree,
getActionsForCurrentPage,
(tree: DataTree, actions: ActionDataState) => {
2020-03-23 12:40:17 +00:00
log.debug("Evaluating data tree to get autocomplete values");
2020-02-18 10:41:52 +00:00
const cachedResponses: Record<string, any> = {};
if (actions && actions.length) {
actions.forEach(action => {
if (!(action.config.name in tree) && action.config.cacheResponse) {
2020-02-18 10:41:52 +00:00
try {
cachedResponses[action.config.name] = JSON.parse(
action.config.cacheResponse,
);
} catch (e) {
cachedResponses[action.config.name] = action.config.cacheResponse;
}
}
});
}
_.omit(tree, ["MainContainer", "actionPaths"]);
2020-02-18 10:41:52 +00:00
const libs: Record<string, any> = {};
2020-02-25 08:50:40 +00:00
extraLibraries.forEach(config => (libs[config.accessor] = config.lib));
return { ...tree, ...cachedResponses, ...libs };
2020-02-18 10:41:52 +00:00
},
);