This commit is contained in:
@@ -39,7 +39,7 @@ function stringifyPrimitive(v) {
|
||||
// Try to handle an api error
|
||||
// return true if handled or false if not
|
||||
//
|
||||
function handleError(action, error, route, reject) {
|
||||
function handleError(action, error, route) {
|
||||
let errorMessage =
|
||||
"API error: " + action + " route =" + route + ", message =" + error.message;
|
||||
window.$gz.store.commit("logItem", errorMessage);
|
||||
@@ -56,11 +56,8 @@ function handleError(action, error, route, reject) {
|
||||
window.$gz.translation.get("ErrorUserNotAuthorized")
|
||||
);
|
||||
router.push(window.$gz.store.state.homePage);
|
||||
if (reject) {
|
||||
return reject("[ErrorUserNotAuthorized]");
|
||||
} else {
|
||||
throw "[ErrorUserNotAuthorized]";
|
||||
}
|
||||
|
||||
throw "[ErrorUserNotAuthorized]";
|
||||
}
|
||||
|
||||
//Handle 401 not authenticated
|
||||
@@ -75,11 +72,8 @@ function handleError(action, error, route, reject) {
|
||||
);
|
||||
|
||||
router.push("/login");
|
||||
if (reject) {
|
||||
return reject("[ErrorUserNotAuthenticated]");
|
||||
} else {
|
||||
throw "[ErrorUserNotAuthenticated]";
|
||||
}
|
||||
|
||||
throw "[ErrorUserNotAuthenticated]";
|
||||
}
|
||||
|
||||
//is it a network error?
|
||||
@@ -104,12 +98,7 @@ function handleError(action, error, route, reject) {
|
||||
window.$gz.eventBus.$emit("notify-error", msg);
|
||||
//note: using translation key in square brackets
|
||||
|
||||
if (reject) {
|
||||
return reject(msg);
|
||||
} else {
|
||||
throw msg;
|
||||
}
|
||||
//throw "Error: unable to contact server";
|
||||
throw msg;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,13 +111,11 @@ export default {
|
||||
status(response) {
|
||||
//Handle expected api errors
|
||||
if (response.status == 401) {
|
||||
//must reject if not Authenticated
|
||||
return Promise.reject(new Error("[ErrorUserNotAuthenticated]"));
|
||||
throw new Error("[ErrorUserNotAuthenticated]");
|
||||
}
|
||||
|
||||
if (response.status == 403) {
|
||||
//must reject if not Authorized
|
||||
return Promise.reject(new Error("[ErrorUserNotAuthorized]"));
|
||||
throw new Error("[ErrorUserNotAuthorized]");
|
||||
}
|
||||
|
||||
//404 not found is an expected status not worth logging allow to bubble up
|
||||
@@ -140,9 +127,7 @@ export default {
|
||||
if (response.status == 405) {
|
||||
//Probably a development error
|
||||
|
||||
return Promise.reject(
|
||||
new Error("Method Not Allowed (route issue?) " + response.url)
|
||||
);
|
||||
throw new Error("Method Not Allowed (route issue?) " + response.url);
|
||||
}
|
||||
|
||||
if (response.status >= 200 && response.status < 300) {
|
||||
@@ -165,12 +150,10 @@ export default {
|
||||
statusEx(response) {
|
||||
//Handle expected api errors
|
||||
if (response.status == 401) {
|
||||
//must reject if not Authenticated
|
||||
throw new Error("[ErrorUserNotAuthenticated]");
|
||||
}
|
||||
|
||||
if (response.status == 403) {
|
||||
//must reject if not Authorized
|
||||
throw new Error("[ErrorUserNotAuthorized]");
|
||||
}
|
||||
|
||||
@@ -432,7 +415,7 @@ export default {
|
||||
return r;
|
||||
} catch (error) {
|
||||
//fundamental error, can't proceed with this call
|
||||
handleError("GET", error, route, reject);
|
||||
handleError("GET", error, route);
|
||||
}
|
||||
},
|
||||
|
||||
@@ -534,7 +517,7 @@ export default {
|
||||
r = await that.extractBodyEx(r);
|
||||
return r;
|
||||
} catch (error) {
|
||||
handleError("POSTATTACHMENT", error, route, reject);
|
||||
handleError("POSTATTACHMENT", error, route);
|
||||
}
|
||||
}
|
||||
//---------------
|
||||
|
||||
@@ -139,7 +139,6 @@ export default {
|
||||
//
|
||||
required(vm, ref) {
|
||||
if (vm.formState.loading) {
|
||||
// console.log("gzform:required rule - bailing due to loading", ref);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -736,8 +735,7 @@ export default {
|
||||
setFormState(newState) {
|
||||
//this returns a promise so any function that needs to wait for this can utilize that
|
||||
// if (newState.valid != null && newState.valid == false) {
|
||||
// console.trace();
|
||||
// console.log(newState);
|
||||
// console.trace(newState);
|
||||
// // debugger;
|
||||
// }
|
||||
return Vue.nextTick(function() {
|
||||
@@ -758,7 +756,6 @@ export default {
|
||||
}
|
||||
|
||||
if (newState.ready != null) {
|
||||
console.log("gzform setting newstate ready to ", newState.ready);
|
||||
newState.vm.formState.ready = newState.ready;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
<template>
|
||||
<v-container fluid>
|
||||
<gz-report-selector ref="reportSelector"></gz-report-selector>
|
||||
{{ formState }}
|
||||
{{ obj }}
|
||||
<h2 class="red--text">
|
||||
todo: revisit after add customer, ho, vendor to support usertypes
|
||||
customer,headoffice,contractor
|
||||
@@ -239,7 +237,6 @@ const FORM_CUSTOM_TEMPLATE_KEY = "User";
|
||||
export default {
|
||||
async created() {
|
||||
//created is called when the route is updated to show a new record even though we don't need to re-init again
|
||||
|
||||
let vm = this;
|
||||
try {
|
||||
await initForm(vm);
|
||||
@@ -254,16 +251,18 @@ export default {
|
||||
if (this.$route.params.obj) {
|
||||
//yes, no need to fetch it
|
||||
this.obj = this.$route.params.obj;
|
||||
vm.formState.loading = false; //here we handle it immediately
|
||||
} else {
|
||||
vm.getDataFromApi(vm.$route.params.recordid);
|
||||
vm.getDataFromApi(vm.$route.params.recordid); //let getdata handle loading
|
||||
}
|
||||
} else {
|
||||
vm.formState.loading = false; //here we handle it immediately
|
||||
}
|
||||
//set initial form status
|
||||
window.$gz.form.setFormState({
|
||||
vm: vm,
|
||||
dirty: false,
|
||||
valid: true,
|
||||
loading: false
|
||||
valid: true
|
||||
});
|
||||
|
||||
generateMenu(vm);
|
||||
@@ -387,10 +386,7 @@ export default {
|
||||
},
|
||||
async getDataFromApi(recordId) {
|
||||
let vm = this;
|
||||
window.$gz.form.setFormState({
|
||||
vm: vm,
|
||||
loading: true
|
||||
});
|
||||
vm.formState.loading = true;
|
||||
|
||||
if (!recordId) {
|
||||
throw FORM_KEY + "::getDataFromApi -> Missing recordID!";
|
||||
@@ -414,6 +410,9 @@ export default {
|
||||
window.$gz.form.setErrorBoxErrors(vm);
|
||||
} else {
|
||||
vm.obj = res.data;
|
||||
//modify the menu as necessary
|
||||
generateMenu(vm);
|
||||
|
||||
//Update the form status
|
||||
window.$gz.form.setFormState({
|
||||
vm: vm,
|
||||
@@ -421,17 +420,11 @@ export default {
|
||||
valid: true,
|
||||
loading: false
|
||||
});
|
||||
|
||||
//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);
|
||||
} finally {
|
||||
vm.formState.loading = false;
|
||||
}
|
||||
},
|
||||
async submit() {
|
||||
@@ -441,10 +434,7 @@ export default {
|
||||
}
|
||||
|
||||
try {
|
||||
window.$gz.form.setFormState({
|
||||
vm: vm,
|
||||
loading: true
|
||||
});
|
||||
vm.formState.loading = true;
|
||||
let url = API_BASE_URL;
|
||||
//clear any errors vm might be around from previous submit
|
||||
window.$gz.form.deleteAllErrorBoxErrors(vm);
|
||||
@@ -469,16 +459,14 @@ export default {
|
||||
if (res.data.id) {
|
||||
//POST - whole new object returned
|
||||
vm.obj = res.data;
|
||||
//change url to new record in history
|
||||
//Change URL to new record
|
||||
//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 so need a flag to prevent redundant fetch of record
|
||||
|
||||
// vm.$router.replace(vm.$route.fullPath.slice(0, -1) + res.data.id);
|
||||
this.$router.push({
|
||||
name: "adm-user",
|
||||
params: {
|
||||
recordid: res.data.id,
|
||||
obj: res.data // Hidden data/state
|
||||
obj: res.data //Pass data object to new form
|
||||
}
|
||||
});
|
||||
} else {
|
||||
@@ -497,14 +485,7 @@ export default {
|
||||
} catch (ex) {
|
||||
window.$gz.errorHandler.handleFormError(ex, vm);
|
||||
} finally {
|
||||
vm.formState.ready = true;
|
||||
vm.formState.loading = false;
|
||||
|
||||
// window.$gz.form.setFormState({
|
||||
// vm: vm,
|
||||
// loading: false,
|
||||
// ready: true
|
||||
// });
|
||||
}
|
||||
},
|
||||
async remove() {
|
||||
@@ -516,10 +497,7 @@ export default {
|
||||
}
|
||||
|
||||
//do the delete
|
||||
window.$gz.form.setFormState({
|
||||
vm: vm,
|
||||
loading: true
|
||||
});
|
||||
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:
|
||||
@@ -545,11 +523,7 @@ export default {
|
||||
} catch (ex) {
|
||||
window.$gz.errorHandler.handleFormError(ex, vm);
|
||||
} finally {
|
||||
window.$gz.form.setFormState({
|
||||
vm: vm,
|
||||
loading: false,
|
||||
ready: true
|
||||
});
|
||||
vm.formState.loading = false;
|
||||
}
|
||||
},
|
||||
async duplicate() {
|
||||
@@ -557,36 +531,29 @@ export default {
|
||||
if (!vm.canDuplicate || vm.$route.params.recordid == 0) {
|
||||
return;
|
||||
}
|
||||
window.$gz.form.setFormState({
|
||||
vm: vm,
|
||||
loading: true
|
||||
});
|
||||
vm.formState.loading = true;
|
||||
let url = API_BASE_URL + "duplicate/" + vm.$route.params.recordid;
|
||||
|
||||
try {
|
||||
window.$gz.form.deleteAllErrorBoxErrors(vm);
|
||||
let res = await window.$gz.api.upsert(url);
|
||||
vm.formState.loading = false;
|
||||
if (res.error) {
|
||||
vm.formState.serverError = res.error;
|
||||
window.$gz.form.setErrorBoxErrors(vm);
|
||||
} else {
|
||||
//Navigate to new record
|
||||
vm.$router.push(
|
||||
window.$gz.api.replaceAfterLastSlash(
|
||||
vm.$route.fullPath,
|
||||
res.data.id
|
||||
)
|
||||
);
|
||||
this.$router.push({
|
||||
name: "adm-user",
|
||||
params: {
|
||||
recordid: res.data.id,
|
||||
obj: res.data // pass data object to new form
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (ex) {
|
||||
window.$gz.errorHandler.handleFormError(ex, vm);
|
||||
} finally {
|
||||
window.$gz.form.setFormState({
|
||||
vm: vm,
|
||||
loading: false,
|
||||
ready: true
|
||||
});
|
||||
vm.formState.loading = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -718,7 +685,7 @@ function generateMenu(vm) {
|
||||
});
|
||||
}
|
||||
|
||||
if (vm.rights.change) {
|
||||
if (vm.rights.change && vm.$route.params.recordid != 0) {
|
||||
menuOptions.menuItems.push({
|
||||
title: vm.$ay.t("Duplicate"),
|
||||
icon: "fa-clone",
|
||||
|
||||
@@ -273,37 +273,33 @@ export default {
|
||||
vm.rights = window.$gz.role.getRights(window.$gz.type.Widget);
|
||||
vm.formState.readOnly = !vm.rights.change;
|
||||
window.$gz.eventBus.$on("menu-click", clickHandler);
|
||||
|
||||
//id 0 means create a new record don't load one
|
||||
if (vm.$route.params.recordid != 0) {
|
||||
vm.getDataFromApi(vm.$route.params.recordid);
|
||||
//is there already an obj from a prior operation?
|
||||
if (this.$route.params.obj) {
|
||||
//yes, no need to fetch it
|
||||
this.obj = this.$route.params.obj;
|
||||
vm.formState.loading = false; //here we handle it immediately
|
||||
} else {
|
||||
vm.getDataFromApi(vm.$route.params.recordid); //let getdata handle loading
|
||||
}
|
||||
} else {
|
||||
//setup for new record
|
||||
//Update the form status
|
||||
window.$gz.form.setFormState({
|
||||
vm: vm,
|
||||
dirty: false,
|
||||
valid: true,
|
||||
loading: false
|
||||
});
|
||||
|
||||
generateMenu(vm);
|
||||
vm.formState.loading = false; //here we handle it immediately
|
||||
}
|
||||
window.$gz.form.setFormState({
|
||||
vm: vm,
|
||||
dirty: false,
|
||||
valid: true
|
||||
});
|
||||
|
||||
generateMenu(vm);
|
||||
} catch (error) {
|
||||
window.$gz.errorHandler.handleFormError(error, vm);
|
||||
} finally {
|
||||
vm.formState.ready = true;
|
||||
}
|
||||
},
|
||||
beforeRouteUpdate(to, from, next) {
|
||||
//This triggers a fetch of the data when the ID value changes on the route
|
||||
//which happens on duplicate, submit new record and change of active record id (backward nav/forward nav etc)
|
||||
if (to.params.recordid != 0) {
|
||||
this.getDataFromApi(to.params.recordid);
|
||||
}
|
||||
next();
|
||||
// react to route changes...
|
||||
// don't forget to call next()
|
||||
},
|
||||
async beforeRouteLeave(to, from, next) {
|
||||
if (!this.formState.dirty || JUST_DELETED) {
|
||||
next();
|
||||
@@ -442,6 +438,8 @@ export default {
|
||||
window.$gz.form.setErrorBoxErrors(vm);
|
||||
} else {
|
||||
vm.obj = res.data;
|
||||
//modify the menu as necessary
|
||||
generateMenu(vm);
|
||||
//Update the form status
|
||||
window.$gz.form.setFormState({
|
||||
vm: vm,
|
||||
@@ -449,55 +447,61 @@ export default {
|
||||
valid: true,
|
||||
loading: false
|
||||
});
|
||||
|
||||
//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);
|
||||
} finally {
|
||||
vm.formState.loading = false;
|
||||
}
|
||||
},
|
||||
async submit() {
|
||||
let vm = this;
|
||||
if (vm.canSave) {
|
||||
if (vm.canSave == false) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
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);
|
||||
|
||||
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);
|
||||
let res = await window.$gz.api.upsert(url, vm.obj);
|
||||
|
||||
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) {
|
||||
//POST - whole new object returned
|
||||
vm.obj = res.data;
|
||||
//Change URL to new record
|
||||
//NOTE: will not cause a page re-render, almost nothing does unless forced with a KEY property or using router.GO()
|
||||
|
||||
this.$router.push({
|
||||
name: "widget-edit",
|
||||
params: {
|
||||
recordid: res.data.id,
|
||||
obj: res.data // Pass data object to new form
|
||||
}
|
||||
});
|
||||
} 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
|
||||
});
|
||||
}
|
||||
//PUT - only concurrency token is returned (**warning, if server changes object other fields then this needs to act more like POST above but is more efficient this way**)
|
||||
//Handle "put" of an existing record (UPDATE)
|
||||
vm.obj.concurrency = res.data.concurrency;
|
||||
}
|
||||
} catch (error) {
|
||||
vm.formState.loading = false;
|
||||
window.$gz.errorHandler.handleFormError(error, vm);
|
||||
//Update the form status
|
||||
window.$gz.form.setFormState({
|
||||
vm: vm,
|
||||
dirty: false,
|
||||
valid: true
|
||||
});
|
||||
}
|
||||
} catch (ex) {
|
||||
window.$gz.errorHandler.handleFormError(ex, vm);
|
||||
} finally {
|
||||
vm.formState.loading = false;
|
||||
}
|
||||
},
|
||||
async remove() {
|
||||
@@ -552,22 +556,23 @@ export default {
|
||||
try {
|
||||
window.$gz.form.deleteAllErrorBoxErrors(vm);
|
||||
let res = await window.$gz.api.upsert(url);
|
||||
vm.formState.loading = false;
|
||||
if (res.error) {
|
||||
vm.formState.serverError = res.error;
|
||||
window.$gz.form.setErrorBoxErrors(vm);
|
||||
} else {
|
||||
//Navigate to new record
|
||||
vm.$router.push(
|
||||
window.$gz.api.replaceAfterLastSlash(
|
||||
vm.$route.fullPath,
|
||||
res.data.id
|
||||
)
|
||||
);
|
||||
this.$router.push({
|
||||
name: "widget-edit",
|
||||
params: {
|
||||
recordid: res.data.id,
|
||||
obj: res.data // Pass data object to new form
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
} catch (ex) {
|
||||
window.$gz.errorHandler.handleFormError(ex, vm);
|
||||
} finally {
|
||||
vm.formState.loading = false;
|
||||
window.$gz.errorHandler.handleFormError(error, vm);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user