From 35d35c9f40c7e5ebd97b09edeb995265b2778f7f Mon Sep 17 00:00:00 2001 From: nobohan Date: Tue, 15 Jun 2021 21:03:50 +0200 Subject: [PATCH] addresses: edit address (WIP) --- .../Resources/public/vuejs/Address/App.vue | 44 +++++++++++---- .../public/vuejs/Address/store/index.js | 54 ++++++++++++++++++- .../Resources/public/vuejs/_api/AddAddress.js | 18 ++++++- .../public/vuejs/_components/AddAddress.vue | 12 +++-- .../Normalizer/AddressNormalizer.php | 11 +++- .../Resources/views/Address/edit.html.twig | 36 ++++++------- .../Resources/views/Address/new.html.twig | 1 + 7 files changed, 137 insertions(+), 39 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Resources/public/vuejs/Address/App.vue b/src/Bundle/ChillMainBundle/Resources/public/vuejs/Address/App.vue index 1ffae758f..37dd6aba4 100644 --- a/src/Bundle/ChillMainBundle/Resources/public/vuejs/Address/App.vue +++ b/src/Bundle/ChillMainBundle/Resources/public/vuejs/Address/App.vue @@ -2,7 +2,8 @@
-

{{ $t('create_a_new_address') }}

+

{{ $t('create_a_new_address') }}

+

{{ $t('edit_a_new_address') }}

@@ -14,9 +15,12 @@
{{ address.postcode.name }}
+
+ {{ address.country.name }} +
-
+

{{ $t('date') }}

{{ $t('back_to_the_list') }} -
  • +
  • @@ -53,7 +57,9 @@ export default { }, data() { return { + edit: window.mode === 'edit', personId: window.personId, + addressId: window.addressId, backUrl: `/fr/person/${window.personId}/address/list`, //TODO better way to pass this validFrom: new Date().toISOString().split('T')[0] } @@ -90,7 +96,7 @@ export default { }); } - if(address.writeNewPostalCode){ + if (address.writeNewPostalCode){ let newPostalCode = address.newPostalCode; newPostalCode = Object.assign(newPostalCode, { 'country': {'id': address.selected.country.id }, @@ -100,16 +106,32 @@ export default { }); } - this.$store.dispatch('addAddress', newAddress); + if (this.edit){ + this.$store.dispatch('updateAddress', { + addressId: this.addressId, + newAddress: newAddress + }); + } else { + this.$store.dispatch('addAddress', newAddress); + } + modal.showModal = false; }, addToPerson() { - this.$store.dispatch('addDateToAddressAndAddressToPerson', { - personId: this.personId, - addressId: this.$store.state.address.address_id, - body: { validFrom: {datetime: `${this.validFrom}T00:00:00+0100`}} - }) + this.$store.dispatch('addDateToAddressAndAddressToPerson', { + personId: this.personId, + addressId: this.$store.state.address.address_id, + body: { validFrom: {datetime: `${this.validFrom}T00:00:00+0100`}} + }) + }, + getEditAddress() { + this.$store.dispatch('getEditAddress', this.addressId); } - } + }, + mounted() { + if (this.edit) { + this.getEditAddress(); + } + }, }; diff --git a/src/Bundle/ChillMainBundle/Resources/public/vuejs/Address/store/index.js b/src/Bundle/ChillMainBundle/Resources/public/vuejs/Address/store/index.js index e186895c7..2989baaaa 100644 --- a/src/Bundle/ChillMainBundle/Resources/public/vuejs/Address/store/index.js +++ b/src/Bundle/ChillMainBundle/Resources/public/vuejs/Address/store/index.js @@ -1,7 +1,7 @@ import 'es6-promise/auto'; import { createStore } from 'vuex'; -import { patchAddress, postAddress, postPostalCode, postAddressToPerson } from '../../_api/AddAddress' +import { patchAddress, postAddress, postPostalCode, postAddressToPerson, getAddress } from '../../_api/AddAddress' const debug = process.env.NODE_ENV !== 'production'; @@ -9,6 +9,8 @@ const store = createStore({ strict: debug, state: { address: {}, + editAddress: {}, //TODO or should be address? + person: {}, errorMsg: [] }, getters: { @@ -21,6 +23,10 @@ const store = createStore({ console.log('@M addAddress address', address); state.address = address; }, + updateAddress(state, address) { + console.log('@M updateAddress address', address); + state.address = address; + }, addAddressToPerson(state, person) { console.log('@M addAddressToPerson person', person); state.person = person; @@ -28,7 +34,11 @@ const store = createStore({ addDateToAddress(state, validFrom) { console.log('@M addDateToAddress address.validFrom', validFrom); state.validFrom = validFrom; - } + }, + getEditAddress(state, address) { + console.log('@M getEditAddress address', address); + state.editAddress = address; + }, }, actions: { addAddress({ commit }, payload) { @@ -81,6 +91,46 @@ const store = createStore({ commit('catchError', error); }); }, + updateAddress({ commit }, payload) { + console.log('@A updateAddress payload', payload); + + if('newPostalCode' in payload.newAddress){ + postPostalCode(payload.newAddress.newPostalCode) + .then(postalCode => { + let body = payload; + body.postcode = {'id': postalCode.id }, + patchAddress(body) + .then(address => new Promise((resolve, reject) => { + commit('updateAddress', address); + resolve(); + })) + .catch((error) => { + commit('catchError', error); + }); + }) + + } else { + patchAddress(payload.addressId, payload.newAddress) + .then(address => new Promise((resolve, reject) => { + commit('updateAddress', address); + resolve(); + })) + .catch((error) => { + commit('catchError', error); + }); + } + }, + getEditAddress({ commit }, payload) { + console.log('@A getEditAddress payload', payload); + + getAddress(payload).then(address => new Promise((resolve, reject) => { + commit('getEditAddress', address); + resolve(); + })) + .catch((error) => { + commit('catchError', error); + }); + }, } }); diff --git a/src/Bundle/ChillMainBundle/Resources/public/vuejs/_api/AddAddress.js b/src/Bundle/ChillMainBundle/Resources/public/vuejs/_api/AddAddress.js index b41ebf844..0f078a2d9 100644 --- a/src/Bundle/ChillMainBundle/Resources/public/vuejs/_api/AddAddress.js +++ b/src/Bundle/ChillMainBundle/Resources/public/vuejs/_api/AddAddress.js @@ -150,6 +150,21 @@ const postAddressToPerson = (personId, addressId) => { }); }; +/* +* Endpoint chill_api_single_address__index +* method GET, get Address Object +* @params {id} the address id +* @returns {Promise} a promise containing a Address object +*/ +const getAddress = (id) => { + console.log('<<< get address'); + const url = `/api/1.0/main/address/${id}.json`; + return fetch(url) + .then(response => { + if (response.ok) { return response.json(); } + throw Error('Error with request resource response'); + }); +}; export { fetchCountries, @@ -159,5 +174,6 @@ export { postAddress, patchAddress, postPostalCode, - postAddressToPerson + postAddressToPerson, + getAddress }; diff --git a/src/Bundle/ChillMainBundle/Resources/public/vuejs/_components/AddAddress.vue b/src/Bundle/ChillMainBundle/Resources/public/vuejs/_components/AddAddress.vue index 42c9fd279..217a21063 100644 --- a/src/Bundle/ChillMainBundle/Resources/public/vuejs/_components/AddAddress.vue +++ b/src/Bundle/ChillMainBundle/Resources/public/vuejs/_components/AddAddress.vue @@ -1,7 +1,10 @@