interlocutors: actions, mutations, fetch request (wip)

This commit is contained in:
Mathieu Jaumotte 2021-05-12 17:40:23 +02:00
parent 6da8f1c107
commit eac42581a6
3 changed files with 81 additions and 17 deletions

View File

@ -1,3 +1,14 @@
const getBodyTypeId = (payload) => {
const body = {};
if (payload !== null) {
const typeId = `${payload.result.type}_id`;
body[typeId] = payload.result[typeId];
};
console.log('body', body);
return body;
}
/* /*
* Endpoint v.2 chill_api_single_accompanying_course__entity * Endpoint v.2 chill_api_single_accompanying_course__entity
* method GET/HEAD, get AccompanyingCourse Instance * method GET/HEAD, get AccompanyingCourse Instance
@ -68,12 +79,7 @@ const postParticipation = (id, person_id, method) => {
* @method string - POST or DELETE * @method string - POST or DELETE
*/ */
const postRequestor = (id, payload, method) => { const postRequestor = (id, payload, method) => {
const body = {}; const body = getBodyTypeId(payload);
if (payload !== null) {
const typeId = `${payload.result.type}_id`;
body[typeId] = payload.result[typeId];
};
console.log('body', body);
const url = `/api/1.0/person/accompanying-course/${id}/requestor.json`; const url = `/api/1.0/person/accompanying-course/${id}/requestor.json`;
return fetch(url, { return fetch(url, {
method: method, method: method,
@ -88,9 +94,34 @@ const postRequestor = (id, payload, method) => {
}); });
}; };
/*
* Endpoint v.2 chill_api_single_accompanying_course_resource,
* method POST/DELETE, add/remove a resource to the accompanyingCourse
*
* @id integer - id of accompanyingCourse
* @payload object of type person|thirdparty
* @method string - POST or DELETE
*/
const postResource = (id, payload, method) => {
const body = getBodyTypeId(payload);
const url = `/api/1.0/person/accompanying-course/${id}/resource.json`;
return fetch(url, {
method: method,
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');
});
};
export { export {
getAccompanyingCourse, getAccompanyingCourse,
patchAccompanyingCourse, patchAccompanyingCourse,
postParticipation, postParticipation,
postRequestor postRequestor,
postResource
}; };

View File

@ -37,8 +37,8 @@
</template> </template>
<script> <script>
import AddPersons from 'ChillPersonAssets/vuejs/_components/AddPersons.vue' import AddPersons from 'ChillPersonAssets/vuejs/_components/AddPersons.vue';
import InterlocutorItem from './InterlocutorItem.vue' import InterlocutorItem from './InterlocutorItem.vue';
export default { export default {
name: 'Interlocutors', name: 'Interlocutors',
@ -60,7 +60,7 @@ export default {
}, },
computed: { computed: {
accompanyingCourse() { accompanyingCourse() {
return this.$store.state.accompanyingCourse return this.$store.state.accompanyingCourse;
} }
}, },
methods: { methods: {

View File

@ -3,7 +3,8 @@ import { createStore } from 'vuex';
import { getAccompanyingCourse, import { getAccompanyingCourse,
patchAccompanyingCourse, patchAccompanyingCourse,
postParticipation, postParticipation,
postRequestor } from '../api'; postRequestor,
postResource } from '../api';
const debug = process.env.NODE_ENV !== 'production'; const debug = process.env.NODE_ENV !== 'production';
const id = window.accompanyingCourseId; const id = window.accompanyingCourseId;
@ -38,21 +39,30 @@ let initPromise = getAccompanyingCourse(id)
state.accompanyingCourse.participations.push(participation); state.accompanyingCourse.participations.push(participation);
}, },
removeRequestor(state) { removeRequestor(state) {
console.log('### mutation: removeRequestor'); //console.log('### mutation: removeRequestor');
state.accompanyingCourse.requestor = null; state.accompanyingCourse.requestor = null;
}, },
addRequestor(state, requestor) { addRequestor(state, requestor) {
console.log('### mutation: addRequestor', requestor); //console.log('### mutation: addRequestor', requestor);
state.accompanyingCourse.requestor = requestor; state.accompanyingCourse.requestor = requestor;
}, },
requestorIsAnonymous(state, value) { requestorIsAnonymous(state, value) {
console.log('### mutation: requestorIsAnonymous', value); //console.log('### mutation: requestorIsAnonymous', value);
state.accompanyingCourse.requestorAnonymous = value; state.accompanyingCourse.requestorAnonymous = value;
} },
removeInterlocutor(state, interlocutor) {
console.log('### mutation: removeInterlocutor', interlocutor);
state.accompanyingCourse.resources = state.accompanyingCourse.resources.filter(resource => resource !== interlocutor);
},
addInterlocutor(state, interlocutor) {
console.log('### mutation: addInterlocutor', interlocutor);
state.accompanyingCourse.resources.push(interlocutor);
},
}, },
actions: { actions: {
removeParticipation({ commit }, payload) { removeParticipation({ commit }, payload) {
commit('removeParticipation', payload); commit('removeParticipation', payload);
// fetch DELETE request...
}, },
closeParticipation({ commit }, payload) { closeParticipation({ commit }, payload) {
console.log('## action: fetch delete participation: payload', payload); console.log('## action: fetch delete participation: payload', payload);
@ -77,7 +87,7 @@ let initPromise = getAccompanyingCourse(id)
}); });
}, },
removeRequestor({ commit }) { removeRequestor({ commit }) {
console.log('## action: fetch post requestor'); console.log('## action: fetch delete requestor');
postRequestor(id, null, 'DELETE') postRequestor(id, null, 'DELETE')
.then(requestor => new Promise((resolve, reject) => { .then(requestor => new Promise((resolve, reject) => {
commit('removeRequestor'); commit('removeRequestor');
@ -109,7 +119,30 @@ let initPromise = getAccompanyingCourse(id)
.catch((error) => { .catch((error) => {
state.errorMsg.push(error.message); state.errorMsg.push(error.message);
}); });
} },
removeInterlocutor({ commit }, payload) {
console.log('## action: fetch postInterlocutor: payload', payload);
postResource(id, payload, 'POST')
.then(resource => new Promise((resolve, reject) => {
commit('removeInterlocutor', resource)
resolve();
}))
.catch((error) => {
state.errorMsg.push(error.message);
});
},
addInterlocutor({ commit }, payload) {
console.log('## action: fetch postInterlocutor: payload', payload);
postResource(id, payload, 'POST')
.then(resource => new Promise((resolve, reject) => {
commit('addInterlocutor', resource)
resolve();
}))
.catch((error) => {
state.errorMsg.push(error.message);
});
},
} }
}); });
resolve(store); resolve(store);