-
app/Actions/SortStreamsByDateAction.php
Open in GitHubuse App\Models\Stream; use Illuminate\Support\Carbon; use Illuminate\Support\Collection; class SortStreamsByDateAction { public function handle(Collection $streams): Collection { return $streams ->groupBy(static fn(Stream $item): string => $item->scheduled_start_time->format('D d.m.Y')) ->mapWithKeys(static function(Collection $item, string $date): array { $dateObject = Carbon::createFromFormat('D d.m.Y', $date); if ($dateObject->isYesterday()) { $date = 'Yesterday'; } if ($dateObject->isToday()) { $date = 'Today'; } if ($dateObject->isTomorrow()) { $date = 'Tomorrow'; } return [$date => $item]; }); } }
-
app/Http/Livewire/StreamList.php
Open in GitHubuse App\Actions\SortStreamsByDateAction; use App\Models\Stream; use Illuminate\Contracts\View\View; use Illuminate\Database\Eloquent\Builder; use Livewire\Component; use Livewire\WithPagination; class StreamList extends Component { use WithPagination; public bool $isArchive = false; public function render(): View { $streams = Stream::approved() ->when($this->isArchive, function(Builder $builder) { $builder->finished()->fromLatestToOldest(); }, function(Builder $builder) { $builder->upcomingOrLive()->fromOldestToLatest(); }) ->paginate(10); return view('livewire.stream-list', [ 'streamsByDate' => $streams->setCollection( (new SortStreamsByDateAction())->handle($streams->getCollection()) ), ]); } }
-
resources/views/livewire/stream-list.blade.php
Open in GitHub// @foreach ($streamsByDate as $date => $streams) <section class="space-y-2"> <header class="sticky top-0 z-20 py-4 bg-gray-700 bg-opacity-90 backdrop-filter backdrop-blur-lg backdrop-saturate-150"> <div class="w-full max-w-6xl px-4 mx-auto sm:px-6 md:px-8"> <h2 class="text-3xl font-bold tracking-tight text-red-400 md:text-4xl"> {{ $date }} </h2> </div> </header> <div class="w-full max-w-6xl px-4 mx-auto sm:px-6 md:px-8"> <ul class="grid gap-4 md:grid-cols-2 lg:grid-cols-1"> @foreach ($streams as $stream) @include('pages.partials.stream') @endforeach </ul> </div> </section> @endforeach //