It is gonna be a bit long, sorry about that.
- /site/templates/events.php :
<div class="col-full events" data-page="<?= $pagination->nextPage() ?>">
<div class="row-event-wrap">
<?php foreach ($events as $day => $eventsPerDays) : ?>
<?php snippet('event', ['day' => $day, 'eventsPerDays' => $eventsPerDays]) ?>
<?php endforeach ?>
</div>
</div>
<div class="load-more">more</div>
//
//
// The events' content
//
//
- /site/controllers/events.php
<?php
return function ($page) {
$limit = 2;
$callback = function ($p) {
return $p->date()->toDate('d M');
};
$events = $page->children()->listed()->filterBy('template', 'in', ['event-item', 'festival-item'])->sortBy('date', 'asc')->group($callback)->paginate($limit);
return [
'limit' => $limit,
'events' => $events,
'pagination' => $events->pagination(),
];
};
- /site/controllers/events.json.php
<?php
return function ($page) {
$limit = 2;
$callback = function ($p) {
return $p->date()->toDate('d M');
};
$events =
$page->children()->listed()->filterBy('template', 'in', ['event-item', 'festival-item'])->sortBy('date', 'asc')->group($callback)->paginate($limit);
$pagination = $events->pagination();
$more = $pagination->hasNextPage();
return [
'events' => $events,
'more' => $more,
'html' => '',
'json' => [],
];
};
- /site/templates/events.json.php
<?php
foreach ($events as $day => $eventsPerDays) {
$html .= snippet('event', ['day' => $day, 'eventsPerDays' => $eventsPerDays], true);
}
$json['html'] = $html;
$json['more'] = $more;
echo json_encode($json);
- assets/js/templates/events.js
document.documentElement.classList.replace("no-js", "js");
document.addEventListener("DOMContentLoaded", () => {
const element = document.querySelector(".events");
const button = document.querySelector(".load-more");
let page = parseInt(element.getAttribute("data-page"));
console.log({ element, button, page });
const fetchProjects = async () => {
let url = `${window.location.href}.json/page:${page}`;
console.log(url);
try {
const response = await fetch(url);
const { html, more } = await response.json();
console.log({ html, more });
button.hidden = !more;
element.innerHTML += html;
page++;
} catch (error) {
console.log("Fetch error: ", error);
}
};
button.addEventListener("click", fetchProjects);
});
The problem is that the JSON always shows the same 2 events…