mirror of
https://github.com/pacnpal/thrillwiki_laravel.git
synced 2025-12-20 02:31:09 -05:00
feat: implement search functionality with real-time filtering and pagination
This commit is contained in:
136
app/Livewire/SearchComponent.php
Normal file
136
app/Livewire/SearchComponent.php
Normal file
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire;
|
||||
|
||||
use App\Models\Park;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Livewire\Component;
|
||||
use Livewire\WithPagination;
|
||||
|
||||
class SearchComponent extends Component
|
||||
{
|
||||
use WithPagination;
|
||||
|
||||
// Filter properties
|
||||
public string $search = '';
|
||||
public string $location = '';
|
||||
public ?float $minRating = null;
|
||||
public ?float $maxRating = null;
|
||||
public ?int $minRides = null;
|
||||
public ?int $minCoasters = null;
|
||||
public bool $filtersApplied = false;
|
||||
|
||||
// Querystring parameters
|
||||
protected $queryString = [
|
||||
'search' => ['except' => ''],
|
||||
'location' => ['except' => ''],
|
||||
'minRating' => ['except' => ''],
|
||||
'maxRating' => ['except' => ''],
|
||||
'minRides' => ['except' => ''],
|
||||
'minCoasters' => ['except' => '']
|
||||
];
|
||||
|
||||
public function mount(): void
|
||||
{
|
||||
$this->filtersApplied = $this->hasActiveFilters();
|
||||
}
|
||||
|
||||
public function render(): View
|
||||
{
|
||||
return view('livewire.search', [
|
||||
'results' => $this->getFilteredParks()
|
||||
]);
|
||||
}
|
||||
|
||||
public function updatedSearch(): void
|
||||
{
|
||||
$this->resetPage();
|
||||
$this->filtersApplied = $this->hasActiveFilters();
|
||||
}
|
||||
|
||||
public function updatedLocation(): void
|
||||
{
|
||||
$this->resetPage();
|
||||
$this->filtersApplied = $this->hasActiveFilters();
|
||||
}
|
||||
|
||||
public function updatedMinRating(): void
|
||||
{
|
||||
$this->resetPage();
|
||||
$this->filtersApplied = $this->hasActiveFilters();
|
||||
}
|
||||
|
||||
public function updatedMaxRating(): void
|
||||
{
|
||||
$this->resetPage();
|
||||
$this->filtersApplied = $this->hasActiveFilters();
|
||||
}
|
||||
|
||||
public function updatedMinRides(): void
|
||||
{
|
||||
$this->resetPage();
|
||||
$this->filtersApplied = $this->hasActiveFilters();
|
||||
}
|
||||
|
||||
public function updatedMinCoasters(): void
|
||||
{
|
||||
$this->resetPage();
|
||||
$this->filtersApplied = $this->hasActiveFilters();
|
||||
}
|
||||
|
||||
public function clearFilters(): void
|
||||
{
|
||||
$this->reset([
|
||||
'search',
|
||||
'location',
|
||||
'minRating',
|
||||
'maxRating',
|
||||
'minRides',
|
||||
'minCoasters'
|
||||
]);
|
||||
$this->filtersApplied = false;
|
||||
$this->resetPage();
|
||||
}
|
||||
|
||||
protected function getFilteredParks()
|
||||
{
|
||||
return Park::query()
|
||||
->select('parks.*')
|
||||
->with(['location', 'photos'])
|
||||
->when($this->search, function (Builder $query) {
|
||||
$query->where(function (Builder $query) {
|
||||
$query->where('name', 'like', "%{$this->search}%")
|
||||
->orWhere('description', 'like', "%{$this->search}%");
|
||||
});
|
||||
})
|
||||
->when($this->location, function (Builder $query) {
|
||||
$query->whereHas('location', function (Builder $query) {
|
||||
$query->where('address_text', 'like', "%{$this->location}%");
|
||||
});
|
||||
})
|
||||
->when($this->minRating, function (Builder $query) {
|
||||
$query->where('average_rating', '>=', $this->minRating);
|
||||
})
|
||||
->when($this->maxRating, function (Builder $query) {
|
||||
$query->where('average_rating', '<=', $this->maxRating);
|
||||
})
|
||||
->when($this->minRides, function (Builder $query) {
|
||||
$query->where('ride_count', '>=', $this->minRides);
|
||||
})
|
||||
->when($this->minCoasters, function (Builder $query) {
|
||||
$query->where('coaster_count', '>=', $this->minCoasters);
|
||||
})
|
||||
->paginate(10);
|
||||
}
|
||||
|
||||
protected function hasActiveFilters(): bool
|
||||
{
|
||||
return !empty($this->search)
|
||||
|| !empty($this->location)
|
||||
|| !empty($this->minRating)
|
||||
|| !empty($this->maxRating)
|
||||
|| !empty($this->minRides)
|
||||
|| !empty($this->minCoasters);
|
||||
}
|
||||
}
|
||||
@@ -1,20 +1,30 @@
|
||||
# Active Development Context
|
||||
|
||||
## Current Task
|
||||
Implementing ride review components (✅ Completed)
|
||||
Implementing search functionality (✅ Completed)
|
||||
|
||||
## Recent Changes
|
||||
1. Implemented review components:
|
||||
- ✅ RideReviewComponent for submitting reviews
|
||||
- ✅ RideReviewListComponent for displaying reviews
|
||||
- ✅ ReviewModerationComponent for moderation
|
||||
- ✅ Updated Memory Bank documentation
|
||||
- ✅ Committed changes with comprehensive commit message
|
||||
1. Implemented search functionality:
|
||||
- ✅ Created SearchComponent with real-time filtering
|
||||
- ✅ Implemented responsive search UI with filters sidebar
|
||||
- ✅ Added park cards with dynamic content
|
||||
- ✅ Integrated dark mode support
|
||||
- ✅ Added pagination and URL state management
|
||||
- ✅ Created comprehensive documentation in SearchImplementation.md
|
||||
|
||||
## Progress Summary
|
||||
|
||||
### Completed Tasks
|
||||
1. Static Assets Migration
|
||||
1. Search Implementation
|
||||
- Created SearchComponent with real-time filtering
|
||||
- Implemented responsive search UI with filters sidebar
|
||||
- Added park cards with dynamic content
|
||||
- Integrated dark mode support
|
||||
- Added pagination and URL state management
|
||||
- Created comprehensive documentation
|
||||
- See `memory-bank/features/SearchImplementation.md` for details
|
||||
|
||||
2. Static Assets Migration
|
||||
- Created directory structure for images, CSS, and JavaScript
|
||||
- Copied images from Django project
|
||||
- Migrated JavaScript modules
|
||||
@@ -58,8 +68,23 @@ Implementing ride review components (✅ Completed)
|
||||
- User menu
|
||||
- Auth menu
|
||||
- Park list with filtering and view modes
|
||||
- Search with real-time filtering
|
||||
- Review system with moderation
|
||||
- Ride management and details
|
||||
|
||||
### Next Steps
|
||||
1. Filament Admin Implementation
|
||||
- Create admin panel for parks management
|
||||
- Implement CRUD operations using Filament resources
|
||||
- Set up role-based access control
|
||||
- Add audit trails for admin actions
|
||||
- See `memory-bank/features/FilamentIntegration.md` for details
|
||||
|
||||
2. Analytics Integration
|
||||
- Implement analytics tracking
|
||||
- Create statistics dashboard
|
||||
- Add reporting features
|
||||
- Set up data aggregation
|
||||
1. ✅ Park Model Enhancements
|
||||
- ✅ Implemented Photo model and relationship
|
||||
- ✅ Added getBySlug method for historical slug support
|
||||
|
||||
139
memory-bank/features/SearchImplementation.md
Normal file
139
memory-bank/features/SearchImplementation.md
Normal file
@@ -0,0 +1,139 @@
|
||||
# Search Implementation
|
||||
|
||||
## Overview
|
||||
The search functionality has been migrated from Django to Laravel/Livewire while maintaining feature parity and improving the user experience with real-time filtering.
|
||||
|
||||
## Key Components
|
||||
|
||||
### SearchComponent (app/Livewire/SearchComponent.php)
|
||||
- Handles search and filtering logic
|
||||
- Uses Livewire's real-time search capabilities
|
||||
- Maintains query parameters in URL
|
||||
- Implements pagination for results
|
||||
|
||||
#### Filter Properties
|
||||
- `search`: Text search across name and description
|
||||
- `location`: Location-based filtering
|
||||
- `minRating` and `maxRating`: Rating range filtering
|
||||
- `minRides`: Minimum number of rides filter
|
||||
- `minCoasters`: Minimum number of coasters filter
|
||||
|
||||
#### Features
|
||||
- Real-time filtering with `wire:model.live`
|
||||
- URL query string synchronization
|
||||
- Eager loading of relationships for performance
|
||||
- Responsive pagination
|
||||
- Filter state management
|
||||
|
||||
### View Implementation (resources/views/livewire/search.blade.php)
|
||||
- Responsive layout with filters sidebar
|
||||
- Real-time updates without page reload
|
||||
- Dark mode support
|
||||
- Accessible form controls
|
||||
- Mobile-first design
|
||||
|
||||
#### UI Components
|
||||
1. Filters Sidebar
|
||||
- Search input
|
||||
- Location filter
|
||||
- Rating range inputs
|
||||
- Ride count filters
|
||||
- Clear filters button
|
||||
|
||||
2. Results Section
|
||||
- Results count display
|
||||
- Park cards with:
|
||||
* Featured image
|
||||
* Park name and location
|
||||
* Rating badge
|
||||
* Status indicator
|
||||
* Ride/coaster counts
|
||||
* Description preview
|
||||
|
||||
## Differences from Django Implementation
|
||||
|
||||
### Improvements
|
||||
1. Real-time Updates
|
||||
- Replaced HTMX with Livewire's native reactivity
|
||||
- Instant filtering without page reloads
|
||||
- Smoother user experience
|
||||
|
||||
2. State Management
|
||||
- URL query parameters for shareable searches
|
||||
- Persistent filter state during navigation
|
||||
- Clear filters functionality
|
||||
|
||||
3. Performance
|
||||
- Eager loading of relationships
|
||||
- Efficient query building
|
||||
- Optimized view rendering
|
||||
|
||||
### Feature Parity
|
||||
- Maintained all Django filtering capabilities
|
||||
- Preserved UI/UX patterns
|
||||
- Kept identical data presentation
|
||||
- Matched search algorithm functionality
|
||||
|
||||
## Technical Details
|
||||
|
||||
### Query Building
|
||||
```php
|
||||
protected function getFilteredParks()
|
||||
{
|
||||
return Park::query()
|
||||
->select('parks.*')
|
||||
->with(['location', 'photos'])
|
||||
->when($this->search, function (Builder $query) {
|
||||
$query->where(function (Builder $query) {
|
||||
$query->where('name', 'like', "%{$this->search}%")
|
||||
->orWhere('description', 'like', "%{$this->search}%");
|
||||
});
|
||||
})
|
||||
// Additional filter conditions...
|
||||
->paginate(10);
|
||||
}
|
||||
```
|
||||
|
||||
### Filter State Management
|
||||
```php
|
||||
protected $queryString = [
|
||||
'search' => ['except' => ''],
|
||||
'location' => ['except' => ''],
|
||||
'minRating' => ['except' => ''],
|
||||
'maxRating' => ['except' => ''],
|
||||
'minRides' => ['except' => ''],
|
||||
'minCoasters' => ['except' => '']
|
||||
];
|
||||
```
|
||||
|
||||
## Testing Considerations
|
||||
1. Filter Combinations
|
||||
- Test various filter combinations
|
||||
- Verify result accuracy
|
||||
- Check edge cases
|
||||
|
||||
2. Performance Testing
|
||||
- Large result sets
|
||||
- Multiple concurrent users
|
||||
- Query optimization
|
||||
|
||||
3. UI Testing
|
||||
- Mobile responsiveness
|
||||
- Dark mode functionality
|
||||
- Accessibility compliance
|
||||
|
||||
## Future Enhancements
|
||||
1. Advanced Filters
|
||||
- Date range filtering
|
||||
- Category filtering
|
||||
- Geographic radius search
|
||||
|
||||
2. Performance Optimizations
|
||||
- Result caching
|
||||
- Lazy loading options
|
||||
- Query optimization
|
||||
|
||||
3. UI Improvements
|
||||
- Save search preferences
|
||||
- Filter presets
|
||||
- Advanced sorting options
|
||||
189
resources/views/livewire/search.blade.php
Normal file
189
resources/views/livewire/search.blade.php
Normal file
@@ -0,0 +1,189 @@
|
||||
<div class="container mx-auto px-4 py-8">
|
||||
<div class="flex flex-col lg:flex-row gap-8">
|
||||
<!-- Filters Sidebar -->
|
||||
<div class="lg:w-1/4">
|
||||
<div class="bg-white dark:bg-gray-800 p-6 rounded-lg shadow">
|
||||
<h2 class="text-xl font-bold mb-4 dark:text-white">Filter Parks</h2>
|
||||
<div class="space-y-4">
|
||||
<!-- Search -->
|
||||
<div class="flex flex-col">
|
||||
<label for="search" class="text-sm font-medium text-gray-700 dark:text-gray-300">
|
||||
Search
|
||||
</label>
|
||||
<div class="mt-1">
|
||||
<input type="text"
|
||||
wire:model.live="search"
|
||||
id="search"
|
||||
class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white"
|
||||
placeholder="Search parks...">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Location -->
|
||||
<div class="flex flex-col">
|
||||
<label for="location" class="text-sm font-medium text-gray-700 dark:text-gray-300">
|
||||
Location
|
||||
</label>
|
||||
<div class="mt-1">
|
||||
<input type="text"
|
||||
wire:model.live="location"
|
||||
id="location"
|
||||
class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white"
|
||||
placeholder="Filter by location...">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Rating Range -->
|
||||
<div class="flex flex-col">
|
||||
<label class="text-sm font-medium text-gray-700 dark:text-gray-300">Rating Range</label>
|
||||
<div class="flex gap-2 mt-1">
|
||||
<input type="number"
|
||||
wire:model.live="minRating"
|
||||
min="0"
|
||||
max="5"
|
||||
step="0.1"
|
||||
class="w-1/2 rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white"
|
||||
placeholder="Min">
|
||||
<input type="number"
|
||||
wire:model.live="maxRating"
|
||||
min="0"
|
||||
max="5"
|
||||
step="0.1"
|
||||
class="w-1/2 rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white"
|
||||
placeholder="Max">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Minimum Rides -->
|
||||
<div class="flex flex-col">
|
||||
<label for="minRides" class="text-sm font-medium text-gray-700 dark:text-gray-300">
|
||||
Minimum Rides
|
||||
</label>
|
||||
<div class="mt-1">
|
||||
<input type="number"
|
||||
wire:model.live="minRides"
|
||||
id="minRides"
|
||||
min="0"
|
||||
class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white"
|
||||
placeholder="Minimum number of rides">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Minimum Coasters -->
|
||||
<div class="flex flex-col">
|
||||
<label for="minCoasters" class="text-sm font-medium text-gray-700 dark:text-gray-300">
|
||||
Minimum Coasters
|
||||
</label>
|
||||
<div class="mt-1">
|
||||
<input type="number"
|
||||
wire:model.live="minCoasters"
|
||||
id="minCoasters"
|
||||
min="0"
|
||||
class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white"
|
||||
placeholder="Minimum number of coasters">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Filter Actions -->
|
||||
<div class="flex justify-between pt-2">
|
||||
@if($filtersApplied)
|
||||
<button wire:click="clearFilters"
|
||||
type="button"
|
||||
class="inline-flex justify-center py-2 px-4 border border-gray-300 shadow-sm text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white dark:hover:bg-gray-600">
|
||||
Clear Filters
|
||||
</button>
|
||||
@endif
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Results Section -->
|
||||
<div class="lg:w-3/4">
|
||||
<div class="bg-white dark:bg-gray-800 rounded-lg shadow">
|
||||
<div class="p-6 border-b border-gray-200 dark:border-gray-700">
|
||||
<div class="flex justify-between items-center">
|
||||
<h2 class="text-xl font-bold dark:text-white">
|
||||
Search Results
|
||||
<span class="text-sm font-normal text-gray-500 dark:text-gray-400">({{ $results->total() }} found)</span>
|
||||
</h2>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="divide-y divide-gray-200 dark:divide-gray-700">
|
||||
@forelse($results as $park)
|
||||
<div class="p-6 flex flex-col md:flex-row gap-4">
|
||||
<!-- Park Image -->
|
||||
<div class="md:w-48 h-32 bg-gray-200 dark:bg-gray-700 rounded-lg overflow-hidden">
|
||||
@if($park->photos->isNotEmpty())
|
||||
<img src="{{ $park->photos->first()->image_url }}"
|
||||
alt="{{ $park->name }}"
|
||||
class="w-full h-full object-cover">
|
||||
@else
|
||||
<div class="w-full h-full flex items-center justify-center text-gray-400 dark:text-gray-500">
|
||||
No Image
|
||||
</div>
|
||||
@endif
|
||||
</div>
|
||||
|
||||
<!-- Park Details -->
|
||||
<div class="flex-1">
|
||||
<h3 class="text-lg font-semibold">
|
||||
<a href="{{ route('parks.show', $park) }}" class="hover:text-blue-600 dark:text-white dark:hover:text-blue-400">
|
||||
{{ $park->name }}
|
||||
</a>
|
||||
</h3>
|
||||
|
||||
<div class="mt-2 text-sm text-gray-600 dark:text-gray-400">
|
||||
@if($park->formatted_location)
|
||||
<p>{{ $park->formatted_location }}</p>
|
||||
@endif
|
||||
</div>
|
||||
|
||||
<div class="mt-2 flex flex-wrap gap-2">
|
||||
@if($park->average_rating)
|
||||
<span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-800 dark:bg-green-800 dark:text-green-100">
|
||||
{{ number_format($park->average_rating, 1) }} ★
|
||||
</span>
|
||||
@endif
|
||||
|
||||
<span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-blue-100 text-blue-800 dark:bg-blue-800 dark:text-blue-100">
|
||||
{{ $park->status->label() }}
|
||||
</span>
|
||||
|
||||
@if($park->ride_count)
|
||||
<span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-purple-100 text-purple-800 dark:bg-purple-800 dark:text-purple-100">
|
||||
{{ $park->ride_count }} Rides
|
||||
</span>
|
||||
@endif
|
||||
|
||||
@if($park->coaster_count)
|
||||
<span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-yellow-100 text-yellow-800 dark:bg-yellow-800 dark:text-yellow-100">
|
||||
{{ $park->coaster_count }} Coasters
|
||||
</span>
|
||||
@endif
|
||||
</div>
|
||||
|
||||
@if($park->description)
|
||||
<p class="mt-2 text-sm text-gray-600 dark:text-gray-400 line-clamp-2">
|
||||
{{ $park->description }}
|
||||
</p>
|
||||
@endif
|
||||
</div>
|
||||
</div>
|
||||
@empty
|
||||
<div class="p-6 text-center text-gray-500 dark:text-gray-400">
|
||||
No parks found matching your criteria.
|
||||
</div>
|
||||
@endforelse
|
||||
</div>
|
||||
|
||||
@if($results->hasPages())
|
||||
<div class="px-6 py-4 border-t border-gray-200 dark:border-gray-700">
|
||||
{{ $results->links() }}
|
||||
</div>
|
||||
@endif
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -79,6 +79,4 @@ Route::get('/profile/{username}', function () {
|
||||
})->name('profile.show');
|
||||
|
||||
// Search route
|
||||
Route::get('/search', function () {
|
||||
return 'Search';
|
||||
})->name('search');
|
||||
Route::get('/search', \App\Livewire\SearchComponent::class)->name('search');
|
||||
|
||||
Reference in New Issue
Block a user