refactor: streamline todaySessions method and simplify session rendering in the class sessions index view
This commit is contained in:
parent
8d86d696b4
commit
1eaa8fff59
@ -5,7 +5,6 @@
|
||||
use App\Filament\Resources\Learning\ClassSessions\ClassSessionResource;
|
||||
use App\Models\ClassSession;
|
||||
use App\Models\Course;
|
||||
use App\Models\CourseSchedule;
|
||||
use App\Settings\GeneralSettings;
|
||||
use Filament\Actions\Concerns\InteractsWithActions;
|
||||
use Filament\Actions\Contracts\HasActions;
|
||||
@ -14,7 +13,6 @@
|
||||
use Filament\Forms\Contracts\HasForms;
|
||||
use Filament\Resources\Pages\Page;
|
||||
use Filament\Schemas\Schema;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
use Livewire\Attributes\Computed;
|
||||
|
||||
@ -60,64 +58,31 @@ public function form(Schema $schema): Schema
|
||||
#[Computed]
|
||||
public function todaySessions(): Collection
|
||||
{
|
||||
$dayOfWeek = now()->dayOfWeekIso; // 1 (Mon) - 7 (Sun)
|
||||
return ClassSession::query()
|
||||
->whereDate('date', now())
|
||||
->with(['course'])
|
||||
->withCount('attendances')
|
||||
->get()
|
||||
->map(function ($session) {
|
||||
return (object) [
|
||||
'id' => $session->id,
|
||||
'is_pending' => false,
|
||||
'session_number' => $session->session_number,
|
||||
'course_name' => $session->course->name,
|
||||
'course_code' => $session->course->code,
|
||||
'lecturer' => $session->course->lecturer ?? '-',
|
||||
'time_range' => $session->start_time->format('H:i').' - '.$session->end_time->format('H:i'),
|
||||
'attendances_count' => $session->attendances_count,
|
||||
'url' => ClassSessionResource::getUrl('course', ['courseId' => $session->course_id]),
|
||||
|
||||
$schedules = CourseSchedule::query()
|
||||
->where('day_of_week', $dayOfWeek)
|
||||
->whereHas('course', function ($query) {
|
||||
$query->where('semester', app(GeneralSettings::class)->current_semester);
|
||||
})
|
||||
->with([
|
||||
'course',
|
||||
'course.classSessions' => fn ($q) => $q->whereDate('date', now())->withCount('attendances'),
|
||||
])
|
||||
->orderBy('start_time', 'asc')
|
||||
->get();
|
||||
|
||||
return $schedules->map(function ($schedule) {
|
||||
$session = $schedule->course->classSessions->first();
|
||||
$isPending = ! $session;
|
||||
$sessionNumber = $session?->session_number ?? (ClassSession::where('course_id', $schedule->course_id)->max('session_number') ?? 0) + 1;
|
||||
|
||||
return (object) [
|
||||
'id' => $session?->id,
|
||||
'is_pending' => $isPending,
|
||||
'session_number' => $sessionNumber,
|
||||
'course_name' => $schedule->course->name,
|
||||
'course_code' => $schedule->course->code,
|
||||
'lecturer' => $schedule->course->lecturer ?? '-',
|
||||
'time_range' => $schedule->start_time->format('H:i').' - '.$schedule->end_time->format('H:i'),
|
||||
'attendances_count' => $session?->attendances_count ?? 0,
|
||||
'url' => $isPending ? null : ClassSessionResource::getUrl('course', ['courseId' => $schedule->course->id]),
|
||||
|
||||
// Pre-calculated classes
|
||||
'card_classes' => Arr::toCssClasses([
|
||||
'group flex w-full rounded-xl border overflow-hidden relative transition-all hover:shadow-md',
|
||||
'border-primary-300 dark:border-primary-700 bg-primary-50/30 dark:bg-primary-900/10 shadow-sm' => ! $isPending,
|
||||
'border-gray-300 dark:border-gray-700 bg-gray-50/30 dark:bg-gray-800/10 border-dashed' => $isPending,
|
||||
]),
|
||||
'session_badge_classes' => Arr::toCssClasses([
|
||||
'flex h-12 w-12 shrink-0 flex-col items-center justify-center rounded-lg font-bold border',
|
||||
'bg-primary-100 dark:bg-primary-800 text-primary-700 dark:text-primary-300 border-primary-200 dark:border-primary-700' => ! $isPending,
|
||||
'bg-gray-100 dark:bg-gray-800 text-gray-700 dark:text-gray-300 border-gray-200 dark:border-gray-700' => $isPending,
|
||||
]),
|
||||
'title_classes' => Arr::toCssClasses([
|
||||
'font-bold transition-colors',
|
||||
'text-gray-900 dark:text-white group-hover:text-primary-600' => ! $isPending,
|
||||
'text-gray-500 dark:text-gray-400' => $isPending,
|
||||
]),
|
||||
'status_badge_classes' => Arr::toCssClasses([
|
||||
'flex items-center gap-1.5 rounded-full px-3 py-1 text-[11px] font-bold shadow-sm',
|
||||
'bg-primary-500 text-white animate-pulse' => ! $isPending,
|
||||
'bg-gray-500 text-white opacity-60' => $isPending,
|
||||
]),
|
||||
'attendance_section_classes' => Arr::toCssClasses([
|
||||
'flex flex-col items-center justify-center border-l p-2 bg-white/30 dark:bg-black/10 transition-colors',
|
||||
'border-primary-200 dark:border-primary-800 group-hover:bg-primary-500/5' => ! $isPending,
|
||||
'border-gray-200 dark:border-gray-800' => $isPending,
|
||||
]),
|
||||
];
|
||||
});
|
||||
// Pre-calculated classes
|
||||
'card_classes' => 'group flex w-full rounded-xl border border-primary-300 dark:border-primary-700 bg-primary-50/30 dark:bg-primary-900/10 shadow-sm overflow-hidden relative transition-all hover:shadow-md',
|
||||
'session_badge_classes' => 'flex h-12 w-12 shrink-0 flex-col items-center justify-center rounded-lg font-bold border bg-primary-100 dark:bg-primary-800 text-primary-700 dark:text-primary-300 border-primary-200 dark:border-primary-700',
|
||||
'title_classes' => 'font-bold transition-colors text-gray-900 dark:text-white group-hover:text-primary-600',
|
||||
'status_badge_classes' => 'flex items-center gap-1.5 rounded-full px-3 py-1 text-[11px] font-bold shadow-sm bg-primary-500 text-white animate-pulse',
|
||||
'attendance_section_classes' => 'flex flex-col items-center justify-center border-l border-primary-200 dark:border-primary-800 p-2 bg-white/30 dark:bg-black/10 transition-colors group-hover:bg-primary-500/5',
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
#[Computed]
|
||||
|
||||
@ -20,11 +20,7 @@
|
||||
|
||||
<div class="space-y-4">
|
||||
@foreach ($this->todaySessions as $session)
|
||||
@if ($session->is_pending)
|
||||
<div class="{{ $session->card_classes }}">
|
||||
@else
|
||||
<a href="{{ $session->url }}" class="{{ $session->card_classes }}">
|
||||
@endif
|
||||
<a href="{{ $session->url }}" class="{{ $session->card_classes }}">
|
||||
<div class="flex flex-1 items-start gap-4 p-5">
|
||||
<div class="{{ $session->session_badge_classes }}">
|
||||
<span class="text-[9px] uppercase leading-none opacity-60 mb-0.5 font-bold">Sesi</span>
|
||||
@ -73,11 +69,7 @@ class="mt-2 flex items-center gap-1.5 text-[10px] text-gray-400 dark:text-gray-5
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@if ($session->is_pending)
|
||||
</div>
|
||||
@else
|
||||
</a>
|
||||
@endif
|
||||
</a>
|
||||
@endforeach
|
||||
</div>
|
||||
</x-filament::section>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user