redesign calendar api for improving loading performance
This commit is contained in:
parent
78d542cbac
commit
d2b2cea846
5 changed files with 76 additions and 99 deletions
|
|
@ -12,7 +12,7 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.calendar-strip__days.progress {
|
/* .calendar-strip__days.progress {
|
||||||
cursor: progress !important;
|
cursor: progress !important;
|
||||||
}
|
}
|
||||||
.calendar-strip__days.progress > li {
|
.calendar-strip__days.progress > li {
|
||||||
|
|
@ -20,7 +20,7 @@
|
||||||
}
|
}
|
||||||
.calendar-strip__days.progress button {
|
.calendar-strip__days.progress button {
|
||||||
cursor: progress !important;
|
cursor: progress !important;
|
||||||
}
|
} */
|
||||||
|
|
||||||
.grid {
|
.grid {
|
||||||
--column-gap: calc(var(--space-m) * 1.1);
|
--column-gap: calc(var(--space-m) * 1.1);
|
||||||
|
|
|
||||||
|
|
@ -149,18 +149,14 @@ function getId(eventData) {
|
||||||
function createEmptyCalendar() {
|
function createEmptyCalendar() {
|
||||||
const calendar = {};
|
const calendar = {};
|
||||||
|
|
||||||
for (let month = 9; month < 19; month++) {
|
for (let month = 9; month < 21; month++) {
|
||||||
const normalizedMonth = ((month - 1) % 12) + 1;
|
const normalizedMonth = ((month - 1) % 12) + 1;
|
||||||
|
|
||||||
const daysInMonth = dayjs()
|
const daysInMonth = dayjs()
|
||||||
.month(normalizedMonth - 1)
|
.month(normalizedMonth - 1)
|
||||||
.daysInMonth();
|
.daysInMonth();
|
||||||
|
|
||||||
calendar[normalizedMonth] = {
|
calendar[normalizedMonth] = {};
|
||||||
name: dayjs()
|
|
||||||
.month(normalizedMonth - 1)
|
|
||||||
.format("MMMM"),
|
|
||||||
};
|
|
||||||
|
|
||||||
for (let day = 1; day <= daysInMonth; day++) {
|
for (let day = 1; day <= daysInMonth; day++) {
|
||||||
calendar[normalizedMonth][day] = null;
|
calendar[normalizedMonth][day] = null;
|
||||||
|
|
@ -170,7 +166,7 @@ function createEmptyCalendar() {
|
||||||
return calendar;
|
return calendar;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getMergedDates(monthNumb, dates) {
|
async function getCurrentSeasonCalendar(monthNumb, dates) {
|
||||||
const calendar = createEmptyCalendar();
|
const calendar = createEmptyCalendar();
|
||||||
|
|
||||||
const requestParams = {
|
const requestParams = {
|
||||||
|
|
@ -187,7 +183,6 @@ async function getMergedDates(monthNumb, dates) {
|
||||||
requestOptions
|
requestOptions
|
||||||
);
|
);
|
||||||
const json = await response.json();
|
const json = await response.json();
|
||||||
console.log("calendar", json);
|
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -95,6 +95,13 @@ return [
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return json_encode($calendar);
|
$jsonCalendar = json_encode($calendar);
|
||||||
|
|
||||||
|
$seasonDirPath = __DIR__ . '/../../../content/' . $currentSeason->diruri() . '/data.json';
|
||||||
|
$dataFile = fopen($seasonDirPath, "w");
|
||||||
|
fwrite($dataFile, $jsonCalendar);
|
||||||
|
fclose($dataFile);
|
||||||
|
|
||||||
|
return $jsonCalendar;
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -2,114 +2,90 @@
|
||||||
class="calendar-strip"
|
class="calendar-strip"
|
||||||
@mouseleave="open = false"
|
@mouseleave="open = false"
|
||||||
x-data="{
|
x-data="{
|
||||||
monthNumb: dayjs().month(),
|
calendar: createEmptyCalendar(),
|
||||||
weekIndex: (() => {
|
monthNumb: dayjs().month() + 1,
|
||||||
dayjs.extend(window.dayjs_plugin_weekOfYear);
|
token: '<?= $site->mapadoToken() ?>',
|
||||||
|
contractId: '<?= $site->mapadoContractId() ?>',
|
||||||
const date = dayjs();
|
targetSessions: [],
|
||||||
|
open: false,
|
||||||
const firstDayOfMonth = dayjs(date).startOf('month');
|
today: dayjs().format('DD-MM-YYYY'),
|
||||||
|
|
||||||
const currentWeek = date.week();
|
get monthName() {
|
||||||
const firstWeekOfMonth = firstDayOfMonth.week();
|
return dayjs().month(this.monthNumb - 1).format('MMMM');
|
||||||
|
},
|
||||||
const weekIndex = currentWeek - firstWeekOfMonth - 1;
|
|
||||||
|
|
||||||
console.log(`currentWeek: ${currentWeek}, firstWeekOfMonth: ${firstWeekOfMonth}, weekIndex: ${weekIndex}`);
|
get currentMonth() {
|
||||||
|
return this.calendar[this.monthNumb]
|
||||||
// Retourner l'index de la semaine
|
},
|
||||||
return weekIndex;
|
|
||||||
})(),
|
|
||||||
token: '<?= $site->mapadoToken() ?>',
|
|
||||||
contractId: '<?= $site->mapadoContractId() ?>',
|
|
||||||
dates: [],
|
|
||||||
targetSessions: [],
|
|
||||||
open: false,
|
|
||||||
today: dayjs().format('DD-MM-YYYY'),
|
|
||||||
|
|
||||||
get monthName() {
|
async updateDates() {
|
||||||
return dayjs().month(this.monthNumb).format('MMMM');
|
|
||||||
},
|
|
||||||
|
|
||||||
get currentWeek() {
|
fetch('/<?= page('programme')->children()->first()->uri() ?>/data.json')
|
||||||
const dates = JSON.parse(JSON.stringify(this.dates));
|
.then(res => res.json())
|
||||||
const start = this.weekIndex * 7;
|
.then(json => {
|
||||||
const end = start + 7;
|
console.log('Saved calendar : ', json)
|
||||||
|
this.calendar = json
|
||||||
|
})
|
||||||
|
.catch(error => console.log('No saved calendar.'))
|
||||||
|
|
||||||
const allKeys = Object.keys(dates);
|
const daysNode = document.querySelector('.calendar-strip__days');
|
||||||
|
daysNode.classList.toggle('progress');
|
||||||
|
|
||||||
|
this.calendar = await getCurrentSeasonCalendar();
|
||||||
|
|
||||||
|
console.log('Updated calendar for current season :', this.calendar);
|
||||||
|
daysNode.classList.toggle('progress');
|
||||||
|
},
|
||||||
|
|
||||||
const slicedKeys = allKeys.slice(start, end);
|
handleDayHover() {
|
||||||
|
if (!this.date) return
|
||||||
const slicedObject = slicedKeys.reduce((obj, key) => {
|
if (this.date.sessions.length > 0) {
|
||||||
obj[key] = dates[key];
|
|
||||||
return obj;
|
|
||||||
}, {});
|
|
||||||
return slicedObject;
|
|
||||||
},
|
|
||||||
|
|
||||||
async updateDates(resetWeek = false) {
|
|
||||||
const daysNode = document.querySelector('.calendar-strip__days');
|
|
||||||
daysNode.classList.toggle('progress');
|
|
||||||
|
|
||||||
this.dates = getDatesInMonth(this.monthNumb);
|
|
||||||
this.dates = await getMergedDates(this.monthNumb, this.dates);
|
|
||||||
|
|
||||||
console.log('Merged dates', this.dates);
|
|
||||||
daysNode.classList.toggle('progress');
|
|
||||||
if (resetWeek) {
|
|
||||||
this.weekIndex = 0
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
handleDayHover() {
|
|
||||||
if (this.date.sessions.length > 0) {
|
|
||||||
this.targetSessions = this.date.sessions;
|
|
||||||
this.open = true;
|
|
||||||
} else {
|
|
||||||
setTimeout(() => {
|
|
||||||
this.targetSessions = this.date.sessions;
|
this.targetSessions = this.date.sessions;
|
||||||
}, 100);
|
this.open = true;
|
||||||
this.open = false;
|
} else {
|
||||||
}
|
setTimeout(() => {
|
||||||
},
|
this.targetSessions = this.date.sessions;
|
||||||
|
}, 100);
|
||||||
|
this.open = false;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
nextWeek() {
|
nextMonth() {
|
||||||
if (this.weekIndex < Math.floor(this.dates.length / 7)) {
|
if (this.monthNumb == 12) {
|
||||||
this.weekIndex++;
|
this.monthNumb = 1
|
||||||
} else {
|
} else {
|
||||||
this.monthNumb++;
|
this.monthNumb++
|
||||||
this.updateDates();
|
}
|
||||||
|
},
|
||||||
|
prevMonth() {
|
||||||
|
if (this.monthNumb == 1) {
|
||||||
|
this.monthNumb = 12
|
||||||
|
} else {
|
||||||
|
this.monthNumb--
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
}"
|
||||||
|
|
||||||
prevWeek() {
|
|
||||||
if (this.weekIndex > 0) {
|
|
||||||
this.weekIndex--;
|
|
||||||
} else {
|
|
||||||
this.monthNumb--;
|
|
||||||
this.updateDates();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}"
|
|
||||||
|
|
||||||
|
|
||||||
x-init="updateDates()"
|
x-init="updateDates()"
|
||||||
>
|
>
|
||||||
<div class="calendar-strip__selector">
|
<div class="calendar-strip__selector">
|
||||||
<button class="prev-month" @click="monthNumb--; updateDates()">←</button>
|
<button class="prev-month" @click="prevMonth()" :disabled="monthNumb === 9">←</button>
|
||||||
<span x-text="monthName"></span>
|
<span x-text="monthName"></span>
|
||||||
<button class="next-month" @click="monthNumb++; updateDates()">→</button>
|
<button class="next-month" @click="nextMonth()" :disabled="monthNumb === 8">→</button>
|
||||||
</div>
|
</div>
|
||||||
<ul class="calendar-strip__days">
|
<ul class="calendar-strip__days">
|
||||||
<template x-for="(date, index) in currentWeek">
|
<template x-for="(date, index) in currentMonth">
|
||||||
<li class="calendar-strip__day" :class="targetSessions.length > 0 && open && index == targetSessions[0].day ? 'active' : ''" :style="'animation-delay: ' + index * 0.03 + 's'">
|
<li class="calendar-strip__day" :class="targetSessions.length > 0 && open && index == targetSessions[0].day ? 'active' : ''" :style="'animation-delay: ' + index * 0.03 + 's'">
|
||||||
<button
|
<button
|
||||||
:class="date.full === today ? 'today' : ''"
|
:class="!date || date.full !== today ? '' : 'today'"
|
||||||
:disabled="date.sessions.length === 0 ? true : false"
|
:disabled="!date || date.sessions.length === 0 ? true : false"
|
||||||
:title="date.sessions.length === 0 ? 'Pas de représentation.' : 'Voir les représentations.'"
|
:title="!date || date.sessions.length === 0 ? 'Pas de représentation.' : 'Voir les représentations.'"
|
||||||
@mouseenter="handleDayHover()"
|
@mouseenter="handleDayHover()"
|
||||||
>
|
>
|
||||||
<span x-text="date.initial"></span>
|
<template x-if="date">
|
||||||
|
<span x-text="date.initial"></span>
|
||||||
|
</template>
|
||||||
<span x-text="index"></span>
|
<span x-text="index"></span>
|
||||||
</button>
|
</button>
|
||||||
</li>
|
</li>
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
<?php snippet('header') ?>
|
<?php snippet('header') ?>
|
||||||
|
|
||||||
<section class="hero">
|
<section class="hero">
|
||||||
<div class="hero__text">
|
<div class="hero__text">
|
||||||
<?= $site->heroText()->toBlocks() ?>
|
<?= $site->heroText()->toBlocks() ?>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue