Skip to content

Commit

Permalink
fix: [LAR-150] bug on leader when list is not equal to 3
Browse files Browse the repository at this point in the history
  • Loading branch information
mckenziearts committed Jan 1, 2025
1 parent e28c13b commit 02160f8
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 50 deletions.
32 changes: 26 additions & 6 deletions app/Livewire/Pages/Forum/Leaderboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Models\User;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Cache;
use Livewire\Attributes\Layout;
use Livewire\Component;
Expand All @@ -15,15 +16,34 @@ final class Leaderboard extends Component
{
public function render(): View
{
$startPosition = 1;
$leaders = collect();

/** @var Collection $leaderboard */
$leaderboard = User::mostSolutionsInLastDays(365)
->take(30)
->get()
->reject(fn ($leaderboard) => $leaderboard->solutions_count === 0); // @phpstan-ignore-line

if ($leaderboard->count() > 3) {
$leaders = $leaderboard->slice(0, 3);
$startPosition = 4;
}

return view('livewire.pages.forum.leaderboard', [
'leaderboard' => Cache::remember(
key: 'leaderboard',
'members' => Cache::remember(
key: 'members',
ttl: now()->addWeek(),
callback: fn () => $leaderboard->reject(
fn (User $user) => in_array($user->id, $leaders->pluck('id')->toArray()) // @phpstan-ignore-line
)
),
'leaders' => Cache::remember(
key: 'leaders',
ttl: now()->addWeek(),
callback: fn () => User::mostSolutionsInLastDays(365)
->take(30)
->get()
->reject(fn ($leaderboard) => $leaderboard->solutions_count === 0) // @phpstan-ignore-line
callback: fn () => $leaders
),
'startPosition' => $startPosition,
]);
}
}
1 change: 1 addition & 0 deletions lang/en/pages/forum.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@
'prevent_text_one' => 'Make sure you\'ve read our',
'rules' => 'rules of conduct',
'prevent_text_two' => 'before replying to this thread.',
'leaderboard_empty' => 'No ranking available',

];
1 change: 1 addition & 0 deletions lang/fr/pages/forum.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@
'prevent_text_one' => 'Assurez-vous d\'avoir lu nos',
'rules' => 'règles de conduite',
'prevent_text_two' => 'avant de répondre à ce thread.',
'leaderboard_empty' => 'Aucun classement disponible',

];
91 changes: 47 additions & 44 deletions resources/views/livewire/pages/forum/leaderboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,59 +12,57 @@ class="gap-2 w-full justify-center py-2.5"
</x-buttons.primary>
</x-slot:buttons>

@php
$top3 = $leaderboard->slice(0, 3);
$lists = $leaderboard->reject(fn ($user) => in_array($user->id, $top3->pluck('id')->toArray()))
@endphp

<div id="leaderboard">
@php
$first = $top3->first();
$second = $top3->get(1);
$third = $top3->last();
@endphp
@if($leaders->isNotEmpty())
@php
$first = $leaders->first();
$second = $leaders->get(1);
$third = $leaders->last();
@endphp

<div class="relative isolate">
<div class="flex flex-col gap-6 lg:flex-row lg:items-start lg:justify-evenly lg:pl-4">
<div class="group z-10 order-2 sm:flex sm:flex-col sm:items-center lg:order-1 lg:mt-8">
<x-forum.leader :user="$second" :position="2" />
<div class="hidden leaderboard w-60 xl:grid">
<div class="stage h-12 bg-yellow-300">
<div class="stage-front flex items-center justify-center text-white h-24 bg-gradient-to-b from-gray-100 to-gray-50 dark:from-gray-800 dark:to-gray-900"></div>
<div class="relative isolate">
<div class="flex flex-col gap-6 lg:flex-row lg:items-start lg:justify-evenly lg:pl-4">
<div class="group z-10 order-2 sm:flex sm:flex-col sm:items-center lg:order-1 lg:mt-8">
<x-forum.leader :user="$second" :position="2" />
<div class="hidden leaderboard w-60 xl:grid">
<div class="stage h-12 bg-yellow-300">
<div class="stage-front flex items-center justify-center text-white h-24 bg-gradient-to-b from-gray-100 to-gray-50 dark:from-gray-800 dark:to-gray-900"></div>
</div>
</div>
</div>
</div>
<div class="group z-20 order-1 sm:flex sm:flex-col sm:items-center lg:order-2">
<x-forum.leader :user="$first" :position="1" />
<div class="hidden leaderboard w-90 xl:grid">
<div class="stage h-12 bg-green-500">
<div class="stage-front flex items-center justify-center text-white h-32 bg-gradient-to-b from-gray-100 to-gray-50 dark:from-gray-800 dark:to-gray-900"></div>
<div class="group z-20 order-1 sm:flex sm:flex-col sm:items-center lg:order-2">
<x-forum.leader :user="$first" :position="1" />
<div class="hidden leaderboard w-90 xl:grid">
<div class="stage h-12 bg-green-500">
<div class="stage-front flex items-center justify-center text-white h-32 bg-gradient-to-b from-gray-100 to-gray-50 dark:from-gray-800 dark:to-gray-900"></div>
</div>
</div>
</div>
</div>
<div class="group z-10 order-3 sm:flex sm:flex-col sm:items-center lg:mt-8 xl:mt-14">
<x-forum.leader :user="$third" :position="3" />
<div class="hidden leaderboard w-60 xl:grid">
<div class="stage h-12 bg-danger-400">
<div class="stage-front flex items-center justify-center text-white h-[4rem] bg-gradient-to-b from-gray-100 to-gray-50 dark:from-gray-800 dark:to-gray-900"></div>
<div class="group z-10 order-3 sm:flex sm:flex-col sm:items-center lg:mt-8 xl:mt-14">
<x-forum.leader :user="$third" :position="3" />
<div class="hidden leaderboard w-60 xl:grid">
<div class="stage h-12 bg-danger-400">
<div class="stage-front flex items-center justify-center text-white h-[4rem] bg-gradient-to-b from-gray-100 to-gray-50 dark:from-gray-800 dark:to-gray-900"></div>
</div>
</div>
</div>
</div>
</div>
<div class="mt-4 w-full max-w-sm mx-auto flex items-center justify-center">
<div class="relative z-10 flex flex-1 w-full items-center overflow-hidden rounded-lg ring-1 ring-gray-200 p-[1px] dark:ring-white/10">
<div
class="animate-rotate absolute inset-0 h-full w-full rounded-full bg-[conic-gradient(#e21b30_20deg,transparent_120deg)]"
></div>
<div class="flex-1 flex items-center px-4 py-2 rounded-lg bg-white dark:bg-gray-800">
<p class="inline-flex items-center gap-2 text-[13px] text-gray-700 dark:text-gray-300">
<x-phosphor-trophy-duotone class="size-5" aria-hidden="true" />
{{ __('global.ranking_updated') }}
</p>

<div class="mt-4 w-full max-w-sm mx-auto flex items-center justify-center">
<div class="relative z-10 flex flex-1 w-full items-center overflow-hidden rounded-lg ring-1 ring-gray-200 p-[1px] dark:ring-white/10">
<div
class="animate-rotate absolute inset-0 h-full w-full rounded-full bg-[conic-gradient(#e21b30_20deg,transparent_120deg)]"
></div>
<div class="flex-1 flex items-center px-4 py-2 rounded-lg bg-white dark:bg-gray-800">
<p class="inline-flex items-center gap-2 text-[13px] text-gray-700 dark:text-gray-300">
<x-phosphor-trophy-duotone class="size-5" aria-hidden="true" />
{{ __('global.ranking_updated') }}
</p>
</div>
</div>
</div>
</div>
</div>
@endif

<div class="relative mt-10">
<div class="space-y-2">
Expand All @@ -85,11 +83,12 @@ class="animate-rotate absolute inset-0 h-full w-full rounded-full bg-[conic-grad
{{ __('global.last_active') }}
</div>
</div>
@foreach($lists as $user)

@forelse($members as $user)
<div class="grid auto-cols grid-flow-col bg-gray-100 rounded-xl px-4 py-3 text-sm text-gray-700 dark:bg-gray-950/50 dark:text-gray-300">
<div class="w-10 flex items-center gap-2">
<x-phosphor-trophy-duotone class="size-5" aria-hidden="true" />
{{ $loop->index + 4 }}
{{ $loop->index + $startPosition }}
</div>
<div class="w-32 flex items-center gap-2">
<x-user.avatar :user="$user" class="size-7" />
Expand All @@ -107,7 +106,11 @@ class="animate-rotate absolute inset-0 h-full w-full rounded-full bg-[conic-grad
{{ $user->last_active_at?->diffForHumans() }}
</div>
</div>
@endforeach
@empty
<div class="bg-gray-100 rounded-xl px-4 py-3 text-center text-sm text-gray-700 dark:bg-gray-950/50 dark:text-gray-300">
{{ __('pages/forum.leaderboard_empty') }}
</div>
@endforelse
</div>
</div>
</div>
Expand Down

0 comments on commit 02160f8

Please sign in to comment.