adapt payload body to entity convention with type + id

This commit is contained in:
Mathieu Jaumotte 2021-05-12 18:50:18 +02:00
parent 0af78f814c
commit 4a04628d5b
3 changed files with 16 additions and 14 deletions

View File

@ -2,8 +2,8 @@
const getBodyTypeId = (payload) => {
const body = {};
if (payload !== null) {
const typeId = `${payload.result.type}_id`;
body[typeId] = payload.result[typeId];
body['type'] = payload.type;
body['id'] = payload.id;
};
console.log('body', body);
return body;
@ -52,17 +52,18 @@ const patchAccompanyingCourse = (id, body) => {
* method POST/DELETE, add/close a participation to the accompanyingCourse
*
* @id integer - id of accompanyingCourse
* @person_id integer - id of person
* @payload integer - id of person
* @method string - POST or DELETE
*/
const postParticipation = (id, person_id, method) => {
const postParticipation = (id, payload, method) => {
const body = getBodyTypeId(payload);
const url = `/api/1.0/person/accompanying-course/${id}/participation.json`;
return fetch(url, {
method: method,
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
body: JSON.stringify({person_id: person_id})
body: JSON.stringify(body)
})
.then(response => {
if (response.ok) { return response.json(); }

View File

@ -66,7 +66,7 @@ let initPromise = getAccompanyingCourse(id)
},
closeParticipation({ commit }, payload) {
console.log('## action: fetch delete participation: payload', payload);
postParticipation(id, payload.person.person_id, 'DELETE')
postParticipation(id, payload.person, 'DELETE')
.then(participation => new Promise((resolve, reject) => {
commit('closeParticipation', { participation, payload });
resolve();
@ -77,7 +77,7 @@ let initPromise = getAccompanyingCourse(id)
},
addParticipation({ commit }, payload) {
console.log('## action: fetch post participation (select item): payload', payload);
postParticipation(id, payload.result.person_id, 'POST')
postParticipation(id, payload.result, 'POST')
.then(participation => new Promise((resolve, reject) => {
commit('addParticipation', participation);
resolve();
@ -86,12 +86,12 @@ let initPromise = getAccompanyingCourse(id)
state.errorMsg.push(error.message);
});
},
removeRequestor({ commit }) {
removeRequestor({ commit, dispatch }) {
console.log('## action: fetch delete requestor');
postRequestor(id, null, 'DELETE')
.then(requestor => new Promise((resolve, reject) => {
commit('removeRequestor');
commit('requestorIsAnonymous', false);
dispatch('requestorIsAnonymous', false);
resolve();
}))
.catch((error) => {
@ -100,7 +100,7 @@ let initPromise = getAccompanyingCourse(id)
},
addRequestor({ commit }, payload) {
console.log('## action: fetch post requestor: payload', payload);
postRequestor(id, payload, 'POST')
postRequestor(id, payload.result, 'POST')
.then(requestor => new Promise((resolve, reject) => {
commit('addRequestor', requestor);
resolve();
@ -111,7 +111,7 @@ let initPromise = getAccompanyingCourse(id)
},
requestorIsAnonymous({ commit }, payload) {
console.log('## action: fetch patch AccompanyingCourse: payload', payload);
patchAccompanyingCourse(id, { requestorAnonymous: payload })
patchAccompanyingCourse(id, { type: "accompanying_period", requestorAnonymous: payload })
.then(course => new Promise((resolve, reject) => {
commit('requestorIsAnonymous', course.requestorAnonymous)
resolve();
@ -133,7 +133,7 @@ let initPromise = getAccompanyingCourse(id)
},
addInterlocutor({ commit }, payload) {
console.log('## action: fetch postInterlocutor: payload', payload);
postResource(id, payload, 'POST')
postResource(id, payload.result, 'POST')
.then(resource => new Promise((resolve, reject) => {
commit('addInterlocutor', resource)
resolve();

View File

@ -166,6 +166,7 @@ export default {
if (query.length >= 3) {
searchPersons_2({ query, options: this.options })
.then(suggested => new Promise((resolve, reject) => {
console.log('suggested', suggested);
this.loadSuggestions(suggested.results);
resolve();
}));
@ -174,6 +175,7 @@ export default {
}
},
loadSuggestions(suggested) {
console.log('suggested', suggested);
this.search.suggested = suggested;
this.search.suggested.forEach(function(item) {
item.key = this.itemKey(item);
@ -200,8 +202,7 @@ export default {
}, this);
},
itemKey(item) {
let type = item.result.type;
return type + item.result[type + '_id'];
return item.result.type + item.result.id;
}
},
}