@devgateway/dvz-ui-react
Version:
A modular, embeddable React component library for data visualization and UI, built with TypeScript. Provides reusable components for charts, maps, dashboards, and more, with built-in support for internationalization and Redux integration.
210 lines (209 loc) • 8.57 kB
JavaScript
import { getData as M, getCategories as w } from "./data-api.js";
import * as _ from "immutable";
import b from "papaparse";
const d = "LOAD_DATA", f = "LOAD_DATA_DONE", O = "LOAD_DATA_ERROR", F = "LOAD_CATEGORIES", R = "LOAD_CATEGORIES_DONE", D = "LOAD_CATEGORIES_ERROR", L = "SET_PAGE_MODULE_PROPS", A = "SET_FILTER", m = "SET_INITIAL_FILTER", G = "SET_POSTS_FILTER", v = "SET_INITIAL_POSTS_FILTER", U = "SET_POSTS_PAGINATION", P = "SET_APPLY", h = "CLEAN_FILTER", q = _.Map({ mode: "info" }), C = "SET_MEASURES", N = "CLEAN_MEASURES", J = ({ app: n, group: o }) => (e, s) => {
e({ type: N, app: n, group: o });
}, V = ({ app: n, group: o, mGroup: e }) => (s, t) => {
const r = Object.keys(e.measures).filter((l) => e.measures[l].selected), a = { ...e };
a[n] = { measures: {} }, r.forEach((l) => {
a[n].measures[l] = { ...e.measures[l] };
}), a[n].format = e.format, s({ type: C, app: n, group: o, measure: a });
}, Y = ({ app: n, group: o, param: e, value: s, autoApply: t }) => (r, a) => {
r({ type: A, app: n, group: o, param: e, value: s, autoApply: t });
}, $ = ({ app: n, group: o }) => (e, s) => {
e({ type: h, app: n, group: o });
const r = s().getIn(["data", "filters", "initial", n, o]);
if (r) {
const a = r.entrySeq ? r.entrySeq().toArray() : Object.entries(r);
for (const [l, i] of a)
e({
type: A,
app: n,
group: o,
param: l,
value: i,
autoApply: !0
});
}
}, H = ({ app: n, group: o }) => (e, s) => {
e({ type: P, app: n, group: o });
}, K = ({ app: n, group: o, param: e, value: s }) => (t, r) => {
t({ type: m, app: n, group: o, param: e, value: s });
}, Q = (n) => (o, e) => {
const { app: s, params: t, dvzProxyDatasetId: r, uniqueStorage: a } = n;
o({ type: F, params: t, app: s, uniqueStorage: a, dvzProxyDatasetId: r }), w({ app: s, params: t }).then((l) => (l.appliedFilters = t, o({
type: R,
app: s,
data: l,
uniqueStorage: a,
dvzProxyDatasetId: r
}))).catch((l) => o({
type: D,
app: s,
uniqueStorage: a,
error: l,
dvzProxyDatasetId: r
}));
}, W = ({ app: n, group: o, csv: e, store: s, params: t }) => (r, a) => {
const l = a().get("data").getIn(["filters", n, o]);
l ? t = { ...t, ...l.toJS() } : t = t || {};
const i = b.parse(e, { header: !0, dynamicTyping: !0 }), c = i.data.filter((y) => {
let g = !1;
return Object.keys(t).forEach((I) => {
const S = t[I];
if (y[I]) {
const T = y[I].toString().trim().toLowerCase();
g = S.filter((p) => p && p.toString().trim().toLowerCase() == T).length == 0;
}
}), !g;
}), u = { ...i, data: c, appliedFilters: t };
r({ type: f, app: n, group: o, store: s, data: { count: u.data.length, itemsSize: u.data.length, ...u } });
}, X = ({ app: n, group: o, source: e, store: s, params: t }) => (r, a) => {
let l = a().get("data").getIn(["filters", n, o]);
t && Object.keys(t).forEach((c) => {
if (l && l.has(c)) {
let u = t[c], g = l.get(c).filter((I) => u.indexOf(I) > -1);
l = l.set(c, g);
}
}), l && (t = { ...t, ...l.toJS() }), r({ type: d, app: n, group: o, params: t, store: s }), M({ app: n, source: e, params: t }).then((i) => (i.appliedFilters = t, r({ type: f, app: n, group: o, store: s, data: i, params: t }))).catch((i) => r({ type: O, app: n, group: o, store: s, error: i }));
}, Z = ({ data: n }) => (o, e) => {
o({ type: L, data: n });
}, k = (n = q, o) => {
switch (o.type) {
case d: {
const { store: e, app: s, group: t, params: r } = o, a = Date.now();
return n.deleteIn([...e, "error"]).setIn([...e, "loading"], !0).setIn([...e, "time"], a).setIn([...e, "presetFilter"], r);
}
case O: {
const { error: e, store: s, app: t, group: r } = o;
return n.setIn([...s, "loading"], !1).setIn([...s, "error"], e).setIn(["filters-settings", t, r, "apply"], null);
}
case f: {
const { data: e, app: s, group: t, store: r } = o;
return n.setIn([...r, "loading"], !1).deleteIn([...r, "error"]).setIn([...r, "data"], e).setIn(["filters-settings", s, t, "apply"], null);
}
case L: {
const { data: e } = o;
return n.setIn(["pageModuleProps"], e);
}
case F: {
const { data: e, app: s, uniqueStorage: t, dvzProxyDatasetId: r } = o, a = ["categories", s];
return t && a.push(t), r && a.push(r), n.setIn([...a, "loading"], !0).deleteIn([...a, "error"]);
}
case R: {
const { data: e, app: s, uniqueStorage: t, dvzProxyDatasetId: r } = o, a = ["categories", s];
return r && a.push(r), t && a.push(t), n.setIn([...a, "loading"], !1).setIn([...a, "items"], _.fromJS(e));
}
case D: {
const { data: e, app: s, uniqueStorage: t, dvzProxyDatasetId: r } = o, a = ["categories", s];
return t && a.push(t), r && a.push(r), n.setIn([...a, "loading"], !1).setIn([...a, "error"], e);
}
case P: {
const { app: e, group: s } = o;
return n.setIn(["filters-settings", e, s, "apply"], (/* @__PURE__ */ new Date()).getTime());
}
case A: {
const e = Date.now(), { app: s, group: t, param: r, value: a, autoApply: l } = o;
return n.setIn(["filters-settings", s, t, "autoApply"], l).setIn(["filters", s, t, r], a.length === 0 ? [Number.MIN_SAFE_INTEGER] : a).setIn(["filters-settings", s, t, "apply"], null).setIn(["filters-settings", s, t, "lastUserFilterChange"], e);
}
case m: {
const e = Date.now(), { app: s, group: t, param: r, value: a } = o;
return console.log(r), n.setIn(["filters", "initial", s, t, r], a.length === 0 ? [Number.MIN_SAFE_INTEGER] : a).setIn(["filters", s, t, r], a.length === 0 ? [Number.MIN_SAFE_INTEGER] : a).setIn(["filters-settings", s, t, "lastInitialFilterChange"], e);
}
case h: {
const { app: e, group: s } = o, t = n.getIn(["filters", "initial", e, s]);
return n.setIn(["filters", e, s], t);
}
case v: {
const {
group: e,
sortFirstBy: s,
categoryCategory: t,
countryCategory: r,
categoryTaxonomy: a,
countryTaxonomy: l,
categoryFilter: i,
countryFilter: c,
yearFilter: u
} = o, y = Number(s), g = {
yearFilter: u,
categoryFilter: i,
categoryTaxonomy: a,
categoryCategory: t,
countryCategory: r,
countryFilter: c,
countryTaxonomy: l,
page: void 0,
itemsPerPage: void 0,
sortFirstBy: y,
postTaxonomy: null,
postCategory: null,
postType: null
};
return n.setIn(["posts", e], g).setIn(["posts", "initialFilters", e], g);
}
case G: {
const {
group: e,
yearFilter: s,
categoryFilter: t,
countryFilter: r,
page: a,
itemsPerPage: l,
sortFirstBy: i,
countryCategory: c,
categoryCategory: u,
taxonomy: y,
categoryTaxonomy: g,
countryTaxonomy: I
} = o, S = n.getIn(["posts", e]) || {}, T = {
yearFilter: s,
categoryFilter: t,
countryFilter: r,
page: a,
itemsPerPage: l,
sortFirstBy: i,
countryCategory: c,
// preserve or set taxonomy helpers needed by filters UI and querying
categoryCategory: u !== void 0 ? u : S.categoryCategory,
taxonomy: y,
categoryTaxonomy: g !== void 0 ? g : S.categoryTaxonomy,
countryTaxonomy: I !== void 0 ? I : S.countryTaxonomy
};
let p = n.setIn(["posts", e], T);
const E = n.getIn(["posts", "initialFilters", e]);
if ((!E || E && E.yearFilter === null && E.categoryFilter === null && E.countryFilter === null && E.categoryCategory === null) && (Array.isArray(t) && t.length > 0 || Array.isArray(r) && r.length > 0)) {
const x = { ...T };
p = p.setIn(["posts", "initialFilters", e], x);
}
return p;
}
case U: {
const { group: e, totalPages: s, totalItems: t } = o;
return n.setIn(["postsPagination", e], { totalPages: s, totalItems: t });
}
case C: {
const { app: e, group: s, measure: t } = o;
return n.setIn(["measures", e, s], t);
}
case N: {
const { app: e, group: s, measure: t } = o;
return n.deleteIn(["measures", e, s]);
}
default:
return n;
}
};
export {
H as applyFilter,
$ as cleanFilter,
J as cleanMeasures,
k as default,
Q as getCategories,
X as getData,
W as setData,
Y as setFilter,
K as setInitialFilters,
V as setMeasures,
Z as setPageModuleProps
};