Reviews: submit without a redirect, answered with Turbo Streams

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>
This commit is contained in:
elviraandClaude Opus 5.5 committed 2026-11-03 00:09:58 +02:00
1 parent c1acc0067c
commit f0a1b928dd
9 files changed
+217 -101

No files matched your search

@@ -1,28 +1,47 @@
@props(['product'])
{{--
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 $oldRating = (int) old('rating', 0); @endphp
@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' => $product['id']]) }}"
action="{{ route('product.reviews.store', ['locale' => app()->getLocale(), 'product' => $productId]) }}"
class="flex flex-col gap-8 mt-10"
data-controller="star-rating"
data-star-rating-rating-value="{{ $oldRating }}"
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="$errors->first('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"
aria-describedby="rating-client-error{{ $error('rating') ? ' rating-error' : '' }}"
@if ($error('rating')) aria-invalid="true" @endif
>
<input type="hidden" name="rating" value="{{ $oldRating }}" data-star-rating-target="input">
<input type="hidden" name="rating" value="{{ $rating }}" data-star-rating-target="input">
@for($i = 1; $i <= 5; $i++)
<button
type="button"
@@ -30,7 +49,7 @@ class="flex items-center gap-1.5 text-brand"
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 === $oldRating ? 'true' : 'false' }}"
aria-pressed="{{ $i === $rating ? 'true' : 'false' }}"
>
<svg
xmlns="http://www.w3.org/2000/svg"
@@ -49,16 +68,39 @@ class="w-6 h-6"
<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="$errors->first('content')">
<x-ui.textarea id="review-content" name="content" :required="true" :rows="6">{{ old('content') }}</x-ui.textarea>
<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="$errors->first('name')">
<x-ui.input id="review-name" name="name" autocomplete="name" :value="old('name')" />
<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="$errors->first('email')">
<x-ui.input id="review-email" name="email" type="email" :required="true" autocomplete="email" :value="old('email')" />
<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>
@@ -0,0 +1,12 @@
{{-- Average stars + review count under the product title. Shared by the
product page and the review-stored Turbo Stream (#product-rating-summary). --}}
<x-reviews-stars
id="product-rating-summary"
:rating="$rating ?? 0"
:count="$count"
:showCount="true"
:linkable="true"
data-controller="tab-link"
data-tab-link-tabs-outlet="#product-tabs"
data-tab-link-panel-value="reviews"
/>
@@ -0,0 +1,11 @@
{{-- One review, from its index-shaped array (ProductIndexer::mapReview()).
Shared by the product page's list and the review-stored Turbo Stream. --}}
<x-review-card :review="[
'rating' => $review['rating'],
'name' => $review['reviewer_name'],
'date' => $review['reviewed_at'] ? \Illuminate\Support\Carbon::createFromTimestamp($review['reviewed_at'])->translatedFormat('d M Y') : '',
'text' => $review['body'],
'image' => $review['media'][0]['url'] ?? null,
'reply' => $review['reply'] ?? null,
'replyDate' => ($review['replied_at'] ?? null) ? \Illuminate\Support\Carbon::createFromTimestamp($review['replied_at'])->translatedFormat('d M Y') : null,
]" />
+26 -34
View File
@@ -154,15 +154,10 @@ class="absolute bottom-8 left-1/2 -translate-x-1/2 text-white text-sm lg:left-au
{{ $product['name'] }}
</h1>
<x-reviews-stars
:rating="$product['reviews']['average_rating'] ?? 0"
:count="$product['reviews']['count']"
:showCount="true"
:linkable="true"
data-controller="tab-link"
data-tab-link-tabs-outlet="#product-tabs"
data-tab-link-panel-value="reviews"
/>
@include('product.partials.rating-summary', [
'rating' => $product['reviews']['average_rating'],
'count' => $product['reviews']['count'],
])
@if($product['price'] !== null)
<p class="text-2xl font-bold" data-product-form-target="price">
@@ -237,34 +232,31 @@ class="flex flex-col gap-6"
</div>
</x-slot>
<x-slot name="reviews">
@if(session('reviewSubmitted'))
<p class="mb-8 text-sm font-semibold" role="status">{{ __('storefront.review.thank_you') }}</p>
{{-- Stream targets for ProductController::storeReview(): the new
review is prepended to #product-reviews-list, the empty
message and #product-review-section removed, and the thank-you
put in the #product-review-status live region. The list hides
itself while it has no reviews. --}}
<div id="product-review-status" role="status"></div>
<div id="product-reviews-list" class="mb-10 [&:not(:has(>*))]:hidden">
@foreach($product['reviews']['items'] ?? [] as $review)
@include('product.partials.review', ['review' => $review])
@endforeach
</div>
@if(empty($product['reviews']['items']))
<p id="product-reviews-empty" class="text-neutral-500 mb-10">{{ __('storefront.review.no_reviews_yet') }}</p>
@endif
@if(!empty($product['reviews']['items']))
<div class="mb-10">
@foreach($product['reviews']['items'] as $review)
<x-review-card :review="[
'rating' => $review['rating'],
'name' => $review['reviewer_name'],
'date' => $review['reviewed_at'] ? \Illuminate\Support\Carbon::createFromTimestamp($review['reviewed_at'])->translatedFormat('d M Y') : '',
'text' => $review['body'],
'image' => $review['media'][0]['url'] ?? null,
'reply' => $review['reply'] ?? null,
'replyDate' => $review['replied_at'] ? \Illuminate\Support\Carbon::createFromTimestamp($review['replied_at'])->translatedFormat('d M Y') : null,
]" />
@endforeach
</div>
@else
<p class="text-neutral-500 mb-10">{{ __('storefront.review.no_reviews_yet') }}</p>
@endif
<div id="product-review-section">
<h3 class="text-h4 font-bold">
{{ $product['reviews']['count'] > 0 ? __('storefront.review.write_new') : __('storefront.review.write_first') }}
{{ __('storefront.review.for_product', ['name' => $product['name']]) }}
</h3>
<h3 class="text-h4 font-bold">
{{ $product['reviews']['count'] > 0 ? __('storefront.review.write_new') : __('storefront.review.write_first') }}
{{ __('storefront.review.for_product', ['name' => $product['name']]) }}
</h3>
<x-review-form :product="$product" />
<x-review-form :product-id="$product['id']" />
</div>
</x-slot>
</x-ui.tabs>
@@ -0,0 +1,8 @@
{{-- Turbo Stream answer to a review that failed validation
(ProductController::storeReview(), status 422): the form again, with
inline errors and the entered values, focus on the first invalid field. --}}
<turbo-stream action="replace" target="product-review-form">
<template>
<x-review-form :product-id="$productId" :messages="$messages" :values="$values" :focus="true" />
</template>
</turbo-stream>
@@ -0,0 +1,30 @@
{{-- Turbo Stream answer to a successful review submission
(ProductController::storeReview()). Targets live in product/show.blade.php. --}}
<turbo-stream action="prepend" target="product-reviews-list">
<template>
@include('product.partials.review', ['review' => $review])
</template>
</turbo-stream>
<turbo-stream action="remove" target="product-reviews-empty"></turbo-stream>
<turbo-stream action="replace" target="product-rating-summary">
<template>
@include('product.partials.rating-summary', ['rating' => $averageRating, 'count' => $count])
</template>
</turbo-stream>
<turbo-stream action="update" target="tab-btn-reviews">
<template>{{ __('storefront.product.reviews') . ' (' . $count . ')' }}</template>
</turbo-stream>
<turbo-stream action="remove" target="product-review-section"></turbo-stream>
{{-- Into the live region that's already on the page, so it's announced;
focus moves to it too, since the form (and the focused submit button)
just went away. --}}
<turbo-stream action="update" target="product-review-status">
<template>
<p class="mb-8 text-sm font-semibold" tabindex="-1" data-controller="focus-on-connect">{{ __('storefront.review.thank_you') }}</p>
</template>
</turbo-stream>