Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions assets/controllers/not-voted-list-controller.js
Original file line number Diff line number Diff line change
@@ -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' });
}
}
}
7 changes: 6 additions & 1 deletion src/Controller/QotdController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
70 changes: 39 additions & 31 deletions templates/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -28,57 +28,65 @@
{% endfor %}
<div {{ stimulus_controller('hide-votes') }} class="container">
{% block menu %}
<nav class="navbar navbar-expand-xxl navbar-light bg-light mb-2">
<nav class="navbar navbar-expand-lg navbar-light bg-light mb-2">
<div class="container-fluid">
<a class="navbar-brand" href="{{ url('qotd_index') }}" {{ tt('brand') }}>QOTD</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
{% set active = active ?? null %}
<div class="collapse navbar-collapse text-nowrap" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-xxl-0">
<li class="nav-item">
<a class="nav-link {{ 'qotd_top' == active ? 'active' }}" href="{{ url('qotd_index_top') }}">👍 {{ 'Top'|trans }}</a>
</li>
<li class="nav-item">
<a class="nav-link {{ 'qotd_flop' == active ? 'active' }}" href="{{ url('qotd_index_flop') }}">👎 {{ 'Flop'|trans }}</a>
</li>
<ul class="navbar-nav me-auto">
<li class="nav-item">
<a class="nav-link {{ 'qotd_not_voted' == active ? 'active' }}" href="{{ url('qotd_index_not_voted') }}">🗳 {{ 'Not voted yet'|trans }} <twig:NotVotedCounter /></a>
</li>
<li class="nav-item">
<a class="nav-link {{ 'my_top' == active ? 'active' }}" href="{{ url('my_top') }}">⭐ {{ 'My top'|trans }}</a>
</li>
<li class="nav-item">
<a class="nav-link {{ 'search' == active ? 'active' }}" href="{{ url('qotd_search') }}">🔍 {{ 'Search'|trans }}</a>
</li>
<li class="nav-item">
<a class="nav-link {{ 'random' == active ? 'active' }}" href="{{ url('random') }}">🤷 {{ 'Random'|trans }}</a>
</li>
<li class="nav-item">
<a class="nav-link {{ 'hall_of_fame' == active ? 'active' }}" href="{{ url('hall_of_fame') }}">📆 {{ 'Hall Of Fame'|trans }}</a>
<li class="nav-item dropdown">
<button class="nav-link dropdown-toggle {{ active in ['my_top', 'qotd_flop', 'qotd_top'] ? 'active' }}" type="button" data-bs-toggle="dropdown" aria-expanded="false">🔝 {{ 'Rankings'|trans }}</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item {{ 'qotd_top' == active ? 'active' }}" href="{{ url('qotd_index_top') }}">👍 {{ 'Top'|trans }}</a></li>
<li><a class="dropdown-item {{ 'qotd_flop' == active ? 'active' }}" href="{{ url('qotd_index_flop') }}">👎 {{ 'Flop'|trans }}</a></li>
<li><a class="dropdown-item {{ 'my_top' == active ? 'active' }}" href="{{ url('my_top') }}">⭐ {{ 'My top'|trans }}</a></li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link {{ 'awards' == active ? 'active' }}" href="{{ url('awards') }}">🏅 {{ 'Awards'|trans }}</a>
<li class="nav-item dropdown">
<button class="nav-link dropdown-toggle {{ active in ['awards', 'hall_of_fame', 'stats'] ? 'active' }}" type="button" data-bs-toggle="dropdown" aria-expanded="false">🏆 {{ 'Records'|trans }}</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item {{ 'hall_of_fame' == active ? 'active' }}" href="{{ url('hall_of_fame') }}">📆 {{ 'Hall Of Fame'|trans }}</a></li>
<li><a class="dropdown-item {{ 'awards' == active ? 'active' }}" href="{{ url('awards') }}">🏅 {{ 'Awards'|trans }}</a></li>
<li><a class="dropdown-item {{ 'stats' == active ? 'active' }}" href="{{ url('stats') }}">📈 {{ 'Stats'|trans }}</a></li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link {{ 'stats' == active ? 'active' }}" href="{{ url('stats') }}">📈 {{ 'Stats'|trans }}</a>
</ul>
<ul class="navbar-nav">
<li class="nav-item dropdown">
<button class="nav-link dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false" title="{{ 'Settings'|trans }}">⚙️</button>
<ul class="dropdown-menu dropdown-menu-end">
<li><a class="dropdown-item" href="{{ path('keyboard_shortcuts') }}" data-turbo-frame="modal" title="h, ?">⌨️ {{ 'Keyboard shortcuts'|trans }}</a></li>
{% for locale in app.enabled_locales|filter(locale => locale != app.request.locale) %}
<li><a class="dropdown-item text-uppercase" href="{{ path('locale_switch', {locale: locale}) }}" data-turbo="false">🌐 {{ locale }}</a></li>
{% endfor %}
<li><hr class="dropdown-divider"></li>
<li class="px-3">
<div class="form-check form-switch mb-0">
<input
class="form-check-input" type="checkbox" role="switch" id="hideVotesSwitch"
{{ stimulus_target('hide-votes', 'switch') }}
{{ stimulus_action('hide-votes', 'toggle', 'change') }}
>
<label class="form-check-label" for="hideVotesSwitch">
{{ 'Hide votes'|trans }}
</label>
</div>
</li>
</ul>
</li>
</ul>
<a href="{{ path('keyboard_shortcuts') }}" class="nav-link me-3" data-turbo-frame="modal" title="{{ 'Keyboard shortcuts'|trans }} (h, ?)">⌨️</a>
{% for locale in app.enabled_locales|filter(locale => locale != app.request.locale) %}
<a href="{{ path('locale_switch', {locale: locale}) }}" class="nav-link me-3 text-uppercase" data-turbo="false">{{ locale }}</a>
{% endfor %}
<div class="form-check form-switch">
<input
class="form-check-input" type="checkbox" role="switch" id="hideVotesSwitch"
{{ stimulus_target('hide-votes', 'switch') }}
{{ stimulus_action('hide-votes', 'toggle', 'change') }}
>
<label class="form-check-label" for="hideVotesSwitch">
{{ 'Hide votes'|trans }}
</label>
</div>
</div>
</div>
</nav>
Expand Down
2 changes: 1 addition & 1 deletion templates/components/QotdCard.html.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% import 'macros/qotd/message.html.twig' as qotd_macro %}

<div {{ attributes.defaults({class: 'card mb-2 qotd-card', 'data-controller': 'ui--clipboard'}) }} data-qotd-id="{{ qotd.id }}" {{ tt('quote') }}>
<div {{ attributes.defaults({class: 'card mb-2 qotd-card', 'data-controller': 'ui--clipboard'}) }} data-qotd-id="{{ qotd.id }}" {{ stimulus_target('not-voted-list', 'card') }} {{ tt('quote') }}>
<div class="card-body" {{ stimulus_target('ui--clipboard', 'source') }}>
{{ qotd_macro.message(qotd) }}
{% for imageUrl in qotd.imageUrls %}
Expand Down
18 changes: 10 additions & 8 deletions templates/qotd/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@
</details>
{% endif %}

{% for qotd in pagination %}
<twig:QotdCard :qotd :direction />
{% else %}
<div class="card mb-2">
<div class="card-body">
{{ 'No QOTD found.'|trans }}
<div {{ 'qotd_not_voted' == active ? stimulus_controller('not-voted-list', {url: path('qotd_index_not_voted')}) }}>
{% for qotd in pagination %}
<twig:QotdCard :qotd :direction />
{% else %}
<div class="card mb-2">
<div class="card-body">
{{ 'No QOTD found.'|trans }}
</div>
</div>
</div>
{% endfor %}
{% endfor %}
</div>

<div class="navigation">
{{ ux_pagination(pagination) }}
Expand Down
70 changes: 64 additions & 6 deletions tests/Controller/QotdControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public function testNotVotedYet(): void
self::assertResponseIsSuccessful();
self::assertSelectorTextContains('title', 'QOTD not voted yet');
self::assertCount(7, $crawler->filter('[data-test=quote]'));
self::assertCount(7, $crawler->filter('[data-controller~=not-voted-list] [data-not-voted-list-target=card]'));
}

public function testNotVotedYetNextPage(): void
Expand All @@ -109,16 +110,34 @@ public function testNotVotedYetNextPageIsStableWhileVoting(): void
$nextPageLink = $crawler->filter('a[rel=next]')->link();
$nextPageIds = self::getQuoteIds($client->click($nextPageLink));

$connection = self::getContainer()->get(Connection::class);
$vote = 'UPDATE qotd SET voter_ids = :voterIds WHERE id IN (:ids)';
$types = ['ids' => ArrayParameterType::STRING];

try {
$connection->executeStatement($vote, ['voterIds' => '{"test-user":"up"}', 'ids' => $firstPageIds], $types);
self::setVoterIds($firstPageIds, '{"test-user":"up"}');

self::assertSame($nextPageIds, self::getQuoteIds($client->click($nextPageLink)));
} finally {
$connection->executeStatement($vote, ['voterIds' => null, 'ids' => $firstPageIds], $types);
self::setVoterIds($firstPageIds, null);
}
}

public function testNotVotedYetRedirectsToTheFirstPageWhenThePreviousOneIsEmpty(): void
{
$client = self::createClientAndLogin();

$crawler = $client->request('GET', '/not-voted');
$firstPageIds = self::getQuoteIds($crawler);
$crawler = $client->click($crawler->filter('a[rel=next]')->link());
$nextPageIds = self::getQuoteIds($crawler);
$previousPageLink = $crawler->filter('a[rel=prev]')->link();

try {
self::setVoterIds($firstPageIds, '{"test-user":"up"}');

$client->click($previousPageLink);

self::assertResponseRedirects('/not-voted');
self::assertSame($nextPageIds, self::getQuoteIds($client->followRedirect()));
} finally {
self::setVoterIds($firstPageIds, null);
}
}

Expand Down Expand Up @@ -147,6 +166,33 @@ public function testShow(): void
self::assertSame(0, (int) $crawler->filter('[data-test=quote-vote]')->text());
}

/**
* @dataProvider provideMenuHighlightsTheGroupOfTheCurrentPageCases
*/
public function testMenuHighlightsTheGroupOfTheCurrentPage(string $url, string $group): void
{
$client = self::createClientAndLogin();

$client->request('GET', $url);

self::assertResponseIsSuccessful();
self::assertSelectorCount(1, '.navbar-nav .dropdown-item.active');
self::assertSelectorTextContains('.navbar-nav .dropdown-toggle.active', $group);
}

/**
* @return iterable<array{string, string}>
*/
public static function provideMenuHighlightsTheGroupOfTheCurrentPageCases(): iterable
{
yield ['/top', 'Rankings'];
yield ['/flop', 'Rankings'];
yield ['/my-top', 'Rankings'];
yield ['/hall-of-fame', 'Records'];
yield ['/awards', 'Records'];
yield ['/stats', 'Records'];
}

public static function assertTestSelectorTextContains(string $selector, string $text, string $message = ''): void
{
self::assertSelectorTextContains(\sprintf('[data-test=%s]', $selector), $text, $message);
Expand All @@ -163,6 +209,18 @@ private static function getQuoteIds(Crawler $crawler): array
;
}

/**
* @param list<string> $ids
*/
private static function setVoterIds(array $ids, ?string $voterIds): void
{
self::getContainer()->get(Connection::class)->executeStatement(
'UPDATE qotd SET voter_ids = :voterIds WHERE id IN (:ids)',
['voterIds' => $voterIds, 'ids' => $ids],
['ids' => ArrayParameterType::STRING],
);
}

private function createClientAndLogin(): KernelBrowser
{
$client = self::createClient();
Expand Down
3 changes: 3 additions & 0 deletions translations/messages.fr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ QOTD of the year: QOTD de l'année
QOTD page: Page de la QOTD
Random: Hasard
Random QOTD: QOTD au hasard
Rankings: Classements
Ranks: Rangs
Raw message: Message brut
Records: Palmarès
Remove: Retirer
Remove from my top: Retirer de mon top
Rendered message: Message rendu
Expand All @@ -75,6 +77,7 @@ Search: Rechercher
Search a QOTD: Rechercher une QOTD
Select the next QOTD: Sélectionner la QOTD suivante
Select the previous QOTD: Sélectionner la QOTD précédente
Settings: Réglages
Show the details of the selected QOTD: Afficher les détails de la QOTD sélectionnée
Show this help: Afficher cette aide
Stats: Stats
Expand Down
Loading