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:
1 parent
c1acc0067c
commit
f0a1b928dd
9 files changed
+217
-101
No files matched your search
@@ -0,0 +1,17 @@
|
||||
import { Controller } from '@hotwired/stimulus'
|
||||
|
||||
// Moves focus into an element as soon as it lands in the DOM: only rendered
|
||||
// on markup that arrives through a Turbo Stream (the review form's response),
|
||||
// never on a normal page load. Focuses the first invalid field if there is
|
||||
// one (or the first focusable thing inside it, e.g. the rating stars),
|
||||
// otherwise the element itself (give it tabindex="-1").
|
||||
export default class extends Controller {
|
||||
connect() {
|
||||
const invalid = this.element.querySelector('[aria-invalid="true"]')
|
||||
const target = invalid?.matches(FOCUSABLE) ? invalid : invalid?.querySelector(FOCUSABLE)
|
||||
|
||||
;(target ?? this.element).focus()
|
||||
}
|
||||
}
|
||||
|
||||
const FOCUSABLE = 'input:not([type="hidden"]), textarea, select, button, [tabindex]'
|
||||
@@ -10,6 +10,7 @@ import CartCountController from './cart-count-controller'
|
||||
import CarouselController from './carousel-controller'
|
||||
import CustomFieldUploadController from './custom-field-upload-controller'
|
||||
import DropdownController from './dropdown-controller'
|
||||
import FocusOnConnectController from './focus-on-connect-controller'
|
||||
import FrameScrollController from './frame-scroll-controller'
|
||||
import NavSearchController from './nav-search-controller'
|
||||
import ProductFormController from './product-form-controller'
|
||||
@@ -28,6 +29,7 @@ export function registerControllers(application) {
|
||||
application.register('carousel', CarouselController)
|
||||
application.register('custom-field-upload', CustomFieldUploadController)
|
||||
application.register('dropdown', DropdownController)
|
||||
application.register('focus-on-connect', FocusOnConnectController)
|
||||
application.register('frame-scroll', FrameScrollController)
|
||||
application.register('nav-search', NavSearchController)
|
||||
application.register('product-form', ProductFormController)
|
||||
|
||||
Reference in new issue
Block a user