Add photo management features, update database configuration, and enhance park model seeding

This commit is contained in:
pacnpal
2025-02-25 15:44:21 -05:00
parent 15b2d4ebcf
commit b4462ba89e
31 changed files with 2700 additions and 71 deletions

View File

@@ -0,0 +1,89 @@
<?php
namespace App\Livewire;
use App\Models\Park;
use App\Models\Photo;
use Livewire\Component;
use Illuminate\Support\Facades\Log;
class FeaturedPhotoSelectorComponent extends Component
{
public Park $park;
public $photos = [];
public $featuredPhotoId = null;
public $isLoading = true;
public $error = null;
public $success = false;
protected $listeners = ['photoUploaded' => 'loadPhotos'];
public function mount(Park $park)
{
$this->park = $park;
$this->loadPhotos();
}
public function loadPhotos()
{
$this->isLoading = true;
$this->error = null;
$this->success = false;
try {
$this->photos = $this->park->photos()->ordered()->get();
$featuredPhoto = $this->park->featuredPhoto();
$this->featuredPhotoId = $featuredPhoto ? $featuredPhoto->id : null;
$this->isLoading = false;
} catch (\Exception $e) {
Log::error('Error loading photos: ' . $e->getMessage());
$this->error = 'Failed to load photos: ' . $e->getMessage();
$this->isLoading = false;
}
}
public function setFeatured($photoId)
{
$this->isLoading = true;
$this->error = null;
$this->success = false;
try {
$photo = Photo::findOrFail($photoId);
if ($photo->photoable_id !== $this->park->id || $photo->photoable_type !== Park::class) {
throw new \Exception('Photo does not belong to this park');
}
$success = $this->park->setFeaturedPhoto($photo);
if ($success) {
$this->featuredPhotoId = $photoId;
$this->success = true;
$this->dispatch('notify', [
'type' => 'success',
'message' => 'Featured photo updated successfully'
]);
// Emit event to refresh other components
$this->dispatch('featuredPhotoChanged');
} else {
throw new \Exception('Failed to set featured photo');
}
} catch (\Exception $e) {
Log::error('Error setting featured photo: ' . $e->getMessage());
$this->error = 'Failed to set featured photo: ' . $e->getMessage();
$this->dispatch('notify', [
'type' => 'error',
'message' => 'Failed to set featured photo'
]);
} finally {
$this->isLoading = false;
}
}
public function render()
{
return view('livewire.featured-photo-selector-component');
}
}

View File

@@ -0,0 +1,108 @@
<?php
namespace App\Livewire;
use App\Models\Park;
use App\Models\Photo;
use Livewire\Component;
use Illuminate\Support\Facades\Log;
class PhotoGalleryComponent extends Component
{
public Park $park;
public $photos = [];
public $featuredPhoto = null;
public $selectedPhoto = null;
public $isLoading = true;
public $error = null;
public $viewMode = 'grid'; // grid or carousel
protected $listeners = ['photoUploaded' => 'loadPhotos'];
public function mount(Park $park)
{
$this->park = $park;
$this->loadPhotos();
}
public function loadPhotos()
{
$this->isLoading = true;
$this->error = null;
try {
$this->photos = $this->park->photos()->ordered()->get();
$this->featuredPhoto = $this->park->featuredPhoto();
$this->isLoading = false;
} catch (\Exception $e) {
Log::error('Error loading photos: ' . $e->getMessage());
$this->error = 'Failed to load photos: ' . $e->getMessage();
$this->isLoading = false;
}
}
public function selectPhoto($photoId)
{
$this->selectedPhoto = collect($this->photos)->firstWhere('id', $photoId);
}
public function closePhotoDetail()
{
$this->selectedPhoto = null;
}
public function setFeatured($photoId)
{
try {
$photo = Photo::findOrFail($photoId);
$this->park->setFeaturedPhoto($photo);
$this->loadPhotos();
$this->dispatch('notify', [
'type' => 'success',
'message' => 'Featured photo updated successfully'
]);
} catch (\Exception $e) {
Log::error('Error setting featured photo: ' . $e->getMessage());
$this->dispatch('notify', [
'type' => 'error',
'message' => 'Failed to set featured photo'
]);
}
}
public function deletePhoto($photoId)
{
try {
$photo = Photo::findOrFail($photoId);
// Make API request to the PhotoController
app(\App\Http\Controllers\PhotoController::class)->destroy($photo);
$this->loadPhotos();
$this->dispatch('notify', [
'type' => 'success',
'message' => 'Photo deleted successfully'
]);
if ($this->selectedPhoto && $this->selectedPhoto->id === $photoId) {
$this->selectedPhoto = null;
}
} catch (\Exception $e) {
Log::error('Error deleting photo: ' . $e->getMessage());
$this->dispatch('notify', [
'type' => 'error',
'message' => 'Failed to delete photo: ' . $e->getMessage()
]);
}
}
public function toggleViewMode()
{
$this->viewMode = $this->viewMode === 'grid' ? 'carousel' : 'grid';
}
public function render()
{
return view('livewire.photo-gallery-component');
}
}

View File

@@ -0,0 +1,100 @@
<?php
namespace App\Livewire;
use App\Models\Park;
use Livewire\Component;
use Illuminate\Support\Facades\Log;
class PhotoManagerComponent extends Component
{
public Park $park;
public $photos = [];
public $isLoading = true;
public $error = null;
public $reordering = false;
public $photoOrder = [];
protected $listeners = ['photoUploaded' => 'loadPhotos'];
public function mount(Park $park)
{
$this->park = $park;
$this->loadPhotos();
}
public function loadPhotos()
{
$this->isLoading = true;
$this->error = null;
try {
$this->photos = $this->park->photos()->ordered()->get()->toArray();
$this->photoOrder = collect($this->photos)->pluck('id')->toArray();
$this->isLoading = false;
} catch (\Exception $e) {
Log::error('Error loading photos: ' . $e->getMessage());
$this->error = 'Failed to load photos: ' . $e->getMessage();
$this->isLoading = false;
}
}
public function startReordering()
{
$this->reordering = true;
}
public function cancelReordering()
{
$this->reordering = false;
$this->loadPhotos(); // Reset to original order
}
public function saveOrder()
{
try {
// Make API request to the PhotoController
app(\App\Http\Controllers\PhotoController::class)->reorder(
request: new \Illuminate\Http\Request(['photo_ids' => $this->photoOrder]),
park: $this->park
);
$this->reordering = false;
$this->loadPhotos();
$this->dispatch('notify', [
'type' => 'success',
'message' => 'Photo order updated successfully'
]);
} catch (\Exception $e) {
Log::error('Error reordering photos: ' . $e->getMessage());
$this->dispatch('notify', [
'type' => 'error',
'message' => 'Failed to update photo order: ' . $e->getMessage()
]);
}
}
public function moveUp($index)
{
if ($index > 0) {
$temp = $this->photoOrder[$index - 1];
$this->photoOrder[$index - 1] = $this->photoOrder[$index];
$this->photoOrder[$index] = $temp;
}
}
public function moveDown($index)
{
if ($index < count($this->photoOrder) - 1) {
$temp = $this->photoOrder[$index + 1];
$this->photoOrder[$index + 1] = $this->photoOrder[$index];
$this->photoOrder[$index] = $temp;
}
}
public function render()
{
return view('livewire.photo-manager-component');
}
}

View File

@@ -0,0 +1,92 @@
<?php
namespace App\Livewire;
use App\Models\Park;
use Livewire\Component;
use Livewire\WithFileUploads;
use Illuminate\Support\Facades\Log;
class PhotoUploadComponent extends Component
{
use WithFileUploads;
public Park $park;
public $photo;
public $title;
public $description;
public $alt_text;
public $credit;
public $source_url;
public $is_featured = false;
public $uploading = false;
public $uploadError = null;
public $uploadSuccess = false;
protected $rules = [
'photo' => 'required|image|max:10240', // 10MB max
'title' => 'nullable|string|max:255',
'description' => 'nullable|string',
'alt_text' => 'nullable|string|max:255',
'credit' => 'nullable|string|max:255',
'source_url' => 'nullable|url|max:255',
'is_featured' => 'boolean',
];
public function mount(Park $park)
{
$this->park = $park;
}
public function updatedPhoto()
{
$this->validate([
'photo' => 'image|max:10240', // 10MB max
]);
}
public function save()
{
$this->uploading = true;
$this->uploadError = null;
$this->uploadSuccess = false;
try {
$this->validate();
// Create form data for the API request
$formData = [
'photo' => $this->photo,
'title' => $this->title,
'description' => $this->description,
'alt_text' => $this->alt_text,
'credit' => $this->credit,
'source_url' => $this->source_url,
'is_featured' => $this->is_featured,
];
// Make API request to the PhotoController
$response = app(\App\Http\Controllers\PhotoController::class)->store(
request: new \Illuminate\Http\Request($formData),
park: $this->park
);
// Reset form
$this->reset(['photo', 'title', 'description', 'alt_text', 'credit', 'source_url', 'is_featured']);
$this->uploadSuccess = true;
// Emit event to refresh the photo gallery
$this->dispatch('photoUploaded');
} catch (\Exception $e) {
Log::error('Photo upload error: ' . $e->getMessage());
$this->uploadError = 'Failed to upload photo: ' . $e->getMessage();
} finally {
$this->uploading = false;
}
}
public function render()
{
return view('livewire.photo-upload-component');
}
}