rdv: add dynamic event sources in fullcalendar (WIP)

This commit is contained in:
nobohan 2021-08-17 21:06:32 +02:00
parent 07f086dd61
commit e499ebdf8b
3 changed files with 230 additions and 118 deletions

View File

@ -1,20 +1,82 @@
<template> <template>
<concerned-groups></concerned-groups> <concerned-groups></concerned-groups>
<calendar-user-selector></calendar-user-selector> <calendar-user-selector
<calendar-event></calendar-event> v-bind:users="users"
v-bind:calendarEvents="calendarEvents"
v-bind:updateEventsSource="updateEventsSource">
</calendar-user-selector>
<calendar-event
v-bind:calendarEvents="calendarEvents">
</calendar-event>
</template> </template>
<script> <script>
import ConcernedGroups from 'ChillActivityAssets/vuejs/Activity/components/ConcernedGroups.vue'; import ConcernedGroups from 'ChillActivityAssets/vuejs/Activity/components/ConcernedGroups.vue';
import CalendarEvent from '../_components/CalendarEvent/CalendarEvent.vue'; import CalendarEvent from '../_components/CalendarEvent/CalendarEvent.vue';
import CalendarUserSelector from '../_components/CalendarUserSelector/CalendarUserSelector.vue'; import CalendarUserSelector from '../_components/CalendarUserSelector/CalendarUserSelector.vue';
import { fetchCalendarRanges } from '../_components/CalendarEvent/js/api';
export default { export default {
name: "App", name: "App",
components: { components: {
ConcernedGroups, ConcernedGroups,
CalendarEvent, CalendarEvent,
CalendarUserSelector, CalendarUserSelector,
} },
data() {
return {
errorMsg: [],
users: {
loaded: [],
selected: []
},
calendarEvents: {
loaded: [],
selected: []
}
}
},
methods: {
updateEventsSource() {
console.log('updateEventsSource')
//TODO: how to update source events??? use https://fullcalendar.io/docs/Calendar-addEventSource?
//TODO
// fetchCalendarRanges().then(calendarRanges => new Promise((resolve, reject) => {
// let calendarEvents = [];
// calendarRanges.results.forEach(i => {
// calendarEvents.push({
// })
// });
// this.calendarEvents = calendarEvents;
// resolve()
// }))
// .catch((error) => {
// this.errorMsg.push(error.message);
// });
// this.calendarEvents = [
// {
// events: [
// {
// title: 'Event1',
// start: '2021-08-04T12:30:00'
// },
// {
// title: 'Event2',
// start: '2021-08-05T12:30:00'
// }
// ],
// color: 'yellow', // an option!
// textColor: 'black' // an option!
// }
// ]
}
},
// mounted() {
// this.init();
// }
} }
</script> </script>

View File

@ -18,23 +18,23 @@ export default {
components: { components: {
FullCalendar FullCalendar
}, },
props: ['calendarEvents'],
data() { data() {
return { return {
calendarRanges: [], //calendarRanges: [],
calendarOptions: { calendarOptions: {
plugins: [ dayGridPlugin, interactionPlugin, timeGridPlugin ], plugins: [ dayGridPlugin, interactionPlugin, timeGridPlugin ],
initialView: 'timeGridWeek', initialView: 'timeGridWeek',
initialEvents: window.startDate !== undefined ? // initialEvents: window.startDate !== undefined ?
[ // [
{ // {
id: 1, // id: 1,
start: window.startDate, // start: window.startDate,
end: window.endDate // end: window.endDate
} // }
] : [], // ] : [],
initialDate: window.startDate !== undefined ? window.startDate : new Date(), initialDate: window.startDate !== undefined ? window.startDate : new Date(),
//events: this.displayEvents(), eventSources: this.calendarEvents,
eventSources: this.getEventsSources(),
selectable: true, selectable: true,
select: this.onDateSelect, select: this.onDateSelect,
eventChange: this.onEventChange, eventChange: this.onEventChange,
@ -47,103 +47,108 @@ export default {
right: 'dayGridMonth,timeGridWeek,timeGridDay' right: 'dayGridMonth,timeGridWeek,timeGridDay'
}, },
}, },
errorMsg: {}, errorMsg: [],
} }
}, },
methods: { methods: {
getCalendarRanges(userId) { init() {
console.log('get Calendar Ranges'); this.getCalendarRanges()
return fetchCalendarRanges().then(calendarRanges => new Promise((resolve, reject) => { console.log(window.startDate)
console.log(calendarRanges);
this.calendarRanges = calendarRanges.results;
let source = { //TODO transform the results above into a object as following
events: [
{
title: 'Event1',
start: '2021-07-04T09:30:00'
},
{
title: 'Event2',
start: '2021-07-05T09:30:00'
}
],
color: 'red', // an option!
textColor: 'white' // an option!
};
this.calendarRanges = source;
resolve()
}))
.catch((error) => {
this.errorMsg.push(error.message);
});
}, },
getEventsSources() { // soit on appelle cette function plus tard, soit on gère différement l'asynchroniété getCalendarRanges() {
let userId = 1; // console.log('getCalendarRanges')
let arr = [ // fetchCalendarRanges().then(calendarRanges => new Promise((resolve, reject) => {
this.displayEventSource1(), // // let calendarEvents = [];
this.displayEventSource2(),
// // this.users.loaded.forEach(u => {
fetchCalendarRanges().then(response => { // // let arr = calendarRanges.results.filter(i => i.user.id = u.id);
console.log(response) // // console.log(arr)
return response // // calendarEvents.push({
// if (response.ok) { return response.results } // // events: arr,
}) // // color: u.color
//this.calendarRanges // // })
]; // // })
console.log(arr);
return arr; // // this.calendarEvents = [
}, // // {
displayEvents() { //TODO WIP // // events: [
return [ // // {
{ // // title: 'Event1',
title : 'event1', // // start: '2021-08-04T12:30:00'
start : '2021-07-04T10:30:00' // // },
}, // // {
{ // // title: 'Event2',
title : 'event2', // // start: '2021-08-05T12:30:00'
start : '2021-07-05', // // }
end : '2021-07-07' // // ],
}, // // color: 'yellow', // an option!
{ // // textColor: 'black' // an option!
title : 'event3', // // }
start : '2021-07-09T12:30:00', // // ]
allDay : false // will make the time show // // console.log(calendarEvents)
} // // this.calendarEvents = calendarEvents;
] // resolve()
//return this.calendarRanges // undefined as not fetched when evaluated // }))
}, // .catch((error) => {
displayEventSource1(){ // TODO replace this with the fetch function depending on the User calendar ranges // this.errorMsg.push(error.message);
return { // });
events: [
{
title: 'Event1',
start: '2021-07-04T12:30:00'
},
{
title: 'Event2',
start: '2021-07-05T12:30:00'
}
],
color: 'yellow', // an option!
textColor: 'black' // an option!
}
},
displayEventSource2(){
return {
events: [
{
title: 'Event1',
start: '2021-08-04T12:30:00'
},
{
title: 'Event2',
start: '2021-08-05T12:30:00'
}
],
color: 'green', // an option!
textColor: 'red' // an option!
}
}, },
// getCalendarRanges(userId) {
// console.log('get Calendar Ranges');
// return fetchCalendarRanges().then(calendarRanges => new Promise((resolve, reject) => {
// console.log(calendarRanges);
// this.calendarRanges = calendarRanges.results;
// let source = { //TODO transform the results above into a object as following
// events: [
// {
// title: 'Event1',
// start: '2021-07-04T09:30:00'
// },
// {
// title: 'Event2',
// start: '2021-07-05T09:30:00'
// }
// ],
// color: 'red', // an option!
// textColor: 'white' // an option!
// };
// this.calendarRanges = source;
// resolve()
// }))
// .catch((error) => {
// this.errorMsg.push(error.message);
// });
// },
// getEventsSources() { // soit on appelle cette function plus tard, soit on gère différement l'asynchroniété
// let arr = [
// this.displayEventSource1(),
// fetchCalendarRanges().then(response => {
// console.log(response)
// return response
// // if (response.ok) { return response.results }
// })
// //this.calendarRanges
// ];
// console.log(arr);
// return arr;
// },
// displayEventSource1(){ // TODO replace this with the fetch function depending on the User calendar ranges
// return {
// events: [
// {
// title: 'Event1',
// start: '2021-07-04T12:30:00'
// },
// {
// title: 'Event2',
// start: '2021-07-05T12:30:00'
// }
// ],
// color: 'yellow', // an option!
// textColor: 'black' // an option!
// }
// },
onDateSelect(payload) { onDateSelect(payload) {
this.$store.dispatch('createEvent', payload); this.$store.dispatch('createEvent', payload);
}, },
@ -152,7 +157,7 @@ export default {
} }
}, },
mounted() { mounted() {
this.getCalendarRanges() this.init()
} }
} }
</script> </script>

View File

@ -13,7 +13,7 @@
:close-on-select="false" :close-on-select="false"
:allow-empty="true" :allow-empty="true"
:model-value="value" :model-value="value"
@update:model-value="updateUsers" @update:model-value="selectUsers"
:options="options"> :options="options">
</VueMultiselect> </VueMultiselect>
</div> </div>
@ -23,9 +23,12 @@
import { fetchCalendarRanges } from './js/api' import { fetchCalendarRanges } from './js/api'
import VueMultiselect from 'vue-multiselect'; import VueMultiselect from 'vue-multiselect';
const COLORS = ['red', 'green', 'yellow', 'blue', 'orange', 'white', 'grey'];
export default { export default {
name: 'CalendarUserSelector', name: 'CalendarUserSelector',
components: { VueMultiselect }, components: { VueMultiselect },
props: ['users', 'updateEventsSource', 'calendarEvents'],
data() { data() {
return { return {
errorMsg: [], errorMsg: [],
@ -39,16 +42,57 @@ export default {
}, },
getUniqueUserCalendarRanges() { getUniqueUserCalendarRanges() {
fetchCalendarRanges().then(calendarRanges => new Promise((resolve, reject) => { fetchCalendarRanges().then(calendarRanges => new Promise((resolve, reject) => {
let results = calendarRanges.results;
let users = []; let users = [];
calendarRanges.results.forEach(i => {
results.forEach(i => {
if (!(users.some(j => i.user.id === j.id))){ if (!(users.some(j => i.user.id === j.id))){
users.push({ users.push({
id: i.user.id, id: i.user.id,
username: i.user.username username: i.user.username,
color: COLORS[users.length] //TODO manage case where indice is larger than COLORS; Do something recursive with a max. value
}) })
} }
}); });
let calendarEvents = [];
users.forEach(u => {
let arr = results.filter(i => i.user.id === u.id).map(i =>
({
start: i.startDate.datetime,
end: i.endDate.datetime
})
);
console.log(arr);
calendarEvents.push({
events: arr,
color: u.color
})
})
console.log(users)
this.users.loaded = users;
this.options = users; this.options = users;
console.log(calendarEvents)
this.calendarEvents.loaded = calendarEvents;
// // TO TEST
// this.calendarEvents.loaded = [{events: [
// {
// title: 'Event1',
// start: '2021-08-04T12:30:00'
// },
// {
// title: 'Event2',
// start: '2021-08-05T12:30:00'
// }
// ],
// color: 'yellow', // an option!
// textColor: 'black' // an option!
// }];
resolve() resolve()
})) }))
.catch((error) => { .catch((error) => {
@ -58,8 +102,9 @@ export default {
transName(value) { transName(value) {
return `${value.username}`; return `${value.username}`;
}, },
updateUsers(value) { selectUsers(value) {
console.log(value) this.users.selected = value;
this.updateEventsSource();
} }
}, },
mounted() { mounted() {