From 1b709d39a4916cf090c57afbf97ee218310f43b8 Mon Sep 17 00:00:00 2001 From: nobohan Date: Fri, 11 Jun 2021 14:33:22 +0200 Subject: [PATCH] addresses: enable POSTing Postal Code entities (back + front) --- .../ChillMainExtension.php | 3 +- .../ChillMainBundle/Entity/PostalCode.php | 8 +- .../Resources/public/vuejs/Address/App.vue | 14 +++- .../Resources/public/vuejs/Address/js/i18n.js | 5 +- .../public/vuejs/Address/store/index.js | 37 ++++++--- .../Resources/public/vuejs/_api/AddAddress.js | 24 +++++- .../public/vuejs/_components/AddAddress.vue | 7 ++ .../AddAddress/AddressSelection.vue | 5 +- .../_components/AddAddress/CitySelection.vue | 46 +++++++++- .../AddAddress/CountrySelection.vue | 2 +- .../ChillMainBundle/chill.api.specs.yaml | 83 +++++++++++++++++++ .../public/vuejs/HouseholdAddress/App.vue | 14 +++- .../vuejs/HouseholdAddress/store/index.js | 38 ++++++--- 13 files changed, 247 insertions(+), 39 deletions(-) diff --git a/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php b/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php index 2bb0a32f7..d5c24b110 100644 --- a/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php +++ b/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php @@ -321,7 +321,8 @@ class ChillMainExtension extends Extension implements PrependExtensionInterface, '_entity' => [ 'methods' => [ Request::METHOD_GET => true, - Request::METHOD_HEAD => true + Request::METHOD_HEAD => true, + Request::METHOD_POST => true, ] ], ] diff --git a/src/Bundle/ChillMainBundle/Entity/PostalCode.php b/src/Bundle/ChillMainBundle/Entity/PostalCode.php index a70a955b8..26d75a532 100644 --- a/src/Bundle/ChillMainBundle/Entity/PostalCode.php +++ b/src/Bundle/ChillMainBundle/Entity/PostalCode.php @@ -26,7 +26,7 @@ class PostalCode * @ORM\Id * @ORM\Column(name="id", type="integer") * @ORM\GeneratedValue(strategy="AUTO") - * @groups({"read"}) + * @groups({"write", "read"}) */ private $id; @@ -34,7 +34,7 @@ class PostalCode * @var string * * @ORM\Column(type="string", length=255, name="label") - * @groups({"read"}) + * @groups({"write", "read"}) */ private $name; @@ -42,7 +42,7 @@ class PostalCode * @var string * * @ORM\Column(type="string", length=100) - * @groups({"read"}) + * @groups({"write", "read"}) */ private $code; @@ -50,7 +50,7 @@ class PostalCode * @var Country * * @ORM\ManyToOne(targetEntity="Chill\MainBundle\Entity\Country") - * @groups({"read"}) + * @groups({"write", "read"}) */ private $country; diff --git a/src/Bundle/ChillMainBundle/Resources/public/vuejs/Address/App.vue b/src/Bundle/ChillMainBundle/Resources/public/vuejs/Address/App.vue index f412bb209..edd089950 100644 --- a/src/Bundle/ChillMainBundle/Resources/public/vuejs/Address/App.vue +++ b/src/Bundle/ChillMainBundle/Resources/public/vuejs/Address/App.vue @@ -49,7 +49,19 @@ export default { }; if (address.selected.address.point !== undefined){ - newAddress = Object.assign(newAddress, {'point': address.selected.address.point.coordinates}); + createdAddress = Object.assign(createdAddress, { + 'point': address.selected.address.point.coordinates + }); + } + + if(address.writeNewPostalCode){ + let newPostalCode = address.newPostalCode; + newPostalCode = Object.assign(newPostalCode, { + 'country': {'id': address.selected.country.id }, + }); + createdAddress = Object.assign(createdAddress, { + 'newPostalCode': newPostalCode + }); } this.$store.dispatch('addAddress', newAddress); diff --git a/src/Bundle/ChillMainBundle/Resources/public/vuejs/Address/js/i18n.js b/src/Bundle/ChillMainBundle/Resources/public/vuejs/Address/js/i18n.js index 92556398a..cc042f7bf 100644 --- a/src/Bundle/ChillMainBundle/Resources/public/vuejs/Address/js/i18n.js +++ b/src/Bundle/ChillMainBundle/Resources/public/vuejs/Address/js/i18n.js @@ -16,7 +16,10 @@ const addressMessages = { flat: 'Appartement', buildingName: 'Nom du batiment', extra: 'Complément d\'adresse', - distribution: 'Service particulier de distribution' + distribution: 'Service particulier de distribution', + create_postal_code: 'Localité inconnue. Cliquez ici pour créer une nouvelle localité', + postalCode_name: 'Nom de la localité', + postalCode_code: 'Code postal de la localité' } }; 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 59e573d19..452936208 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 { postAddress } from '../../_api/AddAddress' +import { postAddress, postPostalCode } from '../../_api/AddAddress' const debug = process.env.NODE_ENV !== 'production'; @@ -25,19 +25,32 @@ const store = createStore({ actions: { addAddress({ commit }, payload) { console.log('@A addAddress payload', payload); - //commit('addAddress', payload); // à remplacer par la suite - //fetch POST qui envoie l'adresse, et récupère la confirmation que c'est ok. - //La confirmation est l'adresse elle-même. + if('newPostalCode' in payload){ + postPostalCode(payload.newPostalCode) + .then(postalCode => { + let body = payload; + body.postcode = {'id': postalCode.id }, + postAddress(body) + .then(address => new Promise((resolve, reject) => { + commit('addAddress', address); + resolve(); + })) + .catch((error) => { + commit('catchError', error); + }); + }) - postAddress(payload) - .then(address => new Promise((resolve, reject) => { - commit('addAddress', address); - resolve(); - })) - .catch((error) => { - commit('catchError', error); - }); + } else { + postAddress(payload) + .then(address => new Promise((resolve, reject) => { + commit('addAddress', 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 ddba5ebde..9b9619331 100644 --- a/src/Bundle/ChillMainBundle/Resources/public/vuejs/_api/AddAddress.js +++ b/src/Bundle/ChillMainBundle/Resources/public/vuejs/_api/AddAddress.js @@ -66,7 +66,6 @@ const fetchAddresses = () => { * @returns {Promise} */ const postAddress = (address) => { - console.log(address); const url = `/api/1.0/main/address.json?`; const body = address; @@ -91,7 +90,6 @@ const postAddress = (address) => { * @body Object - dictionary with changes to post */ const patchAddress = (id, body) => { - const url = `/api/1.0/main/address/${id}.json`; return fetch(url, { method: 'PATCH', @@ -106,7 +104,26 @@ const patchAddress = (id, body) => { }); }; +/* +* Endpoint chill_api_single_postal_code__entity_create +* method POST, post Postal Code Object +* @returns {Promise} +*/ +const postPostalCode = (postalCode) => { + const url = `/api/1.0/main/postal-code.json?`; + const body = postalCode; + return fetch(url, { + method: 'POST', + headers: { + 'Content-Type': 'application/json;charset=utf-8' + }, + body: JSON.stringify(body) + }).then(response => { + if (response.ok) { return response.json(); } + throw Error('Error with request resource response'); + }); +}; export { fetchCountries, @@ -114,5 +131,6 @@ export { fetchReferenceAddresses, fetchAddresses, postAddress, - patchAddress + patchAddress, + postPostalCode }; diff --git a/src/Bundle/ChillMainBundle/Resources/public/vuejs/_components/AddAddress.vue b/src/Bundle/ChillMainBundle/Resources/public/vuejs/_components/AddAddress.vue index 1fbeae8cb..7e5fb69e0 100644 --- a/src/Bundle/ChillMainBundle/Resources/public/vuejs/_components/AddAddress.vue +++ b/src/Bundle/ChillMainBundle/Resources/public/vuejs/_components/AddAddress.vue @@ -106,6 +106,7 @@ export default { }, address: { writeNewAddress: false, + writeNewPostalCode: false, loaded: { countries: [], cities: [], @@ -116,6 +117,10 @@ export default { city: {}, address: {}, }, + newPostalCode: { + code: null, + name: null + }, addressMap: { center : [48.8589, 2.3469], // Note: LeafletJs demands [lat, lon] cfr https://macwright.com/lonlat/ zoom: 12 @@ -211,6 +216,8 @@ export default { this.address.distribution = null; this.address.extra = null; this.address.writeNewAddress = false; + this.address.writeNewPostalCode = false; + this.address.newPostalCode = {}; console.log('cities and addresses', this.address.loaded.cities, this.address.loaded.addresses); } } diff --git a/src/Bundle/ChillMainBundle/Resources/public/vuejs/_components/AddAddress/AddressSelection.vue b/src/Bundle/ChillMainBundle/Resources/public/vuejs/_components/AddAddress/AddressSelection.vue index dc4fef0dc..0bab7f570 100644 --- a/src/Bundle/ChillMainBundle/Resources/public/vuejs/_components/AddAddress/AddressSelection.vue +++ b/src/Bundle/ChillMainBundle/Resources/public/vuejs/_components/AddAddress/AddressSelection.vue @@ -15,7 +15,7 @@ :options="addresses"> -
+
+
+ + +