Skip to content

Commit

Permalink
Optimized Datasheet Livewire component
Browse files Browse the repository at this point in the history
  • Loading branch information
MGeurts committed Nov 6, 2024
1 parent 14e6e1b commit 1b47c25
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 31 deletions.
27 changes: 16 additions & 11 deletions app/Livewire/People/Datasheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
42 changes: 22 additions & 20 deletions resources/views/livewire/people/datasheet.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
<br/>
@if (!$loop->last)
<br />
@endif
@endforeach
</td>
Expand Down Expand Up @@ -154,7 +154,7 @@
<td>{{ __('person.father') }} :</td>
<td>
@if ($person->father)
{{ $person->father->name }}
{{ $person->father->name }}
<x-ts-icon icon="{{ $person->father->sex == 'm' ? 'gender-male' : 'gender-female' }}" class="inline-block size-5" />
{{ $person->father->birth_year }}
@endif
Expand All @@ -176,8 +176,10 @@
<td>{{ __('person.parents') }} :</td>
<td>
@if ($person->parents)
{{ $person->parents->person_1->name }} <x-ts-icon icon="{{ $person->parents->person_1->sex == 'm' ? 'gender-male' : 'gender-female' }}" class="inline-block size-5" /> {{ $person->parents->person_1->birth_year }}<br/>
{{ $person->parents->person_2->name }} <x-ts-icon icon="{{ $person->parents->person_2->sex == 'm' ? 'gender-male' : 'gender-female' }}" class="inline-block size-5" /> {{ $person->parents->person_2->birth_year }}
{{ $person->parents->person_1->name }} <x-ts-icon icon="{{ $person->parents->person_1->sex == 'm' ? 'gender-male' : 'gender-female' }}" class="inline-block size-5" />
{{ $person->parents->person_1->birth_year }}<br />
{{ $person->parents->person_2->name }} <x-ts-icon icon="{{ $person->parents->person_2->sex == 'm' ? 'gender-male' : 'gender-female' }}" class="inline-block size-5" />
{{ $person->parents->person_2->birth_year }}
@endif
</td>
</tr>
Expand All @@ -187,12 +189,12 @@
<td>
@if ($person->currentPartner())
{{ $person->currentPartner()->name }}
<x-ts-icon icon="{{ $person->currentPartner()->sex == 'm' ? 'gender-male' : 'gender-female' }}" class="inline-block size-5" />
<x-ts-icon icon="{{ $person->currentPartner()->sex == 'm' ? 'gender-male' : 'gender-female' }}" class="inline-block size-5" />
{{ $person->currentPartner()->birth_year }}
@endif
</td>
</tr>

<tr>
<td colspan="4">&nbsp;</td>
</tr>
Expand Down Expand Up @@ -235,7 +237,7 @@
{{ $couple->date_end ? $couple->date_end->isoFormat('LL') : '??' }}
@endif
</td>
</tr>
</tr>
@endforeach

<tr>
Expand All @@ -258,11 +260,11 @@
<td>
{{ $child->name }}
<x-ts-icon icon="{{ $child->sex == 'm' ? 'gender-male' : 'gender-female' }}" class="inline-block size-5" />
{{ $child->birth_year }}
{{ $child->birth_year }}
</td>
</tr>
</tr>
@endforeach

<tr>
<td colspan="4">&nbsp;</td>
</tr>
Expand All @@ -283,12 +285,12 @@
<td>
{{ $sibling->name }}
<x-ts-icon icon="{{ $sibling->sex == 'm' ? 'gender-male' : 'gender-female' }}" class="inline-block size-5" />
{{ $sibling->birth_year }}
{{ $sibling->birth_year }}
<span class="text-warning-500">{{ $sibling->type }}</span>
</td>
</tr>
</tr>
@endforeach

<tr>
<td colspan="4">&nbsp;</td>
</tr>
Expand All @@ -307,15 +309,15 @@
<tr>
<td colspan="3">&nbsp;</td>
<td>
{{ $file->file_name }}
{{ $file }}
</td>
</tr>
</tr>
@endforeach

<tr>
<td colspan="4">&nbsp;</td>
</tr>

{{-- photos --}}
<tr class="border-gray-600 border-solid border-y-2">
<td colspan="4">
Expand All @@ -332,7 +334,7 @@
@foreach ($images as $image)
<div>
<img class="rounded max-w-48" src="{{ asset('storage/photos-384/' . $person->team_id . '/' . $image) }}" alt="{{ $person->name }}" />
</div>
</div>
@endforeach
</div>
</div>

0 comments on commit 1b47c25

Please sign in to comment.