This commit is contained in:
2020-06-22 18:18:56 +00:00
parent d8ae4f43f8
commit 0158796796
4 changed files with 122 additions and 80 deletions

View File

@@ -758,6 +758,7 @@ export default {
}
if (newState.ready != null) {
console.log("gzform setting newstate ready to ", newState.ready);
newState.vm.formState.ready = newState.ready;
}
});

View File

@@ -842,7 +842,6 @@ function loadFormSettings(vm) {
}
}
//process TEMP form settings
if (formSettings.temp && formSettings.temp.page) {

View File

@@ -1,6 +1,8 @@
<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
@@ -233,21 +235,30 @@
const FORM_KEY = "user-edit";
const API_BASE_URL = "user/";
const FORM_CUSTOM_TEMPLATE_KEY = "User";
let InternalRouteChange = false;
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);
vm.rights = window.$gz.role.getRights(window.$gz.type.User);
vm.formState.readOnly = !vm.rights.change;
window.$gz.form.setFormState({
vm: vm,
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) {
console.trace("created, calling get data from api irc=",InternalRouteChange)
vm.getDataFromApi(vm.$route.params.recordid);
//is there already an obj from a create?
if (this.$route.params.obj) {
this.obj = this.$route.params.obj;
} else {
vm.getDataFromApi(vm.$route.params.recordid);
}
} else {
//setup for new record
//Update the form status
@@ -263,23 +274,20 @@ export default {
} catch (error) {
window.$gz.errorHandler.handleFormError(error, vm);
} finally {
vm.formState.ready = true;
window.$gz.form.setFormState({
vm: vm,
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)
console.trace("beforeRouteUpdate irc=",InternalRouteChange)
if (!InternalRouteChange && to.params.recordid != 0) {
console.trace("beforeRouteUpdate, calling get data from api irc=",InternalRouteChange)
this.getDataFromApi(to.params.recordid);
}
next();
// react to route changes...
// don't forget to call next()
//reset flag
InternalRouteChange = false;
},
// 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();
// },
async beforeRouteLeave(to, from, next) {
if (!this.formState.dirty || JUST_DELETED) {
next();
@@ -339,7 +347,6 @@ export default {
watch: {
formState: {
handler: function(val) {
//,oldval is available here too if necessary
if (this.formState.loading) {
return;
}
@@ -395,14 +402,17 @@ export default {
},
async getDataFromApi(recordId) {
let vm = this;
vm.formState.loading = true;
window.$gz.form.setFormState({
vm: vm,
loading: true
});
if (!recordId) {
throw FORM_KEY + "::getDataFromApi -> Missing recordID!";
}
let url = API_BASE_URL + recordId;
try {
window.$gz.form.deleteAllErrorBoxErrors(vm);
let res = await window.$gz.api.get(url);
if (res.error) {
@@ -441,55 +451,75 @@ export default {
},
async submit() {
let vm = this;
if (vm.canSave) {
vm.formState.loading = true;
if (vm.canSave == false) {
return;
}
try {
window.$gz.form.setFormState({
vm: vm,
loading: true
});
let url = API_BASE_URL;
//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);
//add in password and login if changed
let submitObject = vm.obj;
if (vm.password != null && vm.password != "") {
submitObject.password = vm.password;
}
if (vm.login != null && vm.login != "") {
submitObject.login = vm.login;
}
if (submitObject.roles == null) {
submitObject.roles = 0;
}
let res = await window.$gz.api.upsert(url, submitObject);
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
InternalRouteChange = true;
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 (error) {
vm.formState.loading = false;
window.$gz.errorHandler.handleFormError(error, vm);
//add in password and login if changed
let submitObject = vm.obj;
if (vm.password != null && vm.password != "") {
submitObject.password = vm.password;
}
if (vm.login != null && vm.login != "") {
submitObject.login = vm.login;
}
if (submitObject.roles == null) {
submitObject.roles = 0;
}
let res = await window.$gz.api.upsert(url, submitObject);
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 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 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
}
});
} else {
//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;
}
//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.ready = true;
vm.formState.loading = false;
// window.$gz.form.setFormState({
// vm: vm,
// loading: false,
// ready: true
// });
}
},
async remove() {
@@ -501,7 +531,10 @@ export default {
}
//do the delete
vm.formState.loading = true;
window.$gz.form.setFormState({
vm: vm,
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:
@@ -524,13 +557,14 @@ export default {
vm.$router.go(-1);
}
}
} catch (error) {
//Update the form status
} catch (ex) {
window.$gz.errorHandler.handleFormError(ex, vm);
} finally {
window.$gz.form.setFormState({
vm: vm,
loading: false
loading: false,
ready: true
});
window.$gz.errorHandler.handleFormError(error, vm);
}
},
async duplicate() {
@@ -538,7 +572,10 @@ export default {
if (!vm.canDuplicate || vm.$route.params.recordid == 0) {
return;
}
vm.formState.loading = true;
window.$gz.form.setFormState({
vm: vm,
loading: true
});
let url = API_BASE_URL + "duplicate/" + vm.$route.params.recordid;
try {
@@ -557,9 +594,14 @@ export default {
)
);
}
} catch (error) {
vm.formState.loading = false;
window.$gz.errorHandler.handleFormError(error, vm);
} catch (ex) {
window.$gz.errorHandler.handleFormError(ex, vm);
} finally {
window.$gz.form.setFormState({
vm: vm,
loading: false,
ready: true
});
}
}
}
@@ -583,7 +625,7 @@ async function clickHandler(menuItem) {
break;
case "new":
m.vm.$router.push({
name: "adm-usr",
name: "adm-user",
params: { recordid: 0, new: true }
});
break;

View File

@@ -25,7 +25,7 @@ export default {
data() {
return {
currentListViewId: 1,
dataListKey: "UserDataList",
dataListKey: "UserDataList",
dataListFilter: "",
dataListSort: "",
rights: window.$gz.role.defaultRightsObject()