changes
This commit is contained in:
@@ -1134,7 +1134,6 @@ export default defineNuxtPlugin((nuxtApp) => {
|
||||
return apisWithReady;
|
||||
};
|
||||
|
||||
// get data
|
||||
const getapi = async function (list) {
|
||||
try {
|
||||
const arr = list.map((v) => {
|
||||
@@ -1159,7 +1158,6 @@ export default defineNuxtPlugin((nuxtApp) => {
|
||||
}
|
||||
};
|
||||
|
||||
// insert data
|
||||
const insertapi = async function (name, { data, values, notify = true } = {}) {
|
||||
try {
|
||||
const api = findapi(name);
|
||||
@@ -1206,64 +1204,43 @@ export default defineNuxtPlugin((nuxtApp) => {
|
||||
}
|
||||
};
|
||||
|
||||
// update api
|
||||
const updateapi = async function (name, data, values, notify) {
|
||||
try {
|
||||
let found = findapi(name);
|
||||
let curpath = found.path ? paths.find((x) => x.name === found.path).url : path;
|
||||
let updateUrl = found.url_detail ? found.url_detail : found.url;
|
||||
let rs = await $fetch(`${curpath}${updateUrl}${data.id}/`, {
|
||||
method: "PUT",
|
||||
body: data,
|
||||
params: { values: values || found.params.values },
|
||||
});
|
||||
if (found.commit) {
|
||||
let index = $store[found.commit] ? $store[found.commit].findIndex((v) => v.id === rs.id) : -1;
|
||||
if (index >= 0) {
|
||||
var copy = $copy($store[found.commit]);
|
||||
if (Array.isArray(rs) === false) copy[index] = rs;
|
||||
else {
|
||||
rs.forEach((v) => {
|
||||
let index = copy.findIndex((v) => v.id === v.id);
|
||||
if (index >= 0) copy[index] = v;
|
||||
});
|
||||
}
|
||||
$store.commit(found.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");
|
||||
}
|
||||
return rs;
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
return "error";
|
||||
}
|
||||
};
|
||||
|
||||
// patch api
|
||||
const patchapi = async function (name, data, values, notify) {
|
||||
try {
|
||||
let found = findapi(name);
|
||||
let curpath = found.path ? paths.find((x) => x.name === found.path).url : path;
|
||||
let updateUrl = found.url_detail ? found.url_detail : found.url;
|
||||
const api = findapi(name);
|
||||
const curpath = api.path ? paths.find((x) => x.name === api.path).url : path;
|
||||
const updateUrl = api.url_detail || api.url;
|
||||
|
||||
const rs = await $fetch(`${curpath}${updateUrl}${data.id}/`, {
|
||||
method: "PATCH",
|
||||
body: data,
|
||||
params: { values: values || found.params?.values },
|
||||
params: { values: values || api.params?.values },
|
||||
});
|
||||
|
||||
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 (api.commit) {
|
||||
const index = $store[api.commit] ? $store[api.commit].findIndex((v) => v.id === rs.id) : -1;
|
||||
if (index >= 0) {
|
||||
const copy = $copy($store[api.commit]);
|
||||
if (Array.isArray(rs)) {
|
||||
rs.forEach((r) => {
|
||||
const index = copy.findIndex((v) => v.id === r.id);
|
||||
if (index >= 0) copy[index] = r;
|
||||
});
|
||||
} else {
|
||||
copy[index] = rs;
|
||||
}
|
||||
$store.commit(api.commit, copy);
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
console.log(err);
|
||||
console.error(err);
|
||||
return "error";
|
||||
}
|
||||
};
|
||||
@@ -1335,10 +1312,10 @@ export default defineNuxtPlugin((nuxtApp) => {
|
||||
};
|
||||
|
||||
const getdata = async function (name, { filter, params, first = false } = {}) {
|
||||
const found = findapi(name);
|
||||
if (params) found.params = params;
|
||||
else if (filter) found.params.filter = filter;
|
||||
const rs = await getapi([found]);
|
||||
const api = findapi(name);
|
||||
if (params) api.params = params;
|
||||
else if (filter) api.params.filter = filter;
|
||||
const rs = await getapi([api]);
|
||||
const { data } = rs[0];
|
||||
if (data) {
|
||||
if (data.rows) {
|
||||
@@ -1394,7 +1371,6 @@ export default defineNuxtPlugin((nuxtApp) => {
|
||||
subscribeToData(payload, callback);
|
||||
};
|
||||
|
||||
// insert row
|
||||
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;
|
||||
@@ -1404,18 +1380,7 @@ export default defineNuxtPlugin((nuxtApp) => {
|
||||
return result;
|
||||
};
|
||||
|
||||
// update row
|
||||
const updaterow = async function (name, data, values, pagename, notify) {
|
||||
let result = await updateapi(name, data, values, notify);
|
||||
if (result === "error" || !pagename || !$store[pagename]) return result;
|
||||
let copy = $clone($store[pagename]);
|
||||
copy.update = { refresh: true };
|
||||
$store.commit(pagename, copy);
|
||||
return result;
|
||||
};
|
||||
|
||||
// patch row
|
||||
const patchrow = async function (name, data, values, pagename, notify) {
|
||||
let result = await patchapi(name, data, values, notify);
|
||||
if (result === "error" || !pagename || !$store[pagename]) return result;
|
||||
let copy = $clone($store[pagename]);
|
||||
@@ -1683,7 +1648,6 @@ export default defineNuxtPlugin((nuxtApp) => {
|
||||
getdata,
|
||||
subscribe,
|
||||
insertapi,
|
||||
updateapi,
|
||||
patchapi,
|
||||
updaterow,
|
||||
findpage,
|
||||
|
||||
Reference in New Issue
Block a user