mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-12 13:24:25 +00:00
fetch postParticipation request, catch fetch error, store mutation+action
This commit is contained in:
parent
4900c81c11
commit
35e6d36ce0
@ -1,31 +1,36 @@
|
|||||||
const
|
const
|
||||||
locale = 'fr',
|
locale = 'fr',
|
||||||
format = 'json',
|
format = 'json'
|
||||||
accompanying_period_id = () => window.accompanyingCourseId
|
, accompanying_period_id = window.accompanyingCourseId //tmp
|
||||||
;
|
;
|
||||||
|
|
||||||
// 1. chill_person_accompanying_course_api_show (GET)
|
// 1. chill_person_accompanying_course_api_show (GET)
|
||||||
let getAccompanyingCourse = (accompanying_period_id) => {
|
let getAccompanyingCourse = (accompanying_period_id___) => { //tmp
|
||||||
const url = `/${locale}/person/api/1.0/accompanying-course/${accompanying_period_id}/show.${format}`;
|
const url = `/${locale}/person/api/1.0/accompanying-course/${accompanying_period_id}/show.${format}`;
|
||||||
return fetch(url)
|
return fetch(url)
|
||||||
.then(response => response.json());
|
.then(response => {
|
||||||
|
if (response.ok) { return response.json(); }
|
||||||
|
throw Error('Error with request resource response');
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// 2. chill_person_accompanying_course_api_add_participation (POST)
|
// 2. chill_person_accompanying_course_api_add_participation (POST)
|
||||||
let postParticipation = (accompanying_period_id, participation) => {
|
let postParticipation = (accompanying_period_id, person_id) => {
|
||||||
const url = `/${locale}/person/api/1.0/accompanying-course/${accompanying_period_id}/participation.${format}`
|
const url = `/${locale}/person/api/1.0/accompanying-course/${accompanying_period_id}/participation.${format}`
|
||||||
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(participation)
|
body: JSON.stringify({id: person_id})
|
||||||
})
|
})
|
||||||
.then(response => response.json());
|
.then(response => {
|
||||||
|
if (response.ok) { return response.json(); }
|
||||||
|
throw Error('Error with request resource response');
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export { getAccompanyingCourse, postParticipation };
|
export {
|
||||||
|
getAccompanyingCourse,
|
||||||
/// TODO
|
postParticipation
|
||||||
// * cfr. promise.all() pour plusieurs promesses
|
};
|
||||||
// * catch throw sur le dernier then pour capturer l'erreur
|
|
||||||
|
@ -54,7 +54,7 @@ export default {
|
|||||||
methods: {
|
methods: {
|
||||||
addPerson() {
|
addPerson() {
|
||||||
console.log('[wip] opening add persons modal');
|
console.log('[wip] opening add persons modal');
|
||||||
this.$store.commit('addParticipation', {
|
this.$store.dispatch('addParticipation', {
|
||||||
id: SimpsonId++,
|
id: SimpsonId++,
|
||||||
person: { firstName: "Lisa", lastName: "Simpson", id: SimpsonId },
|
person: { firstName: "Lisa", lastName: "Simpson", id: SimpsonId },
|
||||||
startDate: { datetime: "1975-09-15T00:00:00+0100" },
|
startDate: { datetime: "1975-09-15T00:00:00+0100" },
|
||||||
@ -62,7 +62,7 @@ export default {
|
|||||||
})
|
})
|
||||||
},
|
},
|
||||||
removePerson(item) {
|
removePerson(item) {
|
||||||
this.$store.commit('removeParticipation', item)
|
this.$store.dispatch('removeParticipation', item)
|
||||||
},
|
},
|
||||||
savePersons() {
|
savePersons() {
|
||||||
console.log('[wip] saving persons');
|
console.log('[wip] saving persons');
|
||||||
|
@ -4,49 +4,46 @@ import { getAccompanyingCourse, postParticipation } from '../api/accompanyingCou
|
|||||||
|
|
||||||
const debug = process.env.NODE_ENV !== 'production';
|
const debug = process.env.NODE_ENV !== 'production';
|
||||||
|
|
||||||
const id = window.accompanyingCourseId;
|
const id = window.accompanyingCourseId; //tmp
|
||||||
|
|
||||||
let getDataPromise = getAccompanyingCourse(id)
|
let getDataPromise = getAccompanyingCourse(id)
|
||||||
.then(accompanying_course => new Promise((resolve, reject) => {
|
.then(accompanying_course => new Promise((resolve, reject) => {
|
||||||
|
|
||||||
const store = createStore({
|
const store = createStore({
|
||||||
strict: debug,
|
strict: debug,
|
||||||
state: {
|
state: {
|
||||||
accompanying_course: accompanying_course
|
accompanying_course: accompanying_course,
|
||||||
|
errorMsg: []
|
||||||
},
|
},
|
||||||
getters: {
|
getters: {
|
||||||
},
|
},
|
||||||
mutations: {
|
mutations: {
|
||||||
removeParticipation(state, item) {
|
removeParticipation(state, item) {
|
||||||
//console.log('remove item', item.id);
|
console.log('remove item', item.id);
|
||||||
state.accompanying_course.participations = state.accompanying_course.participations.filter(
|
state.accompanying_course.participations = state.accompanying_course.participations.filter(
|
||||||
participation => participation !== item
|
participation => participation !== item
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
addParticipation(state, item) {
|
addParticipation(state, item) {
|
||||||
//console.log('add new item');
|
console.log('add new item');
|
||||||
state.accompanying_course.participations.push(item)
|
state.accompanying_course.participations.push(item)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
actions: {
|
actions: {
|
||||||
|
addParticipation({ commit }, payload) {
|
||||||
|
console.log('payload', payload);
|
||||||
|
commit('addParticipation', payload);
|
||||||
|
|
||||||
|
postParticipation(id, payload.id).catch((error) => {
|
||||||
|
commit('removeParticipation', payload)
|
||||||
|
state.errorMsg.push(error.message);
|
||||||
|
})
|
||||||
|
// result action ??
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
console.log('store object', store.state.accompanying_course.id);
|
console.log('store object', store.state.accompanying_course.id);
|
||||||
resolve(store);
|
resolve(store);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
let postDataPromise = postParticipation(id)
|
|
||||||
.then(participation => new Promise((resolve, reject) => {
|
|
||||||
// get store
|
|
||||||
// use store.actions to post (async way)
|
|
||||||
console.log(store);
|
|
||||||
resolve(store);
|
|
||||||
}));
|
|
||||||
|
|
||||||
/*
|
|
||||||
let allPromises = Promise.all([getDataPromise, postDataPromise])
|
|
||||||
.then((values) => {
|
|
||||||
console.log('all promises values', values);
|
|
||||||
});
|
|
||||||
*/
|
|
||||||
|
|
||||||
export { getDataPromise };
|
export { getDataPromise };
|
||||||
|
@ -5,15 +5,13 @@
|
|||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
|
|
||||||
<h1>{{ block('title') }}</h1>
|
<h1>{{ block('title') }}</h1>
|
||||||
|
|
||||||
<div id="accompanying-course"></div>
|
<div id="accompanying-course"></div>
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
{{ encore_entry_script_tags('accompanying_course') }}
|
{% block js %}
|
||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
window.accompanyingCourseId = {{ accompanyingCourse.id|e('js') }};
|
window.accompanyingCourseId = {{ accompanyingCourse.id|e('js') }};
|
||||||
</script>
|
</script>
|
||||||
|
{{ encore_entry_script_tags('accompanying_course') }}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user