From 0075fed9a51405f7914afde4c0d7c11a62d3ec88 Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Sun, 8 Aug 2021 09:47:01 +0200 Subject: [PATCH] submit methods: first promise is shared for person or household context --- .../vuejs/Address/components/AddAddress.vue | 100 ++++++++---------- 1 file changed, 46 insertions(+), 54 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Resources/public/vuejs/Address/components/AddAddress.vue b/src/Bundle/ChillMainBundle/Resources/public/vuejs/Address/components/AddAddress.vue index da3719f27..3c9ac64ff 100644 --- a/src/Bundle/ChillMainBundle/Resources/public/vuejs/Address/components/AddAddress.vue +++ b/src/Bundle/ChillMainBundle/Resources/public/vuejs/Address/components/AddAddress.vue @@ -358,7 +358,7 @@ export default { }, /* - * When changes are validated (step2 edit pane), + * When changes are validated (get out step2 edit pane), * apply some transformations before asyncing with backend * from entity.selected to entity.address */ @@ -494,7 +494,7 @@ export default { }, /* - * When submit address (step1 show pane) + * When submit address (get out step1 show pane) */ submitAddress() { @@ -508,30 +508,59 @@ export default { }, backUrl: this.context.backUrl } + console.log('@@@ CLICK button submitAddress for', this.context.entity.type, 'with payload', payload); + this.addDateToAddressAndAddressTo(payload, this.context.entity.type); - if (this.context.entity.type === 'person') { - this.addDateToAddressAndAddressToPerson(); - - } else if (this.context.entity.type === 'household') { - this.addDateToAddressAndAddressToHousehold(); - - } else { - this.errorMsg.push('this entity type is not yet allowed'); - } this.initForm(); this.flag.showPane = false; }, - addDateToAddressAndAddressToPerson(payload) + addDateToAddressAndAddressTo(payload, postTo) { + console.log('addDateToAddressAndAddressTo', postTo) this.flag.loading = true; patchAddress(payload.addressId, payload.body) .then(address => new Promise((resolve, reject) => { - this.valid.from = address.validFrom; - resolve(); + this.valid.from = address.validFrom; + resolve(); + }) + .then( + this.postAddressTo(payload, postTo) + ) + ) + .catch((error) => { + this.errorMsg.push(error); + this.flag.loading = false; + }); + }, - }).then( + postAddressTo(payload, postTo) + { + console.log('postAddressTo', postTo); + switch (postTo) { + case 'household': + postAddressToHousehold(payload.entityId, payload.addressId) + .then(household => new Promise((resolve, reject) => { + + console.log('TODO commit addAddressToHousehold', household); + + this.flag.loading = false; + this.flag.success = true; + if (this.options.redirectToBackUrl) { + window.location.assign(payload.backUrl); + } + resolve(); + })) + .catch((error) => { + this.errorMsg.push(error); + this.flag.loading = false; + }) + ; + break; + + case 'person': + default: postAddressToPerson(payload.entityId, payload.addressId) .then(person => new Promise((resolve, reject) => { @@ -549,45 +578,8 @@ export default { this.errorMsg.push(error); this.flag.loading = false; }) - - )) - .catch((error) => { - this.errorMsg.push(error); - this.flag.loading = false; - }); - }, - - addDateToAddressAndAddressToHousehold(payload) - { - this.flag.loading = true; - patchAddress(payload.addressId, payload.body) - .then(address => new Promise((resolve, reject) => { - this.valid.from = address.validFrom; - resolve(); - - }).then( - postAddressToHousehold(payload.entityId, payload.addressId) - .then(household => new Promise((resolve, reject) => { - - console.log('TODO commit addAddressToHousehold', household); - - this.flag.loading = false; - this.flag.success = true; - if (this.options.redirectToBackUrl) { - window.location.assign(payload.backUrl); - } - resolve(); - })) - .catch((error) => { - this.errorMsg.push(error); - this.flag.loading = false; - }) - - )) - .catch((error) => { - this.errorMsg.push(error); - this.flag.loading = false; - }); + ; + } }, }