Skip to content

Commit 02160f8

Browse files
committed
fix: [LAR-150] bug on leader when list is not equal to 3
1 parent e28c13b commit 02160f8

File tree

4 files changed

+75
-50
lines changed

4 files changed

+75
-50
lines changed

app/Livewire/Pages/Forum/Leaderboard.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use App\Models\User;
88
use Illuminate\Contracts\View\View;
9+
use Illuminate\Database\Eloquent\Collection;
910
use Illuminate\Support\Facades\Cache;
1011
use Livewire\Attributes\Layout;
1112
use Livewire\Component;
@@ -15,15 +16,34 @@ final class Leaderboard extends Component
1516
{
1617
public function render(): View
1718
{
19+
$startPosition = 1;
20+
$leaders = collect();
21+
22+
/** @var Collection $leaderboard */
23+
$leaderboard = User::mostSolutionsInLastDays(365)
24+
->take(30)
25+
->get()
26+
->reject(fn ($leaderboard) => $leaderboard->solutions_count === 0); // @phpstan-ignore-line
27+
28+
if ($leaderboard->count() > 3) {
29+
$leaders = $leaderboard->slice(0, 3);
30+
$startPosition = 4;
31+
}
32+
1833
return view('livewire.pages.forum.leaderboard', [
19-
'leaderboard' => Cache::remember(
20-
key: 'leaderboard',
34+
'members' => Cache::remember(
35+
key: 'members',
36+
ttl: now()->addWeek(),
37+
callback: fn () => $leaderboard->reject(
38+
fn (User $user) => in_array($user->id, $leaders->pluck('id')->toArray()) // @phpstan-ignore-line
39+
)
40+
),
41+
'leaders' => Cache::remember(
42+
key: 'leaders',
2143
ttl: now()->addWeek(),
22-
callback: fn () => User::mostSolutionsInLastDays(365)
23-
->take(30)
24-
->get()
25-
->reject(fn ($leaderboard) => $leaderboard->solutions_count === 0) // @phpstan-ignore-line
44+
callback: fn () => $leaders
2645
),
46+
'startPosition' => $startPosition,
2747
]);
2848
}
2949
}

lang/en/pages/forum.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@
4747
'prevent_text_one' => 'Make sure you\'ve read our',
4848
'rules' => 'rules of conduct',
4949
'prevent_text_two' => 'before replying to this thread.',
50+
'leaderboard_empty' => 'No ranking available',
5051

5152
];

lang/fr/pages/forum.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@
4747
'prevent_text_one' => 'Assurez-vous d\'avoir lu nos',
4848
'rules' => 'règles de conduite',
4949
'prevent_text_two' => 'avant de répondre à ce thread.',
50+
'leaderboard_empty' => 'Aucun classement disponible',
5051

5152
];

resources/views/livewire/pages/forum/leaderboard.blade.php

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,59 +12,57 @@ class="gap-2 w-full justify-center py-2.5"
1212
</x-buttons.primary>
1313
</x-slot:buttons>
1414

15-
@php
16-
$top3 = $leaderboard->slice(0, 3);
17-
$lists = $leaderboard->reject(fn ($user) => in_array($user->id, $top3->pluck('id')->toArray()))
18-
@endphp
19-
2015
<div id="leaderboard">
21-
@php
22-
$first = $top3->first();
23-
$second = $top3->get(1);
24-
$third = $top3->last();
25-
@endphp
16+
@if($leaders->isNotEmpty())
17+
@php
18+
$first = $leaders->first();
19+
$second = $leaders->get(1);
20+
$third = $leaders->last();
21+
@endphp
2622

27-
<div class="relative isolate">
28-
<div class="flex flex-col gap-6 lg:flex-row lg:items-start lg:justify-evenly lg:pl-4">
29-
<div class="group z-10 order-2 sm:flex sm:flex-col sm:items-center lg:order-1 lg:mt-8">
30-
<x-forum.leader :user="$second" :position="2" />
31-
<div class="hidden leaderboard w-60 xl:grid">
32-
<div class="stage h-12 bg-yellow-300">
33-
<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>
23+
<div class="relative isolate">
24+
<div class="flex flex-col gap-6 lg:flex-row lg:items-start lg:justify-evenly lg:pl-4">
25+
<div class="group z-10 order-2 sm:flex sm:flex-col sm:items-center lg:order-1 lg:mt-8">
26+
<x-forum.leader :user="$second" :position="2" />
27+
<div class="hidden leaderboard w-60 xl:grid">
28+
<div class="stage h-12 bg-yellow-300">
29+
<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>
30+
</div>
3431
</div>
3532
</div>
36-
</div>
37-
<div class="group z-20 order-1 sm:flex sm:flex-col sm:items-center lg:order-2">
38-
<x-forum.leader :user="$first" :position="1" />
39-
<div class="hidden leaderboard w-90 xl:grid">
40-
<div class="stage h-12 bg-green-500">
41-
<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>
33+
<div class="group z-20 order-1 sm:flex sm:flex-col sm:items-center lg:order-2">
34+
<x-forum.leader :user="$first" :position="1" />
35+
<div class="hidden leaderboard w-90 xl:grid">
36+
<div class="stage h-12 bg-green-500">
37+
<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>
38+
</div>
4239
</div>
4340
</div>
44-
</div>
45-
<div class="group z-10 order-3 sm:flex sm:flex-col sm:items-center lg:mt-8 xl:mt-14">
46-
<x-forum.leader :user="$third" :position="3" />
47-
<div class="hidden leaderboard w-60 xl:grid">
48-
<div class="stage h-12 bg-danger-400">
49-
<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>
41+
<div class="group z-10 order-3 sm:flex sm:flex-col sm:items-center lg:mt-8 xl:mt-14">
42+
<x-forum.leader :user="$third" :position="3" />
43+
<div class="hidden leaderboard w-60 xl:grid">
44+
<div class="stage h-12 bg-danger-400">
45+
<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>
46+
</div>
5047
</div>
5148
</div>
5249
</div>
53-
</div>
54-
<div class="mt-4 w-full max-w-sm mx-auto flex items-center justify-center">
55-
<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">
56-
<div
57-
class="animate-rotate absolute inset-0 h-full w-full rounded-full bg-[conic-gradient(#e21b30_20deg,transparent_120deg)]"
58-
></div>
59-
<div class="flex-1 flex items-center px-4 py-2 rounded-lg bg-white dark:bg-gray-800">
60-
<p class="inline-flex items-center gap-2 text-[13px] text-gray-700 dark:text-gray-300">
61-
<x-phosphor-trophy-duotone class="size-5" aria-hidden="true" />
62-
{{ __('global.ranking_updated') }}
63-
</p>
50+
51+
<div class="mt-4 w-full max-w-sm mx-auto flex items-center justify-center">
52+
<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">
53+
<div
54+
class="animate-rotate absolute inset-0 h-full w-full rounded-full bg-[conic-gradient(#e21b30_20deg,transparent_120deg)]"
55+
></div>
56+
<div class="flex-1 flex items-center px-4 py-2 rounded-lg bg-white dark:bg-gray-800">
57+
<p class="inline-flex items-center gap-2 text-[13px] text-gray-700 dark:text-gray-300">
58+
<x-phosphor-trophy-duotone class="size-5" aria-hidden="true" />
59+
{{ __('global.ranking_updated') }}
60+
</p>
61+
</div>
6462
</div>
6563
</div>
6664
</div>
67-
</div>
65+
@endif
6866

6967
<div class="relative mt-10">
7068
<div class="space-y-2">
@@ -85,11 +83,12 @@ class="animate-rotate absolute inset-0 h-full w-full rounded-full bg-[conic-grad
8583
{{ __('global.last_active') }}
8684
</div>
8785
</div>
88-
@foreach($lists as $user)
86+
87+
@forelse($members as $user)
8988
<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">
9089
<div class="w-10 flex items-center gap-2">
9190
<x-phosphor-trophy-duotone class="size-5" aria-hidden="true" />
92-
{{ $loop->index + 4 }}
91+
{{ $loop->index + $startPosition }}
9392
</div>
9493
<div class="w-32 flex items-center gap-2">
9594
<x-user.avatar :user="$user" class="size-7" />
@@ -107,7 +106,11 @@ class="animate-rotate absolute inset-0 h-full w-full rounded-full bg-[conic-grad
107106
{{ $user->last_active_at?->diffForHumans() }}
108107
</div>
109108
</div>
110-
@endforeach
109+
@empty
110+
<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">
111+
{{ __('pages/forum.leaderboard_empty') }}
112+
</div>
113+
@endforelse
111114
</div>
112115
</div>
113116
</div>

0 commit comments

Comments
 (0)