parfum/app/Livewire/Studio/Master/Outlet/Index.php
Yoga Pangestu 5a49bc8ba4 refactor(outlet): menyesuaikan unit test dari segi penamaan, logika bisnis, clean code dan lainnya
-menghapus key closed_date di factory
-salah menulis relasi di model
-antisipasi error ketika image null atau array kosong di index
-memindahkan posisi toast menjadi di atas redirect
2025-10-14 21:10:22 +07:00

86 lines
2.9 KiB
PHP

<?php
namespace App\Livewire\Studio\Master\Outlet;
use App\Enums\Day;
use App\Models\Outlet;
use App\Traits\WithCloseModal;
use App\Traits\WithConfirmation;
use App\Traits\WithMediaHandler;
use App\Traits\WithToast;
use Flux\Flux;
use Illuminate\Support\Str;
use Livewire\Attributes\Title;
use Livewire\Component;
#[Title('Outlet')]
class Index extends Component
{
use WithCloseModal, WithConfirmation, WithMediaHandler, WithToast;
public array $outlets = [];
public ?string $imageUrl = null;
public function mount()
{
$this->outlets = Outlet::with(['openingHours', 'facilities'])
->latest()
->get()
->map(fn ($outlet) => [
'hash' => $outlet->hash,
'name' => $outlet->name,
'phone_number' => $outlet->phone_number,
'landmark' => $outlet->landmark,
'address' => $outlet->address,
'status' => [
'label' => $outlet->status->label(),
'color' => $outlet->status->color(),
],
'opening_hours' => $outlet->openingHours->mapWithKeys(fn ($hour) => [
Str::ucfirst(Str::lower(Day::from($hour->day->value)->label())) => [
'open' => formatTime($hour->open_time),
'close' => formatTime($hour->close_time),
'is_closed' => $hour->open_time === null && $hour->close_time === null,
],
])->toArray(),
'facilities' => $outlet->facilities->pluck('name')->toArray(),
'opened_date' => formatDate($outlet->opened_date),
'opened_ago' => timeAgo($outlet->opened_date),
'closed_date' => $outlet->closed_date ? formatDate($outlet->closed_date) : null,
'closed_ago' => $outlet->closed_date ? timeAgo($outlet->closed_date) : null,
'edit_route' => route('studio.master.outlet.edit', $outlet->hash),
'delete_route' => route('studio.master.outlet.delete', $outlet->hash),
'image' => data_get($this->mapMediaCollection($outlet->getMedia('featured_image'))[0] ?? null, 'temporaryUrl', asset('images/placeholder.jpg')),
'images' => collect($this->mapMediaCollection($outlet->getMedia('images')))
->pluck('temporaryUrl')
->filter()
->values(),
])
->toArray();
}
public function openImage(string $imageUrl)
{
$this->imageUrl = $imageUrl;
Flux::modal('image-modal')->show();
}
public function delete(Outlet $outlet)
{
$outlet->delete();
$this->toast('Outlet berhasil dihapus.');
Flux::modals()->close();
}
public function render()
{
return view('livewire.studio.master.outlet.index', [
'pageTitle' => 'Outlet',
]);
}
}