AddAddress: add new address or update existing address: split fetch cascade and factorize reused functions

This commit is contained in:
Mathieu Jaumotte 2021-09-04 12:47:47 +02:00
parent d51752fa41
commit 2208518ca0

View File

@ -248,7 +248,6 @@ export default {
* Opening and closing Panes when interacting with buttons * Opening and closing Panes when interacting with buttons
*/ */
openShowPane() { openShowPane() {
console.log('open the Show Panel');
if (this.context.edit) { if (this.context.edit) {
this.getInitialAddress(this.context.addressId); this.getInitialAddress(this.context.addressId);
@ -263,17 +262,18 @@ export default {
} else { } else {
this.flag.showPane = true; this.flag.showPane = true;
console.log('step1: open the Show Panel');
} }
}, },
openEditPane() { openEditPane() {
console.log('open the Edit panel'); console.log('step2: open the Edit panel');
this.initForm(); this.initForm();
this.getCountries(); this.getCountries();
}, },
closeEditPane() { closeEditPane() {
console.log('close the Edit Panel'); console.log('step2: close the Edit Panel');
this.applyChanges(); this.applyChanges();
this.flag.showPane = true; this.flag.showPane = true;
this.flag.editPane = false; this.flag.editPane = false;
@ -413,7 +413,7 @@ export default {
newAddress: newAddress newAddress: newAddress
}); });
} else { } else {
this.addAddress(newAddress); this.addNewAddress(newAddress);
} }
}, },
@ -421,56 +421,52 @@ export default {
* Async POST transactions, * Async POST transactions,
* creating new address, and receive backend datas when promise is resolved * creating new address, and receive backend datas when promise is resolved
*/ */
addAddress(payload) addNewAddress(payload)
{ {
//console.log('addNewAddress', payload);
this.flag.loading = true; this.flag.loading = true;
if ('newPostcode' in payload) { if ('newPostcode' in payload) {
let postcodeBody = payload.newPostcode; let postcodeBody = payload.newPostcode;
if (this.context.entity.type === 'person') { if (this.context.entity.type === 'person') {
postcodeBody = Object.assign(postcodeBody, {'origin': 3}); postcodeBody = Object.assign(postcodeBody, {'origin': 3});
} }
postPostalCode(postcodeBody) postPostalCode(postcodeBody)
.then(postalCode => { .then(postalCode => {
let body = payload; payload.postcode = {'id': postalCode.id };
body.postcode = {'id': postalCode.id }, this.postNewAddress(payload);
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;
});
})
} else { } else {
postAddress(payload) this.postNewAddress(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;
});
} }
}, },
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, * Async PATCH transactions,
* then update existing address with backend datas when promise is resolved * then update existing address with backend datas when promise is resolved
*/ */
updateAddress(payload) updateAddress(payload)
{ {
// TODO change the condition because it writes new postal code in edit mode now: !writeNewPostalCode
this.flag.loading = true; this.flag.loading = true;
// TODO change the condition because it writes new postal code in edit mode now: !writeNewPostalCode
if ('newPostcode' in payload.newAddress) { if ('newPostcode' in payload.newAddress) {
let postcodeBody = payload.newAddress.newPostcode; let postcodeBody = payload.newAddress.newPostcode;
@ -478,36 +474,30 @@ export default {
postPostalCode(postcodeBody) postPostalCode(postcodeBody)
.then(postalCode => { .then(postalCode => {
let body = payload.newAddress; payload.newAddress.postcode = {'id': postalCode.id };
body.postcode = {'id': postalCode.id }, this.patchExistingAddress(payload.addressId, payload.newAddress);
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;
});
})
} else { } else {
patchAddress(payload.addressId, payload.newAddress) this.patchExistingAddress(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;
});
} }
}, },
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 * When submit address
* (get out step1 show pane, submit button) * (get out step1 show pane, submit button)