Files

110 lines
5.4 KiB
PHP
Raw Permalink Normal View History

{{--
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,
])
2026-07-31 17:41:55 +03:00
@php
$rating = (int) ($values['rating'] ?? 0);
$error = fn (string $field) => $messages?->first($field);
@endphp
2026-07-31 17:41:55 +03:00
<form
id="product-review-form"
2026-07-31 17:41:55 +03:00
method="POST"
action="{{ route('product.reviews.store', ['locale' => app()->getLocale(), 'product' => $productId]) }}"
2026-07-31 17:41:55 +03:00
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"
2026-07-31 17:41:55 +03:00
>
@csrf
{{-- Rating --}}
<x-ui.field :label="__('storefront.review.rating')" for="rating" :required="true" :error="$error('rating')">
2026-07-31 17:41:55 +03:00
<div
id="rating"
class="flex items-center gap-1.5 text-brand"
role="radiogroup"
aria-label="{{ __('storefront.review.rating') }}"
2026-07-31 17:41:55 +03:00
aria-required="true"
aria-describedby="rating-client-error{{ $error('rating') ? ' rating-error' : '' }}"
@if ($error('rating')) aria-invalid="true" @endif
2026-07-31 17:41:55 +03:00
>
<input type="hidden" name="rating" value="{{ $rating }}" data-star-rating-target="input">
2026-07-31 17:41:55 +03:00
@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' }}"
2026-07-31 17:41:55 +03:00
>
<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>
2026-07-31 17:41:55 +03:00
</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>
2026-07-31 17:41:55 +03:00
</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"
/>
2026-07-31 17:41:55 +03:00
</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"
/>
2026-07-31 17:41:55 +03:00
</x-ui.field>
<div>
<x-ui.button type="submit">{{ __('storefront.review.submit') }}</x-ui.button>
2026-07-31 17:41:55 +03:00
</div>
</form>