diff --git a/assets/controllers/not-voted-list-controller.js b/assets/controllers/not-voted-list-controller.js new file mode 100644 index 0000000..f77d439 --- /dev/null +++ b/assets/controllers/not-voted-list-controller.js @@ -0,0 +1,14 @@ +import { Controller } from '@hotwired/stimulus'; +import { visit } from '@hotwired/turbo'; + +export default class extends Controller { + static targets = ['card']; + static values = { url: String }; + + // A voted card leaves the list: without any card left, the page has nothing to offer but stale links + cardTargetDisconnected() { + if (!this.hasCardTarget) { + visit(this.urlValue, { action: 'replace' }); + } + } +} diff --git a/src/Controller/QotdController.php b/src/Controller/QotdController.php index e4ced68..0fa8722 100644 --- a/src/Controller/QotdController.php +++ b/src/Controller/QotdController.php @@ -61,13 +61,18 @@ public function index( #[Route('/not-voted', name: 'qotd_index_not_voted', options: ['expose' => true])] #[Template('qotd/index.html.twig')] - public function notVoted(#[CurrentUser] UserInterface $user): array + public function notVoted(#[CurrentUser] UserInterface $user): array|Response { $pagination = $this ->qotdRepository ->findForHomepageNotVoted($user) ; + // The QOTD on the other side of the cursor may all have been voted since the link was rendered + if ($pagination->isEmpty() && null !== $pagination->getCursor()) { + return $this->redirectToRoute('qotd_index_not_voted'); + } + return [ 'pagination' => $pagination, 'direction' => QotdDirection::NotVoted, diff --git a/templates/base.html.twig b/templates/base.html.twig index 9faac59..559152e 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -28,7 +28,7 @@ {% endfor %}
{% block menu %} -