further conversions to general makeFetch and toasts

This commit is contained in:
Julie Lenaerts 2021-12-01 10:43:18 +01:00
parent fb9df1dba0
commit e94bbc1271
10 changed files with 229 additions and 283 deletions

View File

@ -10,7 +10,7 @@
<script> <script>
import AddAddress from './components/AddAddress.vue'; import AddAddress from './components/AddAddress.vue';
import { postAddressToHousehold, postAddressToPerson } from "ChillPersonAssets/vuejs/_api/AddAddress"; import { makeFetch } from 'ChillMainAssets/lib/api/apiMethods.js';
export default { export default {
name: "App", name: "App",
@ -31,24 +31,16 @@ export default {
: this.context.target.name + '_' + this.context.target.id ; : this.context.target.name + '_' + this.context.target.id ;
} }
}, },
mounted() {
//console.log('AddAddress: data context', this.context);
//console.log('AddAddress: data options', this.options);
},
methods: { methods: {
displayErrors() { displayErrors() {
return this.$refs.addAddress.errorMsg; return this.$refs.addAddress.errorMsg;
}, },
submitAddress(payload) { submitAddress(payload) {
console.log('@@@ click on Submit Address Button', payload);
// Existing address // Existing address
if (this.context.edit) { if (this.context.edit) {
// address is already linked, just finish ! // address is already linked, just finish !
this.$refs.addAddress.afterLastPaneAction({}); this.$refs.addAddress.afterLastPaneAction({});
this.$emit('addressEdited', payload); this.$emit('addressEdited', payload);
// New created address // New created address
} else { } else {
this.postAddressTo(payload); this.postAddressTo(payload);
@ -60,51 +52,39 @@ export default {
*/ */
postAddressTo(payload) { postAddressTo(payload) {
this.$emit('addressCreated', payload); this.$emit('addressCreated', payload);
let url = '';
console.log('postAddress', payload.addressId, 'To', payload.target, payload.targetId);
switch (payload.target) { switch (payload.target) {
case 'household': case 'household':
postAddressToHousehold(payload.targetId, payload.addressId) url = `/api/1.0/person/household/${payload.targetId}/address.json`
.then(address => new Promise((resolve, reject) => {
console.log('..household address', address);
this.$refs.addAddress.flag.loading = false;
this.$refs.addAddress.flag.success = true;
// finish
this.$refs.addAddress.afterLastPaneAction({ addressId: address.address_id });
resolve();
}))
.catch((error) => {
this.$refs.addAddress.errorMsg.push(error);
this.$refs.addAddress.flag.loading = false;
})
;
break; break;
case 'person': case 'person':
postAddressToPerson(payload.targetId, payload.addressId) url = `/api/1.0/person/person/${payload.targetId}/address.json`
.then(address => new Promise((resolve, reject) => {
console.log('..person address', address);
this.$refs.addAddress.flag.loading = false;
this.$refs.addAddress.flag.success = true;
// finish
this.$refs.addAddress.afterLastPaneAction({ addressId: address.address_id });
resolve();
}))
.catch((error) => {
this.$refs.addAddress.errorMsg.push(error);
this.$refs.addAddress.flag.loading = false;
})
;
break; break;
case 'thirdparty': case 'thirdparty':
console.log('TODO write postAddressToThirdparty'); // TODO write postAddressToThirdparty;
break; break;
default: default:
this.$refs.addAddress.errorMsg.push('That entity is not managed by address !'); this.$refs.addAddress.errorMsg.push('This entity is not managed by address !');
} }
const body = {
'id': payload.addressId
};
makeFetch('POST', url, body)
.then((response) => {
this.$refs.addAddress.flag.loading = false;
this.$refs.addAddress.flag.success = true;
this.$refs.addAddress.afterLastPaneAction({ addressId: response.address_id });
})
.catch(({name, violations}) => {
if (name === 'ValidationException' || name === 'AccessException') {
violations.forEach((violation) => this.$toast.open({message: violation}));
} else {
this.$toast.open({message: 'An error occurred'})
}
this.$refs.addAddress.flag.loading = false;
});
} }
} }
} }

View File

@ -229,7 +229,6 @@ import {
postAddress, postAddress,
postPostalCode, postPostalCode,
} from '../api'; } from '../api';
import { postAddressToPerson, postAddressToHousehold } from "ChillPersonAssets/vuejs/_api/AddAddress.js";
import ShowPane from './ShowPane.vue'; import ShowPane from './ShowPane.vue';
import SuggestPane from './SuggestPane.vue'; import SuggestPane from './SuggestPane.vue';
import EditPane from './EditPane.vue'; import EditPane from './EditPane.vue';
@ -355,14 +354,7 @@ export default {
} }
}, },
mounted() { mounted() {
//console.log('validFrom', this.validFrom);
//console.log('validTo', this.validTo);
//console.log('useDatePane', this.useDatePane);
// console.log('Mounted now !');
if (this.context.edit) { if (this.context.edit) {
// console.log('getInitialAddress', this.context.addressId);
this.getInitialAddress(this.context.addressId); this.getInitialAddress(this.context.addressId);
} }
this.openShowPane(); this.openShowPane();

View File

@ -5,8 +5,6 @@ import { getAccompanyingCourse,
getReferrersSuggested, getReferrersSuggested,
getUsers, getUsers,
} from '../api'; } from '../api';
import { patchPerson } from "ChillPersonAssets/vuejs/_api/OnTheFly";
import { patchThirdparty } from "ChillThirdPartyAssets/vuejs/_api/OnTheFly";
import { makeFetch } from 'ChillMainAssets/lib/api/apiMethods'; import { makeFetch } from 'ChillMainAssets/lib/api/apiMethods';
@ -355,15 +353,14 @@ let initPromise = Promise.all([scopesPromise, accompanyingCoursePromise])
}, },
/** /**
* On The Fly * On The Fly
* ? should be into the dedicated component, no ? JF
* I think it's better to leave it here, because this action is dispatched in 3 different components,
* so lots of code repitition if we would put this in the components themselves.
*/ */
patchOnTheFly({ commit }, payload) { patchOnTheFly({ commit }, payload) {
// TODO should be into the dedicated component, no ? JF
// console.log('## action: patch OnTheFly', payload);
let body = { type: payload.type }; let body = { type: payload.type };
if (payload.type === 'person') { if (payload.type === 'person') {
const id = payload.data.id;
const url = `/api/1.0/person/person/${id}.json`;
body.firstName = payload.data.firstName; body.firstName = payload.data.firstName;
body.lastName = payload.data.lastName; body.lastName = payload.data.lastName;
if (payload.data.birthdate !== null) { body.birthdate = payload.data.birthdate; } if (payload.data.birthdate !== null) { body.birthdate = payload.data.birthdate; }
@ -371,50 +368,27 @@ let initPromise = Promise.all([scopesPromise, accompanyingCoursePromise])
body.mobilenumber = payload.data.mobilenumber; body.mobilenumber = payload.data.mobilenumber;
body.gender = payload.data.gender; body.gender = payload.data.gender;
makeFetch('PATCH', url, body) const id = payload.data.id;
.then((response) => { const url = `/api/1.0/person/person/${id}.json`;
commit('updatePerson', {target: payload.target, person: response}); const mutation = 'updatePerson'
}) } else if (payload.type === 'thirdparty') {
.catch((error) => {
commit('catchError', error);
throw error;
})
// console.log('id', payload.data.id, 'and body', body);
// patchPerson(payload.data.id, body)
// .then(person => new Promise((resolve, reject) => {
// // console.log('patch person', person);
// commit('updatePerson', { target: payload.target, person: person });
// resolve();
// }));
}
else if (payload.type === 'thirdparty') {
body.name = payload.data.text; body.name = payload.data.text;
body.email = payload.data.email; body.email = payload.data.email;
body.telephone = payload.data.phonenumber; body.telephone = payload.data.phonenumber;
body.address = { id: payload.data.address.address_id }; body.address = { id: payload.data.address.address_id };
// console.log('id', payload.data.id, 'and body', body);
// patchThirdparty(payload.data.id, body)
// .then(thirdparty => new Promise((resolve, reject) => {
// console.log('patch thirdparty', thirdparty);
// commit('updateThirdparty', { target: payload.target, thirdparty: thirdparty });
// resolve();
// }));
const id = payload.data.id; const id = payload.data.id;
const url = `/api/1.0/thirdparty/thirdparty/${id}.json`; const url = `/api/1.0/thirdparty/thirdparty/${id}.json`;
const mutation = 'updateThirdparty'
}
makeFetch('PATCH', url, body) makeFetch('PATCH', url, body)
.then((response) => { .then((response) => {
commit('updateThirdparty', {target: payload.target, thirdparty: response}); commit(mutation, {target: payload.target, thirdparty: response});
}) })
.catch((error) => { .catch((error) => {
commit('catchError', error); commit('catchError', error);
throw error; throw error;
}) })
}
}, },
/** /**
* Update accompanying course intensity/emergency/confidentiality * Update accompanying course intensity/emergency/confidentiality
@ -509,8 +483,7 @@ let initPromise = Promise.all([scopesPromise, accompanyingCoursePromise])
let checkedScopesIds = scopes.map(scope => scope.id); let checkedScopesIds = scopes.map(scope => scope.id);
let removedScopesIds = currentServerScopesIds.filter(id => !checkedScopesIds.includes(id)); let removedScopesIds = currentServerScopesIds.filter(id => !checkedScopesIds.includes(id));
let addedScopesIds = checkedScopesIds.filter(id => !currentServerScopesIds.includes(id)); let addedScopesIds = checkedScopesIds.filter(id => !currentServerScopesIds.includes(id));
let lengthAfterOperation = currentServerScopesIds.length + addedScopesIds.length let lengthAfterOperation = currentServerScopesIds.length + addedScopesIds.length - removedScopesIds.length;
- removedScopesIds.length;
return dispatch('updateScopes', { return dispatch('updateScopes', {
addedScopesIds, removedScopesIds addedScopesIds, removedScopesIds

View File

@ -1,51 +1,51 @@
/* // /*
* Endpoint chill_api_single_person_address // * Endpoint chill_api_single_person_address
* method POST, post Person instance // * method POST, post Person instance
* // *
* @id integer - id of Person // * @id integer - id of Person
* @body Object - dictionary with changes to post // * @body Object - dictionary with changes to post
*/ // */
const postAddressToPerson = (personId, addressId) => { // const postAddressToPerson = (personId, addressId) => {
//console.log(personId); // //console.log(personId);
//console.log(addressId); // //console.log(addressId);
const body = { // const body = {
'id': addressId // 'id': addressId
}; // };
const url = `/api/1.0/person/person/${personId}/address.json` // const url = `/api/1.0/person/person/${personId}/address.json`
return fetch(url, { // return fetch(url, {
method: 'POST', // method: 'POST',
headers: {'Content-Type': 'application/json;charset=utf-8'}, // headers: {'Content-Type': 'application/json;charset=utf-8'},
body: JSON.stringify(body) // body: JSON.stringify(body)
}) // })
.then(response => { // .then(response => {
if (response.ok) { return response.json(); } // if (response.ok) { return response.json(); }
throw Error('Error with request resource response'); // throw Error('Error with request resource response');
}); // });
}; // };
/* // /*
* Endpoint household // * Endpoint household
* method POST, post Household instance // * method POST, post Household instance
* // *
* @id integer - id of household // * @id integer - id of household
* @body Object - dictionary with changes to post // * @body Object - dictionary with changes to post
*/ // */
const postAddressToHousehold = (householdId, addressId) => { // const postAddressToHousehold = (householdId, addressId) => {
const body = { // const body = {
'id': addressId // 'id': addressId
}; // };
const url = `/api/1.0/person/household/${householdId}/address.json` // const url = `/api/1.0/person/household/${householdId}/address.json`
return fetch(url, { // return fetch(url, {
method: 'POST', // method: 'POST',
headers: {'Content-Type': 'application/json;charset=utf-8'}, // headers: {'Content-Type': 'application/json;charset=utf-8'},
body: JSON.stringify(body) // body: JSON.stringify(body)
}) // })
.then(response => { // .then(response => {
if (response.ok) { return response.json(); } // if (response.ok) { return response.json(); }
throw Error('Error with request resource response'); // throw Error('Error with request resource response');
}); // });
}; // };
export { postAddressToPerson, postAddressToHousehold }; // export { postAddressToPerson, postAddressToHousehold };

View File

@ -1,53 +1,53 @@
/* /*
* GET a person by id * GET a person by id
*/ */
const getPerson = (id) => { // const getPerson = (id) => {
const url = `/api/1.0/person/person/${id}.json`; // const url = `/api/1.0/person/person/${id}.json`;
return fetch(url) // return fetch(url)
.then(response => { // .then(response => {
if (response.ok) { return response.json(); } // if (response.ok) { return response.json(); }
throw Error('Error with request resource response'); // throw Error('Error with request resource response');
}); // });
}; // };
/* /*
* POST a new person * POST a new person
*/ */
const postPerson = (body) => { // const postPerson = (body) => {
const url = `/api/1.0/person/person.json`; // const url = `/api/1.0/person/person.json`;
return fetch(url, { // return fetch(url, {
method: 'POST', // method: 'POST',
headers: { // headers: {
'Content-Type': 'application/json;charset=utf-8' // 'Content-Type': 'application/json;charset=utf-8'
}, // },
body: JSON.stringify(body) // body: JSON.stringify(body)
}) // })
.then(response => { // .then(response => {
if (response.ok) { return response.json(); } // if (response.ok) { return response.json(); }
throw Error('Error with request resource response'); // throw Error('Error with request resource response');
}); // });
}; // };
/* /*
* PATCH an existing person * PATCH an existing person
*/ */
const patchPerson = (id, body) => { // const patchPerson = (id, body) => {
const url = `/api/1.0/person/person/${id}.json`; // const url = `/api/1.0/person/person/${id}.json`;
return fetch(url, { // return fetch(url, {
method: 'PATCH', // method: 'PATCH',
headers: { // headers: {
'Content-Type': 'application/json;charset=utf-8' // 'Content-Type': 'application/json;charset=utf-8'
}, // },
body: JSON.stringify(body) // body: JSON.stringify(body)
}) // })
.then(response => { // .then(response => {
if (response.ok) { return response.json(); } // if (response.ok) { return response.json(); }
throw Error('Error with request resource response'); // throw Error('Error with request resource response');
}); // });
}; // };
export { export {
getPerson, // getPerson,
postPerson, // postPerson,
patchPerson // patchPerson
}; };

View File

@ -257,16 +257,11 @@ export default {
saveFormOnTheFly({ type, data }) { saveFormOnTheFly({ type, data }) {
// console.log('saveFormOnTheFly from addPersons, type', type, ', data', data); // console.log('saveFormOnTheFly from addPersons, type', type, ', data', data);
if (type === 'person') { if (type === 'person') {
// console.log('type person with', data); const url = `/api/1.0/person/person.json`;
postPerson(data)
.then(person => new Promise((resolve, reject) => {
// console.log('onthefly create: post person', person);
this.newPriorSuggestion(person);
resolve();
}));
} }
else if (type === 'thirdparty') { else if (type === 'thirdparty') {
const url = `/api/1.0/thirdparty/thirdparty.json`; const url = `/api/1.0/thirdparty/thirdparty.json`;
}
makeFetch('POST', url, data) makeFetch('POST', url, data)
.then((response) => { .then((response) => {
this.newPriorSuggestion(response); this.newPriorSuggestion(response);
@ -279,7 +274,6 @@ export default {
} }
}); });
} }
}
}, },
} }
</script> </script>

View File

@ -76,6 +76,7 @@
<script> <script>
import { getPerson } from '../../_api/OnTheFly'; import { getPerson } from '../../_api/OnTheFly';
import { makeFetch } from 'ChillMainAssets/lib/api/apiMethods';
import PersonRenderBox from '../Entity/PersonRenderBox.vue'; import PersonRenderBox from '../Entity/PersonRenderBox.vue';
export default { export default {
@ -156,12 +157,18 @@ export default {
}, },
methods: { methods: {
loadData() { loadData() {
getPerson(this.id) const url = `/api/1.0/person/person/${this.id}.json`;
.then(person => new Promise((resolve, reject) => { makeFetch('GET', url)
this.person = person; .then((response) => {
console.log('get person', this.person); this.person = response;
resolve(); })
})); .catch(({name, violations}) => {
if (name === 'ValidationException' || name === 'AccessException') {
violations.forEach((violation) => this.$toast.open({message: violation}));
} else {
this.$toast.open({message: 'An error occurred'})
}
});
} }
} }
} }