feat: implement ride review components

- Add RideReviewComponent for submitting reviews
  - Star rating input with real-time validation
  - Rate limiting and anti-spam measures
  - Edit capabilities for own reviews

- Add RideReviewListComponent for displaying reviews
  - Paginated list with sort/filter options
  - Helpful vote functionality
  - Statistics display with rating distribution

- Add ReviewModerationComponent for review management
  - Review queue with status filters
  - Approve/reject functionality
  - Batch actions support
  - Edit capabilities

- Update Memory Bank documentation
  - Document component implementations
  - Track feature completion
  - Update technical decisions
This commit is contained in:
pacnpal
2025-02-25 21:59:22 -05:00
parent 4e06f7313e
commit 487c0e5866
8 changed files with 1406 additions and 41 deletions

View File

@@ -0,0 +1,227 @@
<?php
namespace App\Livewire;
use App\Models\Review;
use App\Enums\ReviewStatus;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
use Livewire\WithPagination;
class ReviewModerationComponent extends Component
{
use WithPagination;
/**
* Current filter status
*/
public ?string $statusFilter = null;
/**
* Search query
*/
public string $search = '';
/**
* Selected reviews for batch actions
*/
public array $selected = [];
/**
* Whether to show the edit modal
*/
public bool $showEditModal = false;
/**
* Review being edited
*/
public ?Review $editingReview = null;
/**
* Form fields for editing
*/
public array $form = [
'rating' => null,
'title' => null,
'content' => null,
];
/**
* Success/error message
*/
public ?string $message = null;
/**
* Validation rules
*/
protected function rules()
{
return [
'form.rating' => 'required|integer|min:1|max:5',
'form.title' => 'nullable|string|max:100',
'form.content' => 'required|string|min:10|max:2000',
];
}
/**
* Mount the component
*/
public function mount()
{
$this->statusFilter = ReviewStatus::PENDING->value;
}
/**
* Filter by status
*/
public function filterByStatus(?string $status)
{
$this->statusFilter = $status;
$this->resetPage();
}
/**
* Show edit modal for a review
*/
public function editReview(Review $review)
{
$this->editingReview = $review;
$this->form = [
'rating' => $review->rating,
'title' => $review->title,
'content' => $review->content,
];
$this->showEditModal = true;
}
/**
* Save edited review
*/
public function saveEdit()
{
$this->validate();
try {
$this->editingReview->update([
'rating' => $this->form['rating'],
'title' => $this->form['title'],
'content' => $this->form['content'],
'moderated_at' => now(),
'moderated_by' => Auth::id(),
]);
$this->message = 'Review updated successfully.';
$this->showEditModal = false;
$this->reset(['editingReview', 'form']);
} catch (\Exception $e) {
$this->message = 'An error occurred while updating the review.';
}
}
/**
* Approve a review
*/
public function approve(Review $review)
{
try {
$review->update([
'status' => ReviewStatus::APPROVED,
'moderated_at' => now(),
'moderated_by' => Auth::id(),
]);
$this->message = 'Review approved successfully.';
} catch (\Exception $e) {
$this->message = 'An error occurred while approving the review.';
}
}
/**
* Reject a review
*/
public function reject(Review $review)
{
try {
$review->update([
'status' => ReviewStatus::REJECTED,
'moderated_at' => now(),
'moderated_by' => Auth::id(),
]);
$this->message = 'Review rejected successfully.';
} catch (\Exception $e) {
$this->message = 'An error occurred while rejecting the review.';
}
}
/**
* Batch approve selected reviews
*/
public function batchApprove()
{
try {
Review::whereIn('id', $this->selected)->update([
'status' => ReviewStatus::APPROVED,
'moderated_at' => now(),
'moderated_by' => Auth::id(),
]);
$this->message = count($this->selected) . ' reviews approved successfully.';
$this->selected = [];
} catch (\Exception $e) {
$this->message = 'An error occurred while approving the reviews.';
}
}
/**
* Batch reject selected reviews
*/
public function batchReject()
{
try {
Review::whereIn('id', $this->selected)->update([
'status' => ReviewStatus::REJECTED,
'moderated_at' => now(),
'moderated_by' => Auth::id(),
]);
$this->message = count($this->selected) . ' reviews rejected successfully.';
$this->selected = [];
} catch (\Exception $e) {
$this->message = 'An error occurred while rejecting the reviews.';
}
}
/**
* Get the reviews query
*/
protected function getReviewsQuery()
{
$query = Review::with(['user', 'ride'])
->when($this->statusFilter, function ($query, $status) {
$query->where('status', $status);
})
->when($this->search, function ($query, $search) {
$query->where(function ($query) use ($search) {
$query->where('title', 'like', "%{$search}%")
->orWhere('content', 'like', "%{$search}%")
->orWhereHas('user', function ($query) use ($search) {
$query->where('name', 'like', "%{$search}%");
})
->orWhereHas('ride', function ($query) use ($search) {
$query->where('name', 'like', "%{$search}%");
});
});
})
->orderBy('created_at', 'desc');
return $query;
}
/**
* Render the component
*/
public function render()
{
return view('livewire.review-moderation-component', [
'reviews' => $this->getReviewsQuery()->paginate(10),
'totalPending' => Review::where('status', ReviewStatus::PENDING)->count(),
]);
}
}

View File

@@ -0,0 +1,151 @@
<?php
namespace App\Livewire;
use App\Models\Review;
use App\Models\Ride;
use App\Enums\ReviewStatus;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\RateLimiter;
use Livewire\Component;
use Livewire\Attributes\Rule;
class RideReviewComponent extends Component
{
/**
* The ride being reviewed
*/
public Ride $ride;
/**
* The review being edited (if in edit mode)
*/
public ?Review $review = null;
/**
* Form fields
*/
#[Rule('required|integer|min:1|max:5')]
public int $rating = 3;
#[Rule('nullable|string|max:100')]
public ?string $title = null;
#[Rule('required|string|min:10|max:2000')]
public string $content = '';
/**
* Whether the component is in edit mode
*/
public bool $isEditing = false;
/**
* Success/error message
*/
public ?string $message = null;
/**
* Mount the component
*/
public function mount(Ride $ride, ?Review $review = null)
{
$this->ride = $ride;
if ($review) {
$this->review = $review;
$this->isEditing = true;
$this->rating = $review->rating;
$this->title = $review->title;
$this->content = $review->content;
}
}
/**
* Save or update the review
*/
public function save()
{
// Check if user is authenticated
if (!Auth::check()) {
$this->message = 'You must be logged in to submit a review.';
return;
}
// Rate limiting
$key = 'review_' . Auth::id();
if (RateLimiter::tooManyAttempts($key, 5)) { // 5 attempts per minute
$this->message = 'Please wait before submitting another review.';
return;
}
RateLimiter::hit($key);
// Validate input
$this->validate();
try {
if ($this->isEditing) {
// Check if user can edit this review
if (!$this->review || $this->review->user_id !== Auth::id()) {
$this->message = 'You cannot edit this review.';
return;
}
// Update existing review
$this->review->update([
'rating' => $this->rating,
'title' => $this->title,
'content' => $this->content,
'status' => ReviewStatus::PENDING,
]);
$this->message = 'Review updated successfully. It will be visible after moderation.';
} else {
// Check if user already reviewed this ride
if (!$this->ride->canBeReviewedBy(Auth::user())) {
$this->message = 'You have already reviewed this ride.';
return;
}
// Create new review
Review::create([
'ride_id' => $this->ride->id,
'user_id' => Auth::id(),
'rating' => $this->rating,
'title' => $this->title,
'content' => $this->content,
'status' => ReviewStatus::PENDING,
]);
$this->message = 'Review submitted successfully. It will be visible after moderation.';
// Reset form
$this->reset(['rating', 'title', 'content']);
}
$this->dispatch('review-saved');
} catch (\Exception $e) {
$this->message = 'An error occurred while saving your review. Please try again.';
}
}
/**
* Reset the form
*/
public function resetForm()
{
$this->reset(['rating', 'title', 'content', 'message']);
if ($this->review) {
$this->rating = $this->review->rating;
$this->title = $this->review->title;
$this->content = $this->review->content;
}
}
/**
* Render the component
*/
public function render()
{
return view('livewire.ride-review-component');
}
}

View File

@@ -0,0 +1,169 @@
<?php
namespace App\Livewire;
use App\Models\Review;
use App\Models\Ride;
use App\Models\HelpfulVote;
use App\Enums\ReviewStatus;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\RateLimiter;
use Livewire\Component;
use Livewire\WithPagination;
class RideReviewListComponent extends Component
{
use WithPagination;
/**
* The ride whose reviews are being displayed
*/
public Ride $ride;
/**
* Current sort field
*/
public string $sortField = 'created_at';
/**
* Current sort direction
*/
public string $sortDirection = 'desc';
/**
* Rating filter
*/
public ?int $ratingFilter = null;
/**
* Success/error message
*/
public ?string $message = null;
/**
* Whether to show the statistics panel
*/
public bool $showStats = true;
/**
* Listeners for events
*/
protected $listeners = [
'review-saved' => '$refresh',
];
/**
* Mount the component
*/
public function mount(Ride $ride)
{
$this->ride = $ride;
}
/**
* Toggle sort field
*/
public function sortBy(string $field)
{
if ($this->sortField === $field) {
$this->sortDirection = $this->sortDirection === 'asc' ? 'desc' : 'asc';
} else {
$this->sortField = $field;
$this->sortDirection = 'desc';
}
}
/**
* Filter by rating
*/
public function filterByRating(?int $rating)
{
$this->ratingFilter = $rating === $this->ratingFilter ? null : $rating;
$this->resetPage();
}
/**
* Toggle helpful vote
*/
public function toggleHelpfulVote(Review $review)
{
if (!Auth::check()) {
$this->message = 'You must be logged in to vote on reviews.';
return;
}
// Rate limiting
$key = 'vote_' . Auth::id();
if (RateLimiter::tooManyAttempts($key, 10)) { // 10 attempts per minute
$this->message = 'Please wait before voting again.';
return;
}
RateLimiter::hit($key);
try {
HelpfulVote::toggle($review->id, Auth::id());
$this->message = 'Vote recorded successfully.';
} catch (\Exception $e) {
$this->message = 'An error occurred while recording your vote.';
}
}
/**
* Toggle statistics panel
*/
public function toggleStats()
{
$this->showStats = !$this->showStats;
}
/**
* Get review statistics
*/
public function getStatistics()
{
$reviews = $this->ride->reviews()->approved();
return [
'total' => $reviews->count(),
'average' => round($reviews->avg('rating'), 1),
'distribution' => [
5 => $reviews->where('rating', 5)->count(),
4 => $reviews->where('rating', 4)->count(),
3 => $reviews->where('rating', 3)->count(),
2 => $reviews->where('rating', 2)->count(),
1 => $reviews->where('rating', 1)->count(),
],
];
}
/**
* Get the reviews query
*/
protected function getReviewsQuery()
{
$query = $this->ride->reviews()
->with(['user', 'helpfulVotes'])
->approved();
// Apply rating filter
if ($this->ratingFilter) {
$query->where('rating', $this->ratingFilter);
}
// Apply sorting
$query->orderBy($this->sortField, $this->sortDirection);
return $query;
}
/**
* Render the component
*/
public function render()
{
return view('livewire.ride-review-list-component', [
'reviews' => $this->getReviewsQuery()->paginate(10),
'statistics' => $this->getStatistics(),
]);
}
}