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
*/
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)