/** * @function makeFetch * @param method * @param url * @param body * @returns {Promise} */ const makeFetch = (method, url, body) => { return fetch(url, { method: method, headers: { 'Content-Type': 'application/json;charset=utf-8' }, body: (body !== null) ? JSON.stringify(body) : null }) .then(response => { if (response.ok) { return response.json(); } if (response.status === 422) { return response.json(); } throw { msg: 'Error while updating AccompanyingPeriod Course.', sta: response.status, txt: response.statusText, err: new Error(), body: response.body }; }); } /** * @function getFetch * @param url * @returns {Promise} */ const getFetch = (url) => { return makeFetch('GET', url, null); } /** * @function postFetch * @param url * @param body * @returns {Promise} */ const postFetch = (url, body) => { return makeFetch('POST', url, body); } /** * @function patchFetch * @param url * @param body * @returns {Promise} */ const patchFetch = (url, body) => { return makeFetch('PATCH', url, body); } /** * @function getHouseholdByPerson * @param person * @returns {Promise} */ const getHouseholdByPerson = (person) => { //console.log('getHouseholdByPerson', person.id) if (person.current_household_id === null) { throw 'Currently the person has not household!' } return getFetch( `/api/1.0/person/household/${person.current_household_id}.json`) } /** * @function getCourseByPerson * @param person * @returns {Promise} */ const getCourseByPerson = (person) => { //console.log('getCourseByPerson', person.id) return getFetch( `/api/1.0/person/accompanying-course/by-person/${person.id}.json`) } /** * @function getRelationship * @param person * @returns {Promise} */ const getRelationship = (person) => { //console.log('getRelationship', person.id) return getFetch( `/api/1.0/relations/relationship/by-person/${person.id}.json`) } /** * @function postRelationship * @param person * @returns {Promise} */ const postRelationship = (person) => { //console.log('postRelationship', person.id) return postFetch( `/api/1.0/relations/relationship.json`, { from: { type: 'person', id: 0 }, to: { type: 'person', id: 0 }, relation: { type: 'relation', id: 0 }, reverse: bool } ) } export { getHouseholdByPerson, getCourseByPerson, getRelationship, postRelationship }