generated from boboko/starter
Feat: Moving File Handling To Core
This commit is contained in:
@@ -2,13 +2,13 @@ import { Controller } from '@hotwired/stimulus'
|
||||
|
||||
// One product custom field of type `file` (see x-product-custom-fields).
|
||||
// Uploads the photo to the storefront's own endpoint (CustomFieldUploadController)
|
||||
// as soon as it's picked, then writes the returned opaque reference into the
|
||||
// hidden input the add-to-cart form actually submits — the checkout module
|
||||
// never receives the file itself.
|
||||
// as soon as it's picked, then writes the returned File row's id (boboko-core's
|
||||
// Modules\Core\File\Models\File) into the hidden input the add-to-cart form
|
||||
// actually submits — the checkout module never receives the file itself.
|
||||
//
|
||||
// While uploading, the file input is marked invalid via setCustomValidity(),
|
||||
// so the browser's own form validation blocks add-to-cart until the reference
|
||||
// is in place. A failed upload clears the input, so `required` blocks it too.
|
||||
// so the browser's own form validation blocks add-to-cart until the id is in
|
||||
// place. A failed upload clears the input, so `required` blocks it too.
|
||||
export default class extends Controller {
|
||||
static targets = ['file', 'reference', 'preview', 'error']
|
||||
static values = {
|
||||
@@ -52,12 +52,12 @@ export default class extends Controller {
|
||||
})
|
||||
const data = await response.json().catch(() => null)
|
||||
|
||||
if (!response.ok || !data?.reference) {
|
||||
if (!response.ok || !data?.file_id) {
|
||||
this.fail(data?.error)
|
||||
return
|
||||
}
|
||||
|
||||
this.referenceTarget.value = data.reference
|
||||
this.referenceTarget.value = data.file_id
|
||||
this.showPreview(file)
|
||||
} catch (error) {
|
||||
// A newer pick superseded this upload — reset() already handled it.
|
||||
|
||||
@@ -2,10 +2,12 @@
|
||||
@include('checkout::partials.line-custom-fields', ['line' => $line])
|
||||
|
||||
A cart or order line's custom-field answers (meta.custom_fields, written by
|
||||
CartController::customFieldsMeta()) — label/value pairs. A file answer links
|
||||
to the file through a temporary signed URL (CustomFieldFileController),
|
||||
minted fresh on every render, with a thumbnail when the browser can display
|
||||
the format (HEIC can't be shown outside Safari, so it gets the name only).
|
||||
CartController::customFieldsMeta()). A file answer only carries a File id
|
||||
(Modules\Core\File\Models\File is the source of truth for name/mime/disk/
|
||||
path — never duplicated into meta), resolved here and linked through
|
||||
boboko-core's own signed download route (files.download), minted fresh on
|
||||
every render, with a thumbnail when the browser can display the format
|
||||
(HEIC can't be shown outside Safari, so it gets the name only).
|
||||
--}}
|
||||
@php
|
||||
$fields = $line->meta['custom_fields'] ?? [];
|
||||
@@ -20,18 +22,23 @@
|
||||
<dd>
|
||||
@if ($field['type'] === 'file')
|
||||
@php
|
||||
$fileUrl = \Illuminate\Support\Facades\URL::temporarySignedRoute(
|
||||
'checkout.custom-field-file',
|
||||
now()->addHours(2),
|
||||
['locale' => app()->getLocale(), 'disk' => $field['disk'], 'path' => $field['path']],
|
||||
);
|
||||
$file = \Modules\Core\File\Models\File::find($field['file_id'] ?? null);
|
||||
@endphp
|
||||
<a href="{{ $fileUrl }}" class="bbk-line-field-file" target="_blank" rel="noopener">
|
||||
@if (in_array($field['mime'] ?? null, $previewable, true))
|
||||
<img src="{{ $fileUrl }}" alt="" width="40" height="40" loading="lazy">
|
||||
@endif
|
||||
<span>{{ $field['value'] }}</span>
|
||||
</a>
|
||||
@if ($file)
|
||||
@php
|
||||
$fileUrl = \Illuminate\Support\Facades\URL::temporarySignedRoute(
|
||||
'files.download',
|
||||
now()->addHours(2),
|
||||
['file' => $file->id],
|
||||
);
|
||||
@endphp
|
||||
<a href="{{ $fileUrl }}" class="bbk-line-field-file" target="_blank" rel="noopener">
|
||||
@if (in_array($file->mime, $previewable, true))
|
||||
<img src="{{ $fileUrl }}" alt="" width="40" height="40" loading="lazy">
|
||||
@endif
|
||||
<span>{{ $file->original_name }}</span>
|
||||
</a>
|
||||
@endif
|
||||
@else
|
||||
{{ $field['value'] }}
|
||||
@endif
|
||||
|
||||
@@ -6,10 +6,11 @@
|
||||
custom_fields[key]. text → input, textarea → textarea, file → photo upload.
|
||||
|
||||
A photo is uploaded as soon as it's picked (custom-field-upload-controller.js)
|
||||
and only the returned reference is submitted with the form — the file input
|
||||
itself has no name. It still carries `required`, so the browser's own
|
||||
validation blocks add-to-cart until a photo is picked, and the controller
|
||||
marks it invalid (setCustomValidity) while the upload is in flight.
|
||||
and only the resulting File row's id (boboko-core's Modules\Core\File\
|
||||
Models\File) is submitted with the form — the file input itself has no
|
||||
name. It still carries `required`, so the browser's own validation blocks
|
||||
add-to-cart until a photo is picked, and the controller marks it invalid
|
||||
(setCustomValidity) while the upload is in flight.
|
||||
--}}
|
||||
@props(['fields' => []])
|
||||
|
||||
|
||||
Reference in New Issue
Block a user