From 1b47c2529497d5071e534b9ca0a4ac1bb1f30701 Mon Sep 17 00:00:00 2001
From: MGeurts <5404867+MGeurts@users.noreply.github.com>
Date: Wed, 6 Nov 2024 15:57:50 +0100
Subject: [PATCH] Optimized Datasheet Livewire component
---
app/Livewire/People/Datasheet.php | 27 +++++++-----
.../views/livewire/people/datasheet.blade.php | 42 ++++++++++---------
2 files changed, 38 insertions(+), 31 deletions(-)
diff --git a/app/Livewire/People/Datasheet.php b/app/Livewire/People/Datasheet.php
index eafa26d04..d624f81a0 100644
--- a/app/Livewire/People/Datasheet.php
+++ b/app/Livewire/People/Datasheet.php
@@ -4,32 +4,37 @@
namespace App\Livewire\People;
-use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
use Illuminate\View\View;
use Livewire\Component;
class Datasheet extends Component
{
- // ------------------------------------------------------------------------------
public $person;
public array $images = [];
- public Collection $files;
+ public array $files = [];
- // ------------------------------------------------------------------------------
+ /**
+ * Mount the component and load relevant data.
+ */
public function mount(): void
{
- $this->images = collect(File::glob(public_path() . '/storage/photos/' . $this->person->team_id . '/' . $this->person->id . '_*.webp'))
- ->map(function ($p) {
- return substr($p, strrpos($p, '/') + 1);
- })->toArray();
-
- $this->files = $this->person->getMedia('files');
+ // Load image files for the person (webp format), returning only filenames with extensions
+ $this->images = collect(File::glob(storage_path("app/public/photos/{$this->person->team_id}/{$this->person->id}_*.webp")))
+ ->map(fn ($path) => basename($path)) // Extract filename with extension
+ ->toArray();
+
+ // Load the media files associated with the person and extract only filenames (including extensions)
+ $this->files = $this->person->getMedia('files')
+ ->map(fn ($media) => basename($media->getPath())) // Extract filename with extension from media path
+ ->toArray();
}
- // ------------------------------------------------------------------------------
+ /**
+ * Render the Livewire view.
+ */
public function render(): View
{
return view('livewire.people.datasheet');
diff --git a/resources/views/livewire/people/datasheet.blade.php b/resources/views/livewire/people/datasheet.blade.php
index 277fb8db5..d56c8f4d1 100644
--- a/resources/views/livewire/people/datasheet.blade.php
+++ b/resources/views/livewire/people/datasheet.blade.php
@@ -97,11 +97,11 @@
$cemetery = array_filter([$person->getMetadataValue('cemetery_location_name'), $person->getMetadataValue('cemetery_location_address')]);
@endphp
- @foreach($cemetery as $line)
+ @foreach ($cemetery as $line)
{{ $line }}
- @if(!$loop->last)
-
+ @if (!$loop->last)
+
@endif
@endforeach
@@ -154,7 +154,7 @@