@mee4dy/crud
Version:
Create a backend and frontend in 5 minutes! With our powerful full stack crud system, customize it to suit you.
139 lines (138 loc) • 4.83 kB
JavaScript
import * as g from "lodash";
import p from "qs";
const h = {
setCtx({ getters: n, dispatch: o, state: c, commit: r }) {
r("setCtx", { getters: n, dispatch: o, commit: r, state: c });
},
setQuery({ commit: n, dispatch: o }, c) {
n("setQuery", c), o("setQuerySelectedFilters", c), o("setQuerySelectedGroups", c), o("setQuerySelectedOrders", c);
},
setQuerySelectedFilters({ commit: n, state: o, getters: c }, r) {
const e = c.getFilters, a = r, s = {};
for (let t in a) {
const i = a[t];
t === "pk" && (a[o.pk] = i, t = o.pk), e.find((d) => d.key === t) && (s[t] = i);
}
n("setSelectedFilters", s);
},
setQuerySelectedGroups({ commit: n, state: o, getters: c }, r) {
const e = c.getGroups.map((t) => t.key), a = r != null && r.groups ? r == null ? void 0 : r.groups.split(",") : [], s = [];
for (let t of a)
t === "pk" && (t = o.pk), e.includes(t) && s.push(t);
n("setSelectedGroups", s);
},
setQuerySelectedOrders({ commit: n, state: o, getters: c }, r) {
const e = c.getOrders, a = r != null && r.orders ? r == null ? void 0 : r.orders.split(",") : [], s = {};
for (let t of a) {
const i = t.charAt(0) === "-";
i && (t = t.substring(1)), t === "pk" && (a[o.pk] = a[t], t = o.pk), e.includes(t) && (s[t] = i ? "desc" : "asc");
}
n("setSelectedOrders", s);
},
syncSelectedToQuery({ commit: n, state: o, getters: c }) {
const r = c.getSelectedFilters, e = c.getSelectedGroups.join(","), a = Object.entries(c.getSelectedOrders).map(([d, l]) => `${d === "desc" ? "-" : ""}${l}`).join(","), s = {
filters: r,
groups: e,
orders: a
}, t = new URLSearchParams(window.location.search);
for (const d in s) {
const l = s[d];
l && (typeof l == "object" ? Object.entries(c.getSelectedFilters).forEach(([f, u]) => {
t.set(f, u.toString());
}) : t.set(d, l));
}
const i = decodeURIComponent(t.toString());
history.pushState(null, null, i ? `?${i}` : "");
},
async setItems({ commit: n, state: o, getters: c, dispatch: r }, e) {
await r("setCtx"), n("setItems", e);
},
async fetch({ commit: n, state: o, getters: c, dispatch: r }, e) {
const a = c.getEndpoint("fetch");
if (a)
try {
n("setLoading", !0);
const s = c.getParams, t = c.getState("config.client.abort"), i = c.getState("config.client.cancelToken");
e != null && e.applyQuery && r("syncSelectedToQuery"), e != null && e.filters && (s.filters = e.filters), e != null && e.groups && (s.groups = e.groups), e != null && e.orders && (s.orders = e.orders), t && i && i.cancel();
const l = this.$axios.CancelToken.source();
n("setState", {
path: "config.client.cancelToken",
value: l
});
const f = await this.$axios.get(a, {
cancelToken: l.token,
params: s,
paramsSerializer: (S) => p.stringify(S, { arrayFormat: "brackets" })
});
let u = f.data.data.items;
return r("setItems", u), f == null ? void 0 : f.data;
} catch (s) {
console.log(s);
} finally {
n("setLoading", !1), n("setState", {
path: "config.client.cancelToken",
value: null
});
}
},
async create({ commit: n, state: o, dispatch: c, getters: r }, { data: e }) {
const a = r.getEndpoint("create");
if (a)
try {
const s = await this.$axios.post(a, {
data: e
});
return s == null ? void 0 : s.data;
} catch (s) {
console.log(s);
}
},
async update({ commit: n, state: o, dispatch: c, getters: r }, { pk: e, data: a, level: s }) {
if (s && s.path && s.parentPK) {
const i = g.toPath(s.path)[0];
await c(`${i}/update`, { pk: e, data: a }, { root: !0 }), n("update", {
pk: e,
data: a,
level: s
});
return;
}
const t = r.getEndpoint("update", e);
if (!(!t || !e))
try {
const i = await this.$axios.post(t, {
pk: e,
data: a
});
return n("update", {
pk: e,
data: a
}), i == null ? void 0 : i.data;
} catch (i) {
console.log(i);
}
},
async delete({ commit: n, state: o, getters: c, dispatch: r }, { pk: e, level: a }) {
if (a && a.path && a.parentPK) {
const t = g.toPath(a.path)[0];
await r(`${t}/delete`, { pk: e }, { root: !0 }), n("delete", {
pk: e,
level: a
});
return;
}
const s = c.getEndpoint("delete", e);
if (!(!s || !e))
try {
await this.$axios.post(s, {
pk: e
}), n("delete", { pk: e });
} catch (t) {
console.log(t);
}
}
};
export {
h as default
};
//# sourceMappingURL=actions.js.map