location: use fetchResults for getLocations and getLocationTypes

This commit is contained in:
nobohan 2021-11-25 17:22:25 +01:00
parent f002d48efd
commit 5aa5cb3e08
3 changed files with 46 additions and 73 deletions

View File

@ -1,4 +1,5 @@
import { getSocialIssues } from 'ChillPersonAssets/vuejs/AccompanyingCourse/api.js'; import { getSocialIssues } from 'ChillPersonAssets/vuejs/AccompanyingCourse/api.js';
import { fetchResults } from 'ChillMainAssets/lib/api/apiMethods';
/* /*
* Load socialActions by socialIssue (id) * Load socialActions by socialIssue (id)
@ -12,44 +13,20 @@ const getSocialActionByIssue = (id) => {
}); });
}; };
/* const getLocations = () => fetchResults('/api/1.0/main/location.json');
* 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 getLocationTypes = () => fetchResults('/api/1.0/main/location-type.json');
/* /*
* Load Location Type by defaultFor * Load Location Type by defaultFor
* @param {string} entity - can be "person" or "thirdparty" * @param {string} entity - can be "person" or "thirdparty"
*/ */
const getLocationTypeByDefaultFor = (entity) => { const getLocationTypeByDefaultFor = (entity) => {
return getLocationTypes().then(response => return getLocationTypes().then(results =>
response.results.filter(t => t.defaultFor === entity)[0] results.filter(t => t.defaultFor === entity)[0]
); );
}; };
/*
* Post a Location
*/
const postLocation = (body) => { const postLocation = (body) => {
const url = `/api/1.0/main/location.json`; const url = `/api/1.0/main/location.json`;
return fetch(url, { return fetch(url, {

View File

@ -59,53 +59,50 @@ export default {
}, },
mounted() { mounted() {
getLocations().then( getLocations().then(
(response) => (results) => {
new Promise((resolve) => { getLocationTypeByDefaultFor('person').then(
getLocationTypeByDefaultFor('person').then( (personLocationType) => {
personLocationType => { if (personLocationType) {
if (personLocationType) { const personLocation = this.makeAccompanyingPeriodLocation(personLocationType);
const personLocation = this.makeAccompanyingPeriodLocation(personLocationType); const concernedPersonsLocation =
const concernedPersonsLocation = this.makeConcernedPersonsLocation(personLocationType);
this.makeConcernedPersonsLocation(personLocationType); getLocationTypeByDefaultFor('thirdparty').then(
getLocationTypeByDefaultFor('thirdparty').then( thirdpartyLocationType => {
thirdpartyLocationType => { const concernedThirdPartiesLocation =
const concernedThirdPartiesLocation = this.makeConcernedThirdPartiesLocation(thirdpartyLocationType);
this.makeConcernedThirdPartiesLocation(thirdpartyLocationType); this.locations = [
this.locations = [ {
{ locationGroup: 'Localisation du parcours',
locationGroup: 'Localisation du parcours', locations: [personLocation]
locations: [personLocation] },
}, {
{ locationGroup: 'Parties concernées',
locationGroup: 'Parties concernées', locations: [...concernedPersonsLocation, ...concernedThirdPartiesLocation]
locations: [...concernedPersonsLocation, ...concernedThirdPartiesLocation] },
}, {
{ locationGroup: 'Autres localisations',
locationGroup: 'Autres localisations', locations: results
locations: response.results }
} ];
]; }
} )
) } else {
} else { this.locations = [
this.locations = [ {
{ locationGroup: 'Localisations',
locationGroup: 'Localisations', locations: response.results
locations: response.results }
} ];
]; }
}
if (window.default_location_id) { if (window.default_location_id) {
let location = this.locations.filter( let location = this.locations.filter(
(l) => l.id === window.default_location_id (l) => l.id === window.default_location_id
); );
this.$store.dispatch("updateLocation", location); this.$store.dispatch("updateLocation", location);
} }
resolve(); }
} )
) })
})
);
}, },
methods: { methods: {
labelAccompanyingCourseLocation(value) { labelAccompanyingCourseLocation(value) {

View File

@ -214,10 +214,9 @@ export default {
return cond; return cond;
}, },
getLocationTypesList() { getLocationTypesList() {
getLocationTypes().then(response => new Promise(resolve => { getLocationTypes().then(results => {
this.locationTypes = response.results.filter(t => t.availableForUsers === true); this.locationTypes = results.filter(t => t.availableForUsers === true);
resolve(); })
}))
}, },
openModal() { openModal() {
this.modal.showModal = true; this.modal.showModal = true;