This commit is contained in:
2020-06-19 22:15:32 +00:00
parent bfcae2a750
commit ab549628e0
2 changed files with 124 additions and 103 deletions

View File

@@ -423,6 +423,21 @@ export default {
/////////////////////////////////// ///////////////////////////////////
// GET DATA FROM API SERVER // GET DATA FROM API SERVER
// //
async getEx(route) {
try {
let that = this;
let r = await fetch(that.APIUrl(route), that.fetchGetOptions());
that.statusEx(r);
r = await that.extractBodyEx(r);
return r;
} catch (error) {
//fundamental error, can't proceed with this call
handleError("GET", error, route, reject);
}
},
///////////////////////////////////
// GET DATA FROM API SERVER
//
get(route) { get(route) {
let that = this; let that = this;
return new Promise(function getDataFromServer(resolve, reject) { return new Promise(function getDataFromServer(resolve, reject) {
@@ -489,6 +504,22 @@ export default {
/////////////////////////////////// ///////////////////////////////////
// DELETE DATA FROM API SERVER // DELETE DATA FROM API SERVER
// //
async removeEx(route) {
let that = this;
try {
let r = await fetch(that.APIUrl(route), that.fetchRemoveOptions());
that.statusEx(r);
//delete will return a body if there is an error of some kind with the request
r = await that.extractBodyEx(r);
return r;
} catch (error) {
//fundamental error, can't proceed with this call
handleError("DELETE", error, route);
}
},
///////////////////////////////////
// DELETE DATA FROM API SERVER
//
remove(route) { remove(route) {
let that = this; let that = this;
return new Promise(function removeDataFromServer(resolve, reject) { return new Promise(function removeDataFromServer(resolve, reject) {

View File

@@ -423,140 +423,130 @@ export default {
window.$gz.form.fieldValueChanged(this, ref); window.$gz.form.fieldValueChanged(this, ref);
} }
}, },
getDataFromApi(recordId) { async getDataFromApi(recordId) {
let vm = this; let vm = this;
vm.formState.loading = true; vm.formState.loading = true;
if (!recordId) { if (!recordId) {
throw FORM_KEY + "::getDataFromApi -> Missing recordID!"; throw FORM_KEY + "::getDataFromApi -> Missing recordID!";
} }
let url = API_BASE_URL + recordId; let url = API_BASE_URL + recordId;
try {
window.$gz.form.deleteAllErrorBoxErrors(vm);
window.$gz.form.deleteAllErrorBoxErrors(vm); let res = await window.$gz.api.getEx(url);
window.$gz.api if (res.error) {
.get(url) //Not found?
.then(res => { if (res.error.code == "2010") {
if (res.error) { //notify not found error then navigate backwards
//Not found? window.$gz.eventBus.$emit("notify-error", vm.$ay.t("ErrorAPI2010"));
if (res.error.code == "2010") { // navigate backwards
//notify not found error then navigate backwards window.$gz._.delay(function() {
window.$gz.eventBus.$emit( vm.$router.go(-1);
"notify-error", }, 2000);
vm.$ay.t("ErrorAPI2010")
);
// navigate backwards
window.$gz._.delay(function() {
vm.$router.go(-1);
}, 2000);
}
vm.formState.serverError = res.error;
window.$gz.form.setErrorBoxErrors(vm);
} else {
vm.obj = res.data;
//Update the form status
window.$gz.form.setFormState({
vm: vm,
dirty: false,
valid: true,
loading: false
});
//modify the menu as necessary
generateMenu(vm);
} }
}) vm.formState.serverError = res.error;
.catch(function handleGetDataFromAPIError(error) { window.$gz.form.setErrorBoxErrors(vm);
} else {
vm.obj = res.data;
//Update the form status //Update the form status
window.$gz.form.setFormState({ window.$gz.form.setFormState({
vm: vm, vm: vm,
dirty: false,
valid: true,
loading: false loading: false
}); });
window.$gz.errorHandler.handleFormError(error, vm);
//modify the menu as necessary
generateMenu(vm);
}
} catch (error) {
//Update the form status
window.$gz.form.setFormState({
vm: vm,
loading: false
}); });
window.$gz.errorHandler.handleFormError(error, vm);
}
}, },
submit() { async submit() {
let vm = this; let vm = this;
if (vm.canSave) { if (vm.canSave) {
vm.formState.loading = true; vm.formState.loading = true;
let url = API_BASE_URL; // + vm.$route.params.recordid; let url = API_BASE_URL; // + vm.$route.params.recordid;
//clear any errors vm might be around from previous submit try {
window.$gz.form.deleteAllErrorBoxErrors(vm); //clear any errors vm might be around from previous submit
window.$gz.api window.$gz.form.deleteAllErrorBoxErrors(vm);
.upsert(url, vm.obj) let res = await window.$gz.api.upsert(url, vm.obj);
.then(res => { vm.formState.loading = false;
vm.formState.loading = false; if (res.error) {
if (res.error) { vm.formState.serverError = res.error;
vm.formState.serverError = res.error; window.$gz.form.setErrorBoxErrors(vm);
window.$gz.form.setErrorBoxErrors(vm); } else {
} else { //Logic for detecting if a post or put: if id then it was a post, if no id then it was a put
//Logic for detecting if a post or put: if id then it was a post, if no id then it was a put if (res.data.id) {
if (res.data.id) { //Handle "post" of new record (CREATE)
//Handle "post" of new record (CREATE)
//change url to new record in history //change url to new record in history
//NOTE: will not cause a page re-render, almost nothing does unless forced with a KEY property or using router.GO() //NOTE: will not cause a page re-render, almost nothing does unless forced with a KEY property or using router.GO()
//but will trigger navigation guard beforeRouteUpdate which we use here in this form to fetch data freshly //but will trigger navigation guard beforeRouteUpdate which we use here in this form to fetch data freshly
vm.$router.replace( vm.$router.replace(vm.$route.fullPath.slice(0, -1) + res.data.id);
vm.$route.fullPath.slice(0, -1) + res.data.id } else {
); //Handle "put" of an existing record (UPDATE)
} else { vm.obj.concurrency = res.data.concurrency;
//Handle "put" of an existing record (UPDATE) window.$gz.form.setFormState({
vm.obj.concurrency = res.data.concurrency; vm: vm,
window.$gz.form.setFormState({ dirty: false
vm: vm, });
dirty: false
});
}
} }
}) }
.catch(function handleSubmitError(error) { } catch (error) {
vm.formState.loading = false; vm.formState.loading = false;
window.$gz.errorHandler.handleFormError(error, vm); window.$gz.errorHandler.handleFormError(error, vm);
}); }
} }
}, },
remove() { async remove() {
let vm = this; let vm = this;
window.$gz.dialog.confirmDelete().then(dialogResult => { try {
if (dialogResult == true) { let dialogResult = await window.$gz.dialog.confirmDelete();
//do the delete if (dialogResult != true) {
vm.formState.loading = true; return;
//No need to delete a new record, just abandon it... }
if (vm.$route.params.recordid == 0) {
//this should not get offered for delete but to be safe and clear just in case: //do the delete
vm.formState.loading = true;
//No need to delete a new record, just abandon it...
if (vm.$route.params.recordid == 0) {
//this should not get offered for delete but to be safe and clear just in case:
JUST_DELETED = true;
// navigate backwards
vm.$router.go(-1);
} else {
let url = API_BASE_URL + vm.$route.params.recordid;
window.$gz.form.deleteAllErrorBoxErrors(vm);
let res = await window.$gz.api.removeEx(url);
if (res.error) {
vm.formState.serverError = res.error;
window.$gz.form.setErrorBoxErrors(vm);
} else {
//workaround to prevent warning about leaving dirty record
//For some reason I couldn't just reset isdirty in formstate
JUST_DELETED = true; JUST_DELETED = true;
// navigate backwards // navigate backwards
vm.$router.go(-1); vm.$router.go(-1);
} else {
let url = API_BASE_URL + vm.$route.params.recordid;
window.$gz.form.deleteAllErrorBoxErrors(vm);
window.$gz.api
.remove(url)
.then(res => {
if (res.error) {
vm.formState.serverError = res.error;
window.$gz.form.setErrorBoxErrors(vm);
} else {
//workaround to prevent warning about leaving dirty record
//For some reason I couldn't just reset isdirty in formstate
JUST_DELETED = true;
// navigate backwards
vm.$router.go(-1);
}
})
.catch(function handleGetDataFromAPIError(error) {
//Update the form status
window.$gz.form.setFormState({
vm: vm,
loading: false
});
window.$gz.errorHandler.handleFormError(error, vm);
});
} }
} }
}); } catch (error) {
//Update the form status
window.$gz.form.setFormState({
vm: vm,
loading: false
});
window.$gz.errorHandler.handleFormError(error, vm);
}
}, },
duplicate() { duplicate() {
let vm = this; let vm = this;