Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow activating aggregate queries with a cookie #1913

Merged
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
1 change: 1 addition & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Kernel extends HttpKernel
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Site\Middleware\RedirectsMissingPages::class,
\App\Site\Middleware\RobotsMiddleware::class,
\App\Site\Middleware\FeatureFlagMiddleware::class,
];

/**
Expand Down
1 change: 1 addition & 0 deletions app/Site/Middleware/EncryptCookies.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ class EncryptCookies extends Middleware
'prefers_hidden_user_completed_sets',
'prefers_seeing_saved_hidden_rows_when_reordering',
'progression_status_widths_preference',
'feature_aggregate_queries',
];
}
25 changes: 25 additions & 0 deletions app/Site/Middleware/FeatureFlagMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace App\Site\Middleware;

use Closure;
use Illuminate\Http\Request;

class FeatureFlagMiddleware
{
public function handle(Request $request, Closure $next): mixed
{
$cookieValue = $request->cookie('feature_aggregate_queries');

// Override the feature flag configuration if the cookie is set to 'true'.
if ($cookieValue === 'true') {
config(['feature.aggregate_queries' => true]);
} elseif ($cookieValue === 'false') {
config(['feature.aggregate_queries' => false]);
}

return $next($request);
}
}
2 changes: 2 additions & 0 deletions resources/js/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
autoExpandTextInput,
copyToClipboard,
fetcher,
getCookie,
getStringByteCount,
handleLeaderboardTabClick,
initializeTextareaCounter,
Expand All @@ -36,6 +37,7 @@ lazyLoadModuleOnIdFound({
window.autoExpandTextInput = autoExpandTextInput;
window.copyToClipboard = copyToClipboard;
window.fetcher = fetcher;
window.getCookie = getCookie;
window.getStringByteCount = getStringByteCount;
window.handleLeaderboardTabClick = handleLeaderboardTabClick;
window.initializeTextareaCounter = initializeTextareaCounter;
Expand Down
3 changes: 2 additions & 1 deletion resources/js/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type { handleLeaderboardTabClick as HandleLeaderboardTabClick } from '@/u
import type { initializeTextareaCounter as InitializeTextareaCounter } from '@/utils/initializeTextareaCounter';
import type { injectShortcode as InjectShortcode } from '@/utils/injectShortcode';
import type { loadPostPreview as LoadPostPreview } from '@/utils/loadPostPreview';
import type { setCookie as SetCookie } from '@/utils/cookie';
import type { getCookie as GetCookie, setCookie as SetCookie } from '@/utils/cookie';
import type { toggleUserCompletedSetsVisibility as ToggleUserCompletedSetsVisibility } from '@/utils/toggleUserCompletedSetsVisibility';

declare global {
Expand All @@ -24,6 +24,7 @@ declare global {
var cfg: Record<string, unknown> | undefined;
var copyToClipboard: (text: string) => void;
var fetcher: typeof Fetcher;
var getCookie: typeof GetCookie;
var getStringByteCount: typeof GetStringByteCount;
var handleLeaderboardTabClick: typeof HandleLeaderboardTabClick;
var hideEarnedCheckboxComponent: typeof HideEarnedCheckboxComponent;
Expand Down
32 changes: 32 additions & 0 deletions resources/views/components/feature-flags.blade.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
<?php
$aggregateQueriesSource = \Request::cookie('feature_aggregate_queries') !== null ? 'cookie' : 'env';
?>

<script>
function handleToggleCookie() {
const currentValue = getCookie('feature_aggregate_queries');

let newValue = true;
if (currentValue === 'true') {
newValue = false;
}

setCookie('feature_aggregate_queries', `${newValue}`);
showStatusSuccess(`feature_aggregate_queries cookie set to ${newValue}.`);
}
</script>

<div class="flex justify-between">
<p>Beaten Games Player-facing UX</p>
@hasfeature("beat")
Expand All @@ -6,3 +24,17 @@
Disabled
@endhasfeature
</div>

<div class="flex justify-between">
<p>Aggregate Queries</p>

<button class="btn" onClick="handleToggleCookie()">
Toggle cookie
</button>

@hasfeature("aggregate_queries")
Enabled (Source: {{ $aggregateQueriesSource }})
@else
Disabled (Source: {{ $aggregateQueriesSource }})
@endhasfeature
</div>
Loading