The review form now submits through Turbo (data-turbo="true"; Drive
stays off) and storeReview answers with Turbo Streams instead of a
redirect with flashed data:
- success: the new review is prepended to the list (same Blade partial
as the existing ones), the empty message is removed, the stars/count
under the title and the tab label are updated (count and average from
the DB, which is what the index builds them from), the form is removed
and the thank-you goes into a live region and gets focus
- validation error: 422 + a stream that replaces the form with inline
errors (aria-invalid/aria-describedby) and the entered values, focus on
the first invalid field
Nothing on the product page reads the session any more: no
session('reviewSubmitted'), old() or $errors, and
mergeJustSubmittedReview and the justSubmittedReview flash are gone.
searchableSync() stays.
Without JS it's a plain POST that redirects back to ?tab=reviews,
without inline errors or the thank-you, and the new review shows once
Meilisearch catches up. The ?tab=reviews query parameter is noted in
the controller as a variant to handle once product pages are cached.
New: product/partials/review, product/partials/rating-summary,
product/streams/review-stored|review-invalid, and a small
focus-on-connect Stimulus controller that only appears on streamed
markup.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
111 lines
5.4 KiB
PHP
111 lines
5.4 KiB
PHP
{{--
|
|
Review form. Submitted through Turbo (data-turbo="true", Drive stays off
|
|
elsewhere): ProductController::storeReview() answers with Turbo Streams.
|
|
On a validation error it replaces this form (#product-review-form) with a
|
|
copy rendered with `messages` and the entered `values`, and `focus` so
|
|
focus-on-connect moves focus to the first invalid field. Never reads the
|
|
session (no old() or $errors), so the product page stays cacheable.
|
|
--}}
|
|
@props([
|
|
'productId',
|
|
'messages' => null, // Illuminate\Support\MessageBag of validation errors
|
|
'values' => [],
|
|
'focus' => false,
|
|
])
|
|
|
|
@php
|
|
$rating = (int) ($values['rating'] ?? 0);
|
|
$error = fn (string $field) => $messages?->first($field);
|
|
@endphp
|
|
|
|
<form
|
|
id="product-review-form"
|
|
method="POST"
|
|
action="{{ route('product.reviews.store', ['locale' => app()->getLocale(), 'product' => $productId]) }}"
|
|
class="flex flex-col gap-8 mt-10"
|
|
data-turbo="true"
|
|
data-controller="star-rating{{ $focus ? ' focus-on-connect' : '' }}"
|
|
data-star-rating-rating-value="{{ $rating }}"
|
|
data-action="submit->star-rating#validate"
|
|
>
|
|
@csrf
|
|
|
|
{{-- Rating --}}
|
|
<x-ui.field :label="__('storefront.review.rating')" for="rating" :required="true" :error="$error('rating')">
|
|
<div
|
|
id="rating"
|
|
class="flex items-center gap-1.5 text-brand"
|
|
role="radiogroup"
|
|
aria-label="{{ __('storefront.review.rating') }}"
|
|
aria-required="true"
|
|
aria-describedby="rating-client-error{{ $error('rating') ? ' rating-error' : '' }}"
|
|
@if ($error('rating')) aria-invalid="true" @endif
|
|
>
|
|
<input type="hidden" name="rating" value="{{ $rating }}" data-star-rating-target="input">
|
|
@for($i = 1; $i <= 5; $i++)
|
|
<button
|
|
type="button"
|
|
data-star-rating-target="star"
|
|
data-value="{{ $i }}"
|
|
data-action="click->star-rating#select mouseenter->star-rating#hover mouseleave->star-rating#leave"
|
|
aria-label="{{ trans_choice('storefront.review.stars_count', $i, ['count' => $i]) }}"
|
|
aria-pressed="{{ $i === $rating ? 'true' : 'false' }}"
|
|
>
|
|
<svg
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
viewBox="0 0 23 21"
|
|
fill="none"
|
|
stroke="currentColor"
|
|
class="w-6 h-6"
|
|
aria-hidden="true"
|
|
>
|
|
<path d="m15.054 0 .689 4.894 5.057-.883-2.435 4.348L23 10.5l-4.635 2.141 2.435 4.348-5.061-.883L15.054 21 11.5 17.43 7.946 21l-.689-4.894-5.057.883 2.438-4.348L0 10.5l4.635-2.141L2.2 4.011l5.061.883L7.946 0 11.5 3.57Z"/>
|
|
<path d="m14.34389687 2.13050652-2.84388733 2.85695266-2.84390736-2.85695076-.55294228 3.92571163-4.0049181-.69867801 1.93777848 3.45510769L2.38412858 10.5l3.6518917 1.68735027-1.9377842 3.4551077 4.00493431-.69867802.55292607 3.92570781L11.5 16.01253891l2.84389782 2.85694885.55294228-3.92570781 4.00492382.69867801-1.9377842-3.45510769L20.61587142 10.5l-3.6518917-1.68735027 1.9377842-3.4551077-4.00492382.69867802-.55294323-3.92571353M7.94631004 0l3.5536995 3.5699997L15.05368996 0l.68924999 4.89351082 5.06075954-.88287163-2.43848037 4.34787083L23 10.5l-4.63478088 2.14148998 2.43848037 4.34787083-5.06075954-.88286972L15.05368995 21 11.5 17.4300003 7.94629955 21l-.6892395-4.89350891-5.06075954.88286972 2.43848037-4.34787083L0 10.5l4.63478088-2.14148998-2.43847084-4.34787083 5.06075001.88287163L7.94631005 0Z"/>
|
|
</svg>
|
|
</button>
|
|
@endfor
|
|
</div>
|
|
<p id="rating-client-error" data-star-rating-target="error" class="text-sm text-red-600" role="alert" hidden>{{ __('storefront.review.rating_required') }}</p>
|
|
</x-ui.field>
|
|
|
|
<x-ui.field :label="__('storefront.review.write_label')" for="review-content" :required="true" :error="$error('content')">
|
|
<x-ui.textarea
|
|
id="review-content"
|
|
name="content"
|
|
:required="true"
|
|
:rows="6"
|
|
:aria-invalid="$error('content') ? 'true' : null"
|
|
:aria-describedby="$error('content') ? 'review-content-error' : null"
|
|
>{{ $values['content'] ?? '' }}</x-ui.textarea>
|
|
</x-ui.field>
|
|
|
|
<x-ui.field :label="__('storefront.review.name')" for="review-name" :labelDescription="__('storefront.review.name_optional')" :error="$error('name')">
|
|
<x-ui.input
|
|
id="review-name"
|
|
name="name"
|
|
autocomplete="name"
|
|
:value="$values['name'] ?? null"
|
|
:aria-invalid="$error('name') ? 'true' : null"
|
|
:aria-describedby="$error('name') ? 'review-name-error' : null"
|
|
/>
|
|
</x-ui.field>
|
|
|
|
<x-ui.field :label="__('storefront.review.email')" for="review-email" :required="true" :labelDescription="__('storefront.review.email_not_published')" :error="$error('email')">
|
|
<x-ui.input
|
|
id="review-email"
|
|
name="email"
|
|
type="email"
|
|
:required="true"
|
|
autocomplete="email"
|
|
:value="$values['email'] ?? null"
|
|
:aria-invalid="$error('email') ? 'true' : null"
|
|
:aria-describedby="$error('email') ? 'review-email-error' : null"
|
|
/>
|
|
</x-ui.field>
|
|
|
|
<div>
|
|
<x-ui.button type="submit">{{ __('storefront.review.submit') }}</x-ui.button>
|
|
</div>
|
|
|
|
</form>
|