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
//
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) {
let that = this;
return new Promise(function getDataFromServer(resolve, reject) {
@@ -489,6 +504,22 @@ export default {
///////////////////////////////////
// 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) {
let that = this;
return new Promise(function removeDataFromServer(resolve, reject) {

View File

@@ -423,140 +423,130 @@ export default {
window.$gz.form.fieldValueChanged(this, ref);
}
},
getDataFromApi(recordId) {
async getDataFromApi(recordId) {
let vm = this;
vm.formState.loading = true;
if (!recordId) {
throw FORM_KEY + "::getDataFromApi -> Missing 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
.get(url)
.then(res => {
if (res.error) {
//Not found?
if (res.error.code == "2010") {
//notify not found error then navigate backwards
window.$gz.eventBus.$emit(
"notify-error",
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);
if (res.error) {
//Not found?
if (res.error.code == "2010") {
//notify not found error then navigate backwards
window.$gz.eventBus.$emit("notify-error", vm.$ay.t("ErrorAPI2010"));
// navigate backwards
window.$gz._.delay(function() {
vm.$router.go(-1);
}, 2000);
}
})
.catch(function handleGetDataFromAPIError(error) {
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
});
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;
if (vm.canSave) {
vm.formState.loading = true;
let url = API_BASE_URL; // + vm.$route.params.recordid;
//clear any errors vm might be around from previous submit
window.$gz.form.deleteAllErrorBoxErrors(vm);
window.$gz.api
.upsert(url, vm.obj)
.then(res => {
vm.formState.loading = false;
if (res.error) {
vm.formState.serverError = res.error;
window.$gz.form.setErrorBoxErrors(vm);
} else {
//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) {
//Handle "post" of new record (CREATE)
try {
//clear any errors vm might be around from previous submit
window.$gz.form.deleteAllErrorBoxErrors(vm);
let res = await window.$gz.api.upsert(url, vm.obj);
vm.formState.loading = false;
if (res.error) {
vm.formState.serverError = res.error;
window.$gz.form.setErrorBoxErrors(vm);
} else {
//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) {
//Handle "post" of new record (CREATE)
//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()
//but will trigger navigation guard beforeRouteUpdate which we use here in this form to fetch data freshly
vm.$router.replace(
vm.$route.fullPath.slice(0, -1) + res.data.id
);
} else {
//Handle "put" of an existing record (UPDATE)
vm.obj.concurrency = res.data.concurrency;
window.$gz.form.setFormState({
vm: vm,
dirty: false
});
}
//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()
//but will trigger navigation guard beforeRouteUpdate which we use here in this form to fetch data freshly
vm.$router.replace(vm.$route.fullPath.slice(0, -1) + res.data.id);
} else {
//Handle "put" of an existing record (UPDATE)
vm.obj.concurrency = res.data.concurrency;
window.$gz.form.setFormState({
vm: vm,
dirty: false
});
}
})
.catch(function handleSubmitError(error) {
vm.formState.loading = false;
window.$gz.errorHandler.handleFormError(error, vm);
});
}
} catch (error) {
vm.formState.loading = false;
window.$gz.errorHandler.handleFormError(error, vm);
}
}
},
remove() {
async remove() {
let vm = this;
window.$gz.dialog.confirmDelete().then(dialogResult => {
if (dialogResult == true) {
//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:
try {
let dialogResult = await window.$gz.dialog.confirmDelete();
if (dialogResult != true) {
return;
}
//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;
// navigate backwards
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() {
let vm = this;