diff --git a/src/Bundle/ChillActivityBundle/DataFixtures/ORM/LoadActivityNotifications.php b/src/Bundle/ChillActivityBundle/DataFixtures/ORM/LoadActivityNotifications.php index 588b2b7e1..7835a0db4 100644 --- a/src/Bundle/ChillActivityBundle/DataFixtures/ORM/LoadActivityNotifications.php +++ b/src/Bundle/ChillActivityBundle/DataFixtures/ORM/LoadActivityNotifications.php @@ -3,24 +3,18 @@ namespace Chill\ActivityBundle\DataFixtures\ORM; use Doctrine\Common\DataFixtures\AbstractFixture; -use Doctrine\Common\DataFixtures\OrderedFixtureInterface; -use Doctrine\Persistence\ObjectManager; -use Symfony\Component\Intl\Intl; -use Chill\MainBundle\Entity\Notification; +use Doctrine\Common\DataFixtures\DependentFixtureInterface; use Chill\ActivityBundle\Entity\Activity; use Chill\MainBundle\DataFixtures\ORM\LoadAbstractNotificationsTrait; +use Chill\ActivityBundle\DataFixtures\ORM\LoadActivity; /** * Load notififications into database */ -class LoadActivityNotifications extends AbstractFixture implements OrderedFixtureInterface +class LoadActivityNotifications extends AbstractFixture implements DependentFixtureInterface { use LoadAbstractNotificationsTrait; - public function getOrder() { - return 16500; - } - public $notifs = [ [ 'message' => 'Hello !', @@ -35,5 +29,10 @@ class LoadActivityNotifications extends AbstractFixture implements OrderedFixtur ] ]; - + public function getDependencies() + { + return [ + LoadActivity::class, + ]; + } } diff --git a/src/Bundle/ChillMainBundle/CRUD/CompilerPass/CRUDControllerCompilerPass.php b/src/Bundle/ChillMainBundle/CRUD/CompilerPass/CRUDControllerCompilerPass.php index d8a46837c..11b3c34fd 100644 --- a/src/Bundle/ChillMainBundle/CRUD/CompilerPass/CRUDControllerCompilerPass.php +++ b/src/Bundle/ChillMainBundle/CRUD/CompilerPass/CRUDControllerCompilerPass.php @@ -22,6 +22,7 @@ use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\Reference; use Chill\MainBundle\Routing\MenuComposer; use Symfony\Component\DependencyInjection\Definition; +use Symfony\Component\DependencyInjection\Alias; /** * @@ -49,29 +50,32 @@ class CRUDControllerCompilerPass implements CompilerPassInterface private function configureCrudController(ContainerBuilder $container, array $crudEntry, string $apiOrCrud): void { $controllerClass = $crudEntry['controller']; - $controllerServiceName = 'cs'.$apiOrCrud.'_'.$crudEntry['name'].'_controller'; - if ($container->hasDefinition($controllerClass)) { - $controller = $container->getDefinition($controllerClass); - $container->removeDefinition($controllerClass); - $alreadyDefined = true; - } else { - $controller = new Definition($controllerClass); - $alreadyDefined = false; - } - - $controller->addTag('controller.service_arguments'); - if (FALSE === $alreadyDefined) { - $controller->setAutoconfigured(true); - $controller->setPublic(true); - } - + // create config parameter in container $param = 'chill_main_'.$apiOrCrud.'_config_'.$crudEntry['name']; $container->setParameter($param, $crudEntry); - $controller->addMethodCall('setCrudConfig', ['%'.$param.'%']); - $container->setDefinition($controllerServiceName, $controller); + if ($container->hasDefinition($controllerClass)) { + // create an alias to not to re-create the service + $alias = new Alias($controllerClass, true); + $container->setAlias($controllerServiceName, $alias); + + // add the "addMethodCall" + $container->getDefinition($controllerClass) + ->addMethodCall('setCrudConfig', ['%'.$param.'%']); + + } else { + $controller = new Definition($controllerClass); + + $controller->addTag('controller.service_arguments'); + $controller->setAutoconfigured(true); + $controller->setPublic(true); + + $controller->addMethodCall('setCrudConfig', ['%'.$param.'%']); + + $container->setDefinition($controllerServiceName, $controller); + } } } diff --git a/src/Bundle/ChillMainBundle/CRUD/Controller/ApiController.php b/src/Bundle/ChillMainBundle/CRUD/Controller/ApiController.php index 9686a7b98..cab0bcdcb 100644 --- a/src/Bundle/ChillMainBundle/CRUD/Controller/ApiController.php +++ b/src/Bundle/ChillMainBundle/CRUD/Controller/ApiController.php @@ -359,9 +359,10 @@ class ApiController extends AbstractCRUDController * 6. validate the base entity (not the deserialized one). Groups are fetched from getValidationGroups, validation is perform by `validate` * 7. run onAfterValidation * 8. if errors, return a 422 response with errors - * 9. flush the data - * 10. run onAfterFlush - * 11. return a 202 response for DELETE with empty body, or HTTP 200 for post with serialized posted entity + * 9. if $forcePersist === true, persist the entity + * 10. flush the data + * 11. run onAfterFlush + * 12. return a 202 response for DELETE with empty body, or HTTP 200 for post with serialized posted entity * * @param string action * @param mixed id @@ -370,11 +371,12 @@ class ApiController extends AbstractCRUDController * @param string $property the name of the property. This will be used to make a `add+$property` and `remove+$property` method * @param string $postedDataType the type of the posted data (the content) * @param string $postedDataContext a context to deserialize posted data (the content) + * @param bool $forcePersist force to persist the created element (only for POST request) * @throw BadRequestException if unable to deserialize the posted data * @throw BadRequestException if the method is not POST or DELETE * */ - protected function addRemoveSomething(string $action, $id, Request $request, string $_format, string $property, string $postedDataType, $postedDataContext = []): Response + protected function addRemoveSomething(string $action, $id, Request $request, string $_format, string $property, string $postedDataType, array $postedDataContext = [], bool $forcePersist = false): Response { $entity = $this->getEntity($action, $id, $request); @@ -429,6 +431,10 @@ class ApiController extends AbstractCRUDController return $this->json($errors, 422); } + if ($forcePersist && $request->getMethod() === Request::METHOD_POST) { + $this->getDoctrine()->getManager()->persist($postedData); + } + $this->getDoctrine()->getManager()->flush(); diff --git a/src/Bundle/ChillMainBundle/CRUD/Routing/CRUDRoutesLoader.php b/src/Bundle/ChillMainBundle/CRUD/Routing/CRUDRoutesLoader.php index 9d61d6233..36aadb8b7 100644 --- a/src/Bundle/ChillMainBundle/CRUD/Routing/CRUDRoutesLoader.php +++ b/src/Bundle/ChillMainBundle/CRUD/Routing/CRUDRoutesLoader.php @@ -142,11 +142,11 @@ class CRUDRoutesLoader extends Loader protected function loadApi(array $crudConfig): RouteCollection { $collection = new RouteCollection(); - $controller ='csapi_'.$crudConfig['name'].'_controller'; + $controller = 'csapi_'.$crudConfig['name'].'_controller'; foreach ($crudConfig['actions'] as $name => $action) { // filter only on single actions - $singleCollection = $action['single-collection'] ?? $name === '_entity' ? 'single' : NULL; + $singleCollection = $action['single_collection'] ?? $name === '_entity' ? 'single' : NULL; if ('collection' === $singleCollection) { // continue; } @@ -171,7 +171,7 @@ class CRUDRoutesLoader extends Loader // path are rewritten // if name === 'default', we rewrite it to nothing :-) $localName = \in_array($name, [ '_entity', '_index' ]) ? '' : '/'.$name; - if ('collection' === $action['single-collection'] || '_index' === $name) { + if ('collection' === $action['single_collection'] || '_index' === $name) { $localPath = $action['path'] ?? $localName.'.{_format}'; } else { $localPath = $action['path'] ?? '/{id}'.$localName.'.{_format}'; diff --git a/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php b/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php index 853a38c1b..756e075d9 100644 --- a/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php +++ b/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php @@ -195,7 +195,9 @@ class ChillMainExtension extends Extension implements PrependExtensionInterface, ->prependExtensionConfig( 'doctrine', [ - 'dbal' => [ + 'dbal' => [ + // ignore views: + 'schema_filter' => '~^(?!view_)~', // This is mandatory since we are using postgis as database. 'mapping_types' => [ 'geometry' => 'string', @@ -277,7 +279,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 +322,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/DependencyInjection/Configuration.php b/src/Bundle/ChillMainBundle/DependencyInjection/Configuration.php index 4c90aaabb..c711f911f 100644 --- a/src/Bundle/ChillMainBundle/DependencyInjection/Configuration.php +++ b/src/Bundle/ChillMainBundle/DependencyInjection/Configuration.php @@ -205,7 +205,7 @@ class Configuration implements ConfigurationInterface ->ignoreExtraKeys(false) ->info('the requirements for the route. Will be set to `[ \'id\' => \'\d+\' ]` if left empty.') ->end() - ->enumNode('single-collection') + ->enumNode('single_collection') ->values(['single', 'collection']) ->defaultValue('single') ->info('indicates if the returned object is a single element or a collection. '. diff --git a/src/Bundle/ChillMainBundle/Entity/Address.php b/src/Bundle/ChillMainBundle/Entity/Address.php index 3bbbfa368..af3fea746 100644 --- a/src/Bundle/ChillMainBundle/Entity/Address.php +++ b/src/Bundle/ChillMainBundle/Entity/Address.php @@ -116,7 +116,7 @@ class Address * @ORM\Column(type="date") * @groups({"write"}) */ - private $validFrom; + private \DateTime $validFrom; /** * Indicates when the address ends. Used to build an history @@ -127,7 +127,7 @@ class Address * @ORM\Column(type="date", nullable=true) * @groups({"write"}) */ - private $validTo; + private ?\DateTime $validTo = null; /** * True if the address is a "no address", aka homeless person, ... 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/js/date.js b/src/Bundle/ChillMainBundle/Resources/public/js/date.js index 7b9bf88a2..e49a05972 100644 --- a/src/Bundle/ChillMainBundle/Resources/public/js/date.js +++ b/src/Bundle/ChillMainBundle/Resources/public/js/date.js @@ -11,13 +11,16 @@ * * Do not take time into account * - * **Experimental** */ const dateToISO = (date) => { + if (null === date) { + return null; + } + return [ - this.$store.state.startDate.getFullYear(), - (this.$store.state.startDate.getMonth() + 1).toString().padStart(2, '0'), - this.$store.state.startDate.getDate().toString().padStart(2, '0') + date.getFullYear(), + (date.getMonth() + 1).toString().padStart(2, '0'), + date.getDate().toString().padStart(2, '0') ].join('-'); }; @@ -36,10 +39,12 @@ const ISOToDate = (str) => { /** * Return a date object from iso string formatted as YYYY-mm-dd:HH:MM:ss+01:00 * - * **Experimental** */ const ISOToDatetime = (str) => { - console.log(str); + if (null === str) { + return null; + } + let [cal, times] = str.split('T'), [year, month, date] = cal.split('-'), diff --git a/src/Bundle/ChillMainBundle/Resources/public/modules/scratch/custom/modules/_buttons.scss b/src/Bundle/ChillMainBundle/Resources/public/modules/scratch/custom/modules/_buttons.scss index 0b7af263b..7c332818d 100644 --- a/src/Bundle/ChillMainBundle/Resources/public/modules/scratch/custom/modules/_buttons.scss +++ b/src/Bundle/ChillMainBundle/Resources/public/modules/scratch/custom/modules/_buttons.scss @@ -30,7 +30,7 @@ &.bt-cancel::before, &.bt-view::before, &.bt-show::before { - font: normal normal normal 14px/1 FontAwesome; + font: normal normal normal 14px/1 ForkAwesome; margin-right: 0.5em; } diff --git a/src/Bundle/ChillMainBundle/Resources/public/scss/_alert-first-child.scss b/src/Bundle/ChillMainBundle/Resources/public/scss/_alert-first-child.scss new file mode 100644 index 000000000..9a089c8a7 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Resources/public/scss/_alert-first-child.scss @@ -0,0 +1,10 @@ +/* + * when an alert is the first child of the page, with a banner, we do not want the alert to be merged with the banner + */ +div.container.content { + & > div { + div.alert:nth-child(2) { + margin-top: 1rem; + } + } +} diff --git a/src/Bundle/ChillMainBundle/Resources/public/scss/_alert-with-actions.scss b/src/Bundle/ChillMainBundle/Resources/public/scss/_alert-with-actions.scss new file mode 100644 index 000000000..0779ca7b3 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Resources/public/scss/_alert-with-actions.scss @@ -0,0 +1,29 @@ + +div.alert.alert-with-actions { + display: flex; + flex-direction: row; + + ul.record_actions { + display: flex; + flex-direction: column; + padding: 0; + margin: 0; + + li:nth-child(1n+2) { + margin-top: 0.5rem; + } + + li { + margin-right: 0; + } + } + + @media screen and (max-width: 1050px) { + flex-direction: column; + + ul.record_actions { + margin-top: 1rem; + text-align: center; + } + } +} diff --git a/src/Bundle/ChillMainBundle/Resources/public/scss/chillmain.scss b/src/Bundle/ChillMainBundle/Resources/public/scss/chillmain.scss index 30c7f561d..1d0ad253f 100644 --- a/src/Bundle/ChillMainBundle/Resources/public/scss/chillmain.scss +++ b/src/Bundle/ChillMainBundle/Resources/public/scss/chillmain.scss @@ -7,6 +7,10 @@ */ +@import 'alert-first-child'; +@import 'alert-with-actions'; + + /* [hack] /!\ Contourne le positionnement problématique du div#content_conainter suivant, * car sa position: relative le place au-dessus du bandeau et les liens sont incliquables */ div.subheader { @@ -248,6 +252,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 +262,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 @@