From 2208518ca07d12ad7bcbd78b98bf3f5ddfdd4f42 Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Sat, 4 Sep 2021 12:47:47 +0200 Subject: [PATCH] AddAddress: add new address or update existing address: split fetch cascade and factorize reused functions --- .../vuejs/Address/components/AddAddress.vue | 106 ++++++++---------- 1 file changed, 48 insertions(+), 58 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 fa4ff4773..5eeda6ac7 100644 --- a/src/Bundle/ChillMainBundle/Resources/public/vuejs/Address/components/AddAddress.vue +++ b/src/Bundle/ChillMainBundle/Resources/public/vuejs/Address/components/AddAddress.vue @@ -248,7 +248,6 @@ export default { * Opening and closing Panes when interacting with buttons */ openShowPane() { - console.log('open the Show Panel'); if (this.context.edit) { this.getInitialAddress(this.context.addressId); @@ -263,17 +262,18 @@ export default { } else { this.flag.showPane = true; + console.log('step1: open the Show Panel'); } }, openEditPane() { - console.log('open the Edit panel'); + console.log('step2: open the Edit panel'); this.initForm(); this.getCountries(); }, closeEditPane() { - console.log('close the Edit Panel'); + console.log('step2: close the Edit Panel'); this.applyChanges(); this.flag.showPane = true; this.flag.editPane = false; @@ -413,7 +413,7 @@ export default { newAddress: newAddress }); } else { - this.addAddress(newAddress); + this.addNewAddress(newAddress); } }, @@ -421,56 +421,52 @@ export default { * Async POST transactions, * creating new address, and receive backend datas when promise is resolved */ - addAddress(payload) + addNewAddress(payload) { + //console.log('addNewAddress', payload); this.flag.loading = true; + if ('newPostcode' in payload) { let postcodeBody = payload.newPostcode; if (this.context.entity.type === 'person') { postcodeBody = Object.assign(postcodeBody, {'origin': 3}); } - postPostalCode(postcodeBody) - .then(postalCode => { - let body = payload; - body.postcode = {'id': postalCode.id }, - postAddress(body) - .then(address => new Promise((resolve, reject) => { - this.entity.address = address; - this.flag.loading = false; - this.flag.success = true; - resolve(); - })) - .catch((error) => { - this.errorMsg.push(error); - this.flag.loading = false; - }); - }) + .then(postalCode => { + payload.postcode = {'id': postalCode.id }; + this.postNewAddress(payload); + }); } else { - postAddress(payload) - .then(address => new Promise((resolve, reject) => { - this.entity.address = address; - this.flag.loading = false; - this.flag.success = true; - resolve(); - })) - .catch((error) => { - this.errorMsg.push(error); - this.flag.loading = false; - }); + this.postNewAddress(payload); } }, + postNewAddress(payload) { + //console.log('postNewAddress', payload); + postAddress(payload) + .then(address => new Promise((resolve, reject) => { + this.entity.address = address; + this.flag.loading = false; + this.flag.success = true; + resolve(); + })) + .catch((error) => { + this.errorMsg.push(error); + this.flag.loading = false; + }); + }, + /* * Async PATCH transactions, * then update existing address with backend datas when promise is resolved */ updateAddress(payload) { - // TODO change the condition because it writes new postal code in edit mode now: !writeNewPostalCode this.flag.loading = true; + + // TODO change the condition because it writes new postal code in edit mode now: !writeNewPostalCode if ('newPostcode' in payload.newAddress) { let postcodeBody = payload.newAddress.newPostcode; @@ -478,36 +474,30 @@ export default { postPostalCode(postcodeBody) .then(postalCode => { - let body = payload.newAddress; - body.postcode = {'id': postalCode.id }, - patchAddress(payload.addressId, body) - .then(address => new Promise((resolve, reject) => { - this.entity.address = address; - this.flag.loading = false; - this.flag.success = true; - resolve(); - })) - .catch((error) => { - this.errorMsg.push(error); - this.flag.loading = false; - }); - }) + payload.newAddress.postcode = {'id': postalCode.id }; + this.patchExistingAddress(payload.addressId, payload.newAddress); + }); } else { - patchAddress(payload.addressId, payload.newAddress) - .then(address => new Promise((resolve, reject) => { - this.entity.address = address; - this.flag.loading = false; - this.flag.success = true; - resolve(); - })) - .catch((error) => { - this.errorMsg.push(error); - this.flag.loading = false; - }); + this.patchExistingAddress(payload.addressId, payload.newAddress); } }, + patchExistingAddress(addressId, newAddress) { + console.log('patchExistingAddress', addressId, newAddress); + patchAddress(addressId, newAddress) + .then(address => new Promise((resolve, reject) => { + this.entity.address = address; + this.flag.loading = false; + this.flag.success = true; + resolve(); + })) + .catch((error) => { + this.errorMsg.push(error); + this.flag.loading = false; + }); + }, + /* * When submit address * (get out step1 show pane, submit button)