diff --git a/src/Bundle/ChillActivityBundle/Resources/public/vuejs/Activity/api.js b/src/Bundle/ChillActivityBundle/Resources/public/vuejs/Activity/api.js index 70ba4c04e..8d4bcac3b 100644 --- a/src/Bundle/ChillActivityBundle/Resources/public/vuejs/Activity/api.js +++ b/src/Bundle/ChillActivityBundle/Resources/public/vuejs/Activity/api.js @@ -1,4 +1,5 @@ import { getSocialIssues } from 'ChillPersonAssets/vuejs/AccompanyingCourse/api.js'; +import { fetchResults } from 'ChillMainAssets/lib/api/apiMethods'; /* * Load socialActions by socialIssue (id) @@ -12,44 +13,20 @@ const getSocialActionByIssue = (id) => { }); }; -/* -* Load Locations - */ -const getLocations = () => { - const url = `/api/1.0/main/location.json`; - return fetch(url) - .then(response => { - if (response.ok) { return response.json(); } - throw Error('Error with request resource response'); - }); -}; - -/* -* Load Location Types - */ -const getLocationTypes = () => { - const url = `/api/1.0/main/location-type.json`; - return fetch(url) - .then(response => { - if (response.ok) { return response.json(); } - throw Error('Error with request resource response'); - }); -}; +const getLocations = () => fetchResults('/api/1.0/main/location.json'); +const getLocationTypes = () => fetchResults('/api/1.0/main/location-type.json'); /* * Load Location Type by defaultFor * @param {string} entity - can be "person" or "thirdparty" */ const getLocationTypeByDefaultFor = (entity) => { - return getLocationTypes().then(response => - response.results.filter(t => t.defaultFor === entity)[0] + return getLocationTypes().then(results => + results.filter(t => t.defaultFor === entity)[0] ); }; -/* -* Post a Location - */ const postLocation = (body) => { const url = `/api/1.0/main/location.json`; return fetch(url, { diff --git a/src/Bundle/ChillActivityBundle/Resources/public/vuejs/Activity/components/Location.vue b/src/Bundle/ChillActivityBundle/Resources/public/vuejs/Activity/components/Location.vue index 012c550aa..9a4b78334 100644 --- a/src/Bundle/ChillActivityBundle/Resources/public/vuejs/Activity/components/Location.vue +++ b/src/Bundle/ChillActivityBundle/Resources/public/vuejs/Activity/components/Location.vue @@ -59,53 +59,50 @@ export default { }, mounted() { getLocations().then( - (response) => - new Promise((resolve) => { - getLocationTypeByDefaultFor('person').then( - personLocationType => { - if (personLocationType) { - const personLocation = this.makeAccompanyingPeriodLocation(personLocationType); - const concernedPersonsLocation = - this.makeConcernedPersonsLocation(personLocationType); - getLocationTypeByDefaultFor('thirdparty').then( - thirdpartyLocationType => { - const concernedThirdPartiesLocation = - this.makeConcernedThirdPartiesLocation(thirdpartyLocationType); - this.locations = [ - { - locationGroup: 'Localisation du parcours', - locations: [personLocation] - }, - { - locationGroup: 'Parties concernées', - locations: [...concernedPersonsLocation, ...concernedThirdPartiesLocation] - }, - { - locationGroup: 'Autres localisations', - locations: response.results - } - ]; - } - ) - } else { - this.locations = [ - { - locationGroup: 'Localisations', - locations: response.results - } - ]; - } + (results) => { + getLocationTypeByDefaultFor('person').then( + (personLocationType) => { + if (personLocationType) { + const personLocation = this.makeAccompanyingPeriodLocation(personLocationType); + const concernedPersonsLocation = + this.makeConcernedPersonsLocation(personLocationType); + getLocationTypeByDefaultFor('thirdparty').then( + thirdpartyLocationType => { + const concernedThirdPartiesLocation = + this.makeConcernedThirdPartiesLocation(thirdpartyLocationType); + this.locations = [ + { + locationGroup: 'Localisation du parcours', + locations: [personLocation] + }, + { + locationGroup: 'Parties concernées', + locations: [...concernedPersonsLocation, ...concernedThirdPartiesLocation] + }, + { + locationGroup: 'Autres localisations', + locations: results + } + ]; + } + ) + } else { + this.locations = [ + { + locationGroup: 'Localisations', + locations: response.results + } + ]; + } if (window.default_location_id) { let location = this.locations.filter( (l) => l.id === window.default_location_id ); this.$store.dispatch("updateLocation", location); } - resolve(); - } - ) - }) - ); + } + ) + }) }, methods: { labelAccompanyingCourseLocation(value) { diff --git a/src/Bundle/ChillActivityBundle/Resources/public/vuejs/Activity/components/Location/NewLocation.vue b/src/Bundle/ChillActivityBundle/Resources/public/vuejs/Activity/components/Location/NewLocation.vue index 34aa607e9..35bf9a065 100644 --- a/src/Bundle/ChillActivityBundle/Resources/public/vuejs/Activity/components/Location/NewLocation.vue +++ b/src/Bundle/ChillActivityBundle/Resources/public/vuejs/Activity/components/Location/NewLocation.vue @@ -214,10 +214,9 @@ export default { return cond; }, getLocationTypesList() { - getLocationTypes().then(response => new Promise(resolve => { - this.locationTypes = response.results.filter(t => t.availableForUsers === true); - resolve(); - })) + getLocationTypes().then(results => { + this.locationTypes = results.filter(t => t.availableForUsers === true); + }) }, openModal() { this.modal.showModal = true;