PromucFlow_constructor/app/client/src/selectors/templatesSelectors.tsx

183 lines
4.9 KiB
TypeScript
Raw Normal View History

2022-03-31 05:16:04 +00:00
import { FilterKeys, Template } from "api/TemplatesApi";
2022-03-03 10:56:53 +00:00
import Fuse from "fuse.js";
import { AppState } from "reducers";
import { createSelector } from "reselect";
import { getOrganizationCreateApplication } from "./applicationSelectors";
2022-03-10 14:39:05 +00:00
import { getWidgetCards } from "./editorSelectors";
2022-03-03 10:56:53 +00:00
import { getDefaultPlugins } from "./entitiesSelector";
2022-03-10 14:39:05 +00:00
import { Filter } from "pages/Templates/Filters";
2022-03-03 10:56:53 +00:00
const fuzzySearchOptions = {
keys: ["title", "id", "datasources", "widgets"],
2022-03-03 10:56:53 +00:00
shouldSort: true,
threshold: 0.5,
location: 0,
distance: 100,
};
export const getTemplatesSelector = (state: AppState) =>
state.ui.templates.templates;
export const isImportingTemplateSelector = (state: AppState) =>
state.ui.templates.isImportingTemplate;
export const showTemplateNotificationSelector = (state: AppState) =>
state.ui.templates.templateNotificationSeen;
export const getOrganizationForTemplates = createSelector(
getOrganizationCreateApplication,
(organizationList) => {
if (organizationList.length) {
return organizationList[0];
}
return null;
},
);
export const getTemplateFilterSelector = (state: AppState) =>
state.ui.templates.filters;
export const getTemplateFiltersLength = createSelector(
getTemplateFilterSelector,
(filters) => {
return Object.values(filters)
.map((filterList) => filterList.length)
.reduce((c, a) => c + a, 0);
},
);
export const isFetchingTemplatesSelector = (state: AppState) =>
state.ui.templates.gettingAllTemplates;
2022-03-31 05:16:04 +00:00
export const isFetchingTemplateSelector = (state: AppState) =>
state.ui.templates.gettingTemplate;
2022-03-03 10:56:53 +00:00
export const getTemplateById = (id: string) => (state: AppState) => {
return state.ui.templates.templates.find((template) => template.id === id);
};
2022-03-31 05:16:04 +00:00
export const getActiveTemplateSelector = (state: AppState) =>
state.ui.templates.activeTemplate;
2022-03-03 10:56:53 +00:00
export const getFilteredTemplateList = createSelector(
getTemplatesSelector,
getTemplateFilterSelector,
2022-03-31 05:16:04 +00:00
getTemplateFiltersLength,
(templates, templatesFilters, numberOfFiltersApplied) => {
const result: Template[] = [];
if (!numberOfFiltersApplied) {
return templates;
}
if (!Object.keys(templatesFilters).length) {
return templates;
2022-03-03 10:56:53 +00:00
}
2022-03-31 05:16:04 +00:00
Object.keys(templatesFilters).map((filter) => {
templates.map((template) => {
if (
template[filter as FilterKeys].some((templateFilter) => {
return templatesFilters[filter].includes(templateFilter);
})
) {
result.push(template);
}
});
});
return result;
2022-03-03 10:56:53 +00:00
},
);
export const getTemplateSearchQuery = (state: AppState) =>
state.ui.templates.templateSearchQuery;
export const getSearchedTemplateList = createSelector(
getFilteredTemplateList,
getTemplateSearchQuery,
(templates, query) => {
if (!query) {
return templates;
}
const fuzzy = new Fuse(templates, fuzzySearchOptions);
return fuzzy.search(query);
},
);
export const templatesDatasourceFiltersSelector = createSelector(
getDefaultPlugins,
(plugins) => {
return plugins.map((plugin) => {
return {
label: plugin.name,
value: plugin.packageName,
};
});
},
);
2022-03-10 14:39:05 +00:00
// Get all filters which is associated with atleast one template
// If no template is associated with a filter, then the filter shouldn't be in the filter list
export const getFilterListSelector = createSelector(
getWidgetCards,
templatesDatasourceFiltersSelector,
getTemplatesSelector,
(widgetConfigs, allDatasources, templates) => {
const filters: Record<string, Filter[]> = {
datasources: [],
widgets: [],
};
const allWidgets = widgetConfigs.map((widget) => {
return {
label: widget.displayName,
value: widget.type,
};
});
const filterFilters = (
key: "datasources" | "widgets" | "useCases" | "functions",
dataReference: Filter[],
template: Template,
) => {
template[key].map((templateValue) => {
if (
!filters[key].some((filter) => {
if (filter.value) {
return filter.value === templateValue;
}
return filter.label === templateValue;
})
) {
const filteredData = dataReference.find((datum) => {
if (datum.value) {
return datum.value === templateValue;
}
return datum.label === templateValue;
});
filteredData && filters[key].push(filteredData);
}
});
};
templates.map((template) => {
filterFilters("datasources", allDatasources, template);
filterFilters("widgets", allWidgets, template);
});
return filters;
},
);
2022-03-03 10:56:53 +00:00
export const getForkableOrganizations = createSelector(
getOrganizationCreateApplication,
(organisations) => {
return organisations.map((organization) => {
return {
label: organization.organization.name,
value: organization.organization.id,
};
});
},
);