diff --git a/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php b/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php index 853a38c1b..23a6e4de9 100644 --- a/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php +++ b/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php @@ -277,7 +277,8 @@ class ChillMainExtension extends Extension implements PrependExtensionInterface, 'methods' => [ Request::METHOD_GET => true, Request::METHOD_POST => true, - Request::METHOD_HEAD => true + Request::METHOD_HEAD => true, + Request::METHOD_PATCH => true ] ], ] @@ -319,7 +320,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 433ce58bf..910245680 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,10 +50,17 @@ class PostalCode * @var Country * * @ORM\ManyToOne(targetEntity="Chill\MainBundle\Entity\Country") - * @groups({"read"}) + * @groups({"write", "read"}) */ private $country; + /** + * @var integer + * + * @ORM\Column(name="origin", type="integer", nullable=true) + * @groups({"write", "read"}) + */ + private $origin = 0; /** * Get id @@ -65,6 +72,32 @@ class PostalCode return $this->id; } + + /** + * Set origin + * + * @param int $origin + * + * @return PostalCode + */ + public function setOrigin($origin) + { + $this->origin = $origin; + + return $this; + } + + /** + * Get origin + * + * @return int + */ + public function getOrigin() + { + return $this->origin; + } + + /** * Set name * diff --git a/src/Bundle/ChillMainBundle/Resources/public/scss/chillmain.scss b/src/Bundle/ChillMainBundle/Resources/public/scss/chillmain.scss index 30c7f561d..741dfb119 100644 --- a/src/Bundle/ChillMainBundle/Resources/public/scss/chillmain.scss +++ b/src/Bundle/ChillMainBundle/Resources/public/scss/chillmain.scss @@ -248,6 +248,9 @@ div.address_form { display: flex; flex-direction: column; flex-grow: 1; + div.custom-address, div.custom-postcode { + padding: 12px; + } } div.address_form__select__map { @@ -255,13 +258,20 @@ div.address_form { div#address_map { height:400px; width:400px; + input { + border: 1px solid #999; + } } } } div.address_form__more { - + & > div { + display: flex; + & > label { + width: 30%; + } + } } } - diff --git a/src/Bundle/ChillMainBundle/Resources/public/vuejs/Address/App.vue b/src/Bundle/ChillMainBundle/Resources/public/vuejs/Address/App.vue index 7c27db341..c0d3271ef 100644 --- a/src/Bundle/ChillMainBundle/Resources/public/vuejs/Address/App.vue +++ b/src/Bundle/ChillMainBundle/Resources/public/vuejs/Address/App.vue @@ -1,33 +1,73 @@ 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 ba79e88a7..b6f483894 100644 --- a/src/Bundle/ChillMainBundle/Resources/public/vuejs/Address/js/i18n.js +++ b/src/Bundle/ChillMainBundle/Resources/public/vuejs/Address/js/i18n.js @@ -1,20 +1,32 @@ const addressMessages = { fr: { - add_an_address_title: 'Ajouter une adresse', + add_an_address_title: 'Créer une adresse', + edit_an_address_title: 'Modifier une adresse', + create_a_new_address: 'Créer une nouvelle adresse', + edit_a_new_address: 'Modifier l\'adresse', select_an_address_title: 'Sélectionner une adresse', fill_an_address: 'Compléter l\'adresse', select_country: 'Choisir le pays', select_city: 'Choisir une localité', select_address: 'Choisir une adresse', - create_address: 'Appuyer sur "Entrée" pour créer une nouvelle adresse', + create_address: 'Adresse inconnue. Cliquez ici pour créer une nouvelle adresse', isNoAddress: 'Pas d\'adresse complète', + street: 'Nom de rue', + streetNumber: 'Numéro', floor: 'Étage', corridor: 'Couloir', steps: 'Escalier', 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é', + date: 'Date de la nouvelle adresse', + add_an_address_to_person: 'Ajouter l\'adresse à la personne', + validFrom: 'Date de la nouvelle adresse', + back_to_the_list: 'Retour à la liste' } }; 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..acbf4dcbb 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 { 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: { @@ -20,25 +22,119 @@ const store = createStore({ addAddress(state, address) { 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; + }, + 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) { 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){ + let postalCodeBody = payload.newPostalCode; + postalCodeBody = Object.assign(postalCodeBody, {'origin': 3}); + postPostalCode(postalCodeBody) + .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) + } else { + postAddress(payload) + .then(address => new Promise((resolve, reject) => { + commit('addAddress', address); + resolve(); + })) + .catch((error) => { + commit('catchError', error); + }); + } + }, + addDateToAddressAndAddressToPerson({ commit }, payload) { + console.log('@A addDateToAddressAndAddressToPerson payload', payload); + + patchAddress(payload.addressId, payload.body) .then(address => new Promise((resolve, reject) => { - commit('addAddress', address); + commit('addDateToAddress', address.validFrom); resolve(); - })) + }).then( + postAddressToPerson(payload.personId, payload.addressId) + .then(person => new Promise((resolve, reject) => { + commit('addAddressToPerson', person); + resolve(); + })) + .catch((error) => { + commit('catchError', error); + }) + )) .catch((error) => { commit('catchError', error); }); - } + }, + updateAddress({ commit }, payload) { + console.log('@A updateAddress payload', payload); + + if('newPostalCode' in payload.newAddress){ // TODO change the condition because it writes new postal code in edit mode now: !writeNewPostalCode + let postalCodeBody = payload.newAddress.newPostalCode; + postalCodeBody = Object.assign(postalCodeBody, {'origin': 3}); + postPostalCode(postalCodeBody) + .then(postalCode => { + let body = payload.newAddress; + body.postcode = {'id': postalCode.id }, + patchAddress(payload.addressId, 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 db03da860..0f078a2d9 100644 --- a/src/Bundle/ChillMainBundle/Resources/public/vuejs/_api/AddAddress.js +++ b/src/Bundle/ChillMainBundle/Resources/public/vuejs/_api/AddAddress.js @@ -36,7 +36,6 @@ const fetchCities = (country) => { */ const fetchReferenceAddresses = (postalCode) => { console.log('<<< fetching references addresses for', postalCode); - //TODO deal with huge number of addresses... we should do suggestion... const url = `/api/1.0/main/address-reference.json?item_per_page=1000&postal_code=${postalCode.id}`; return fetch(url) .then(response => { @@ -45,16 +44,75 @@ const fetchReferenceAddresses = (postalCode) => { }); }; +/* +* Endpoint chill_api_single_address_reference__index +* method GET, get AddressReference Object +* @returns {Promise} a promise containing all AddressReference objects filtered with postal code +*/ +const fetchAddresses = () => { + console.log('<<< fetching addresses'); + //TODO deal with huge number of addresses... we should do suggestion... + const url = `/api/1.0/main/address.json?item_per_page=1000`; + return fetch(url) + .then(response => { + if (response.ok) { return response.json(); } + throw Error('Error with request resource response'); + }); +}; + /* * Endpoint chill_api_single_address__entity__create * method POST, post Address Object * @returns {Promise} */ const postAddress = (address) => { - console.log(address); const url = `/api/1.0/main/address.json?`; const body = address; + 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'); + }); +}; + + +/* +* Endpoint chill_api_single_address__entity__create +* method PATCH, patch Address Instance +* +* @id integer - id of 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', + 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'); + }); +}; + +/* +* 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: { @@ -67,9 +125,55 @@ const postAddress = (address) => { }); }; +/* +* Endpoint chill_api_single_person_address +* method POST, post Person instance +* +* @id integer - id of Person +* @body Object - dictionary with changes to post +*/ +const postAddressToPerson = (personId, addressId) => { + console.log(personId); + console.log(addressId); + const body = { + 'id': addressId + }; + const url = `/api/1.0/person/person/${personId}/address.json` + 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'); + }); +}; + +/* +* 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, fetchCities, fetchReferenceAddresses, - postAddress + fetchAddresses, + postAddress, + patchAddress, + postPostalCode, + 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 6072cf850..3417a2d0d 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 @@