From faeef9fa2b2d7d0892c29bd03e159cad6d1faa56 Mon Sep 17 00:00:00 2001
From: Sam Poyigi <6567634+sampoyigi@users.noreply.github.com>
Date: Sun, 22 Sep 2024 15:45:10 +0100
Subject: [PATCH] refactor: use checkout form builder and add preview mode to
fulfilment components
Signed-off-by: Sam Poyigi <6567634+sampoyigi@users.noreply.github.com>
---
resources/js/checkout.js | 4 +-
resources/js/fulfillment.js | 2 +-
resources/lang/en/default.php | 6 +
resources/models/checkoutfields.php | 74 ++++++
.../components/forms/telephone.blade.php | 1 -
.../views/components/fulfillment.blade.php | 14 +-
.../checkout/address-fields.blade.php | 129 -----------
.../includes/checkout/comment-field.blade.php | 16 --
.../checkout/customer-fields.blade.php | 58 -----
.../checkout/delivery-address.blade.php | 14 ++
.../checkout/delivery-comment-field.blade.php | 15 --
.../checkout/field-checkbox.blade.php | 33 +++
.../includes/checkout/field-email.blade.php | 20 ++
...nts.blade.php => field-payments.blade.php} | 10 +-
.../checkout/field-telephone.blade.php | 11 +
.../includes/checkout/field-text.blade.php | 20 ++
.../checkout/field-textarea.blade.php | 19 ++
.../includes/checkout/one-step-form.blade.php | 21 +-
.../includes/checkout/tab-fields.blade.php | 10 +
.../includes/checkout/terms-field.blade.php | 19 --
.../includes/checkout/two-step-form.blade.php | 29 ++-
.../views/includes/local/timeslot.blade.php | 2 +
resources/views/livewire/checkout.blade.php | 2 +-
.../livewire/fulfillment-modal.blade.php | 17 +-
src/Livewire/Checkout.php | 212 ++++++++----------
src/Livewire/Forms/CheckoutForm.php | 92 --------
src/Livewire/FulfillmentModal.php | 17 ++
src/View/Components/Fulfillment.php | 14 ++
28 files changed, 394 insertions(+), 487 deletions(-)
create mode 100644 resources/models/checkoutfields.php
delete mode 100644 resources/views/includes/checkout/address-fields.blade.php
delete mode 100644 resources/views/includes/checkout/comment-field.blade.php
delete mode 100644 resources/views/includes/checkout/customer-fields.blade.php
create mode 100644 resources/views/includes/checkout/delivery-address.blade.php
delete mode 100644 resources/views/includes/checkout/delivery-comment-field.blade.php
create mode 100644 resources/views/includes/checkout/field-checkbox.blade.php
create mode 100644 resources/views/includes/checkout/field-email.blade.php
rename resources/views/includes/checkout/{payments.blade.php => field-payments.blade.php} (90%)
create mode 100644 resources/views/includes/checkout/field-telephone.blade.php
create mode 100644 resources/views/includes/checkout/field-text.blade.php
create mode 100644 resources/views/includes/checkout/field-textarea.blade.php
create mode 100644 resources/views/includes/checkout/tab-fields.blade.php
delete mode 100644 resources/views/includes/checkout/terms-field.blade.php
delete mode 100644 src/Livewire/Forms/CheckoutForm.php
diff --git a/resources/js/checkout.js b/resources/js/checkout.js
index 1101388..f58b43d 100644
--- a/resources/js/checkout.js
+++ b/resources/js/checkout.js
@@ -84,7 +84,7 @@
name = $el.attr('name'),
type = $el.attr('type');
- if (!name.length || name.startsWith('form.')) return;
+ if (!name.length || name.startsWith('fields.')) return;
if (['radio', 'checkbox'].indexOf(type) !== -1 && !$el.prop('checked')) return;
@@ -92,7 +92,7 @@
});
if ((checkoutComponent = Livewire.find(checkoutComponentId)) && Object.keys(paymentFields).length) {
- checkoutComponent.$set('form.payment_fields', paymentFields, false)
+ checkoutComponent.$set('fields.payment_fields', paymentFields, false)
}
}
diff --git a/resources/js/fulfillment.js b/resources/js/fulfillment.js
index 87d8685..56501ce 100644
--- a/resources/js/fulfillment.js
+++ b/resources/js/fulfillment.js
@@ -10,7 +10,7 @@ window.OrangeFulfillment = (timeslot) => {
this.orderTime = this.$wire.get('orderTime');
this.hideDeliveryAddress = this.$wire.get('orderType') !== 'delivery';
- this.showTimePicker = this.$wire.get('isAsap') == 0;
+ this.showTimePicker = !this.$wire.get('previewMode') && this.$wire.get('isAsap') == 0;
this.$wire.$watch('orderDate', value => {
this.orderDate = value;
diff --git a/resources/lang/en/default.php b/resources/lang/en/default.php
index 1bcfd8b..cbe6eeb 100644
--- a/resources/lang/en/default.php
+++ b/resources/lang/en/default.php
@@ -45,6 +45,7 @@
'text_customer' => 'Customer',
'text_guest' => 'Guest',
'text_login' => 'Log In',
+ 'text_no_delivery_address' => 'No delivery address provided',
'text_register' => 'Register It\'s easy and always will be.',
'text_forgot' => 'Forgot password?',
@@ -145,6 +146,11 @@
'help_security' => 'Choose who can access this page',
+ 'alert_preview_mode' => 'Action not allowed in preview mode',
+
+ 'error_telephone_required' => 'Telephone number is required',
+ 'error_telephone_invalid' => 'Telephone number is invalid',
+
'contact' => [
'text_heading' => 'Contact',
'text_summary' => 'Feel free to send a message',
diff --git a/resources/models/checkoutfields.php b/resources/models/checkoutfields.php
new file mode 100644
index 0000000..3983b56
--- /dev/null
+++ b/resources/models/checkoutfields.php
@@ -0,0 +1,74 @@
+ [
+ 'first_name' => [
+ 'label' => 'lang:igniter.cart::default.checkout.label_first_name',
+ 'tab' => 'details',
+ 'type' => 'text',
+ ],
+ 'last_name' => [
+ 'label' => 'lang:igniter.cart::default.checkout.label_last_name',
+ 'tab' => 'details',
+ 'type' => 'text',
+ ],
+ 'email' => [
+ 'label' => 'lang:igniter.cart::default.checkout.label_email',
+ 'tab' => 'details',
+ 'type' => 'email',
+ ],
+ 'telephone' => [
+ 'label' => 'lang:igniter.cart::default.checkout.label_telephone',
+ 'tab' => 'details',
+ 'type' => 'telephone',
+ ],
+ 'comment' => [
+ 'label' => 'lang:igniter.cart::default.checkout.label_comment',
+ 'tab' => 'comments',
+ 'type' => 'textarea',
+ 'cssClass' => 'col-sm-12',
+ ],
+ 'delivery_comment' => [
+ 'label' => 'lang:igniter.cart::default.checkout.label_delivery_comment',
+ 'tab' => 'comments',
+ 'type' => 'textarea',
+ 'cssClass' => 'col-sm-12',
+ ],
+ 'payment' => [
+ 'label' => 'lang:igniter.cart::default.checkout.label_payment_method',
+ 'tab' => 'payments',
+ 'type' => 'payments',
+ 'cssClass' => 'col-sm-12',
+ ],
+ 'termsAgreed' => [
+ 'label' => 'lang:igniter.cart::default.checkout.text_checkout_terms',
+ 'tab' => 'terms',
+ 'type' => 'checkbox',
+ 'cssClass' => 'col-sm-12',
+ 'options' => [],
+ ],
+ ],
+ 'rules' => [
+ 'first_name' => ['required', 'between:1,48'],
+ 'last_name' => ['required', 'between:1,48'],
+ 'email' => ['sometimes', 'required', 'email:filter', 'max:96',
+ Rule::unique('customers', 'email')->ignore(Auth::customer()?->getKey(), 'customer_id')
+ ],
+ 'telephone' => ['sometimes', 'required_if:telephoneIsRequired,true', 'regex:/^([0-9\s\-\+\(\)]*)$/i'],
+ 'comment' => ['max:500'],
+ 'delivery_comment' => ['max:500'],
+ 'payment' => ['sometimes', 'nullable', 'alpha_dash'],
+ 'termsAgreed' => ['sometimes', 'accepted'],
+ 'payment_fields' => ['sometimes', 'array'],
+ 'payment_fields.*' => ['sometimes'],
+ 'payment_fields.pay_from_profile' => ['sometimes', 'integer'],
+ 'payment_fields.create_payment_profile' => ['sometimes', 'integer'],
+ ],
+ 'messages' => [
+ 'telephone.required_if' => lang('igniter.orange::default.error_telephone_required'),
+ 'telephone.regex' => lang('igniter.orange::default.error_telephone_invalid'),
+ ],
+];
diff --git a/resources/views/components/forms/telephone.blade.php b/resources/views/components/forms/telephone.blade.php
index 1117a6d..e30f868 100644
--- a/resources/views/components/forms/telephone.blade.php
+++ b/resources/views/components/forms/telephone.blade.php
@@ -18,7 +18,6 @@
id="hidden-{{$id}}"
value="{{$number}}"
/>
-
@script