This commit is contained in:
Viet An
2026-06-04 13:57:27 +07:00
parent 2981d9790a
commit 2a1f85c2af
44 changed files with 99 additions and 102 deletions

View File

@@ -1160,43 +1160,44 @@ export default defineNuxtPlugin((nuxtApp) => {
};
// insert data
const insertapi = async function (name, data, values, notify) {
const insertapi = async function (name, { data, values, notify = true } = {}) {
try {
const found = findapi(name);
const curpath = found.path ? paths.find((x) => x.name === found.path).url : path;
const api = findapi(name);
const curpath = api.path ? paths.find((x) => x.name === api.path).url : path;
let rs;
if (Array.isArray(data)) {
rs = await $fetch(`${curpath}import-data/${found.url.substring(5, found.url.length - 1)}/`, {
rs = await $fetch(`${curpath}import-data/${api.url.substring(5, api.url.length - 1)}/`, {
method: "POST",
body: data,
params: { values, action: "import" },
});
} else {
rs = await $fetch(`${curpath}${found.url}`, {
rs = await $fetch(`${curpath}${api.url}`, {
method: "POST",
body: data,
params: { values },
});
}
// update store
if (found.commit) {
if ($store[found.commit]) {
const copy = $copy($store[found.commit]);
if (api.commit) {
if ($store[api.commit]) {
const copy = $copy($store[api.commit]);
const rows = Array.isArray(rs) ? rs : [rs];
rows.forEach((v) => {
if (v.id && !v.error) {
let idx = copy.findIndex((x) => x.id === v.id);
const idx = copy.findIndex((x) => x.id === v.id);
if (idx >= 0) copy[idx] = v;
else copy.push(v);
}
});
$store.commit(found.commit, copy);
$store.commit(api.commit, copy);
}
}
if (notify !== false) {
$store.lang === "en"
? $snackbar("Data has been successfully saved to the system.", "Success")
: $snackbar("Dữ liệu đã được lưu vào hệ thống", "Success");
if (notify) {
$snackbar(
$store.lang === "en" ? "Data has been successfully saved to the system." : "Dữ liệu đã được lưu vào hệ thống",
"Success",
);
}
return rs;
} catch (err) {
@@ -1394,8 +1395,8 @@ export default defineNuxtPlugin((nuxtApp) => {
};
// insert row
var insertrow = async function (name, data, values, pagename, notify) {
let result = await insertapi(name, data, values, notify);
const insertrow = async function (name, data, values, pagename, notify) {
let result = await insertapi(name, { data, values, notify });
if (result === "error" || !pagename || !$store[pagename]) return result;
let copy = $clone($store[pagename]);
copy.update = { refresh: true };