Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

V-3085 FIX showing notify_me_when_available modal too quickly (#12701)

brozek8d0173c1fd46
diff --git a/storefront/app/javascript/spree/storefront/controllers/product_form_controller.js b/storefront/app/javascript/spree/storefront/controllers/product_form_controller.js
index 1ed84a120d..52b4afd1dc 100644
--- a/storefront/app/javascript/spree/storefront/controllers/product_form_controller.js
+++ b/storefront/app/javascript/spree/storefront/controllers/product_form_controller.js
@@ -28,20 +28,12 @@ export default class extends Controller {
}
connect() {
- const selectedOptions = new Set()
- this.optionTargets.forEach((option) => {
- if (option.checked) {
- selectedOptions.add(option.dataset.optionId)
- }
- })
- const requiredOptions = new Set(this.requiredOptionsValue)
+ if (this.hasAddToWishlistTarget && this.variantFromOptionsDisabledValue) {
+ const notSelectedOptions = this.getNotSelectedOptions()
- this.notSelectedOptions = [...requiredOptions].filter((x) => !selectedOptions.has(x))
- if (this.notSelectedOptions.length) {
- this.submitTargets.forEach((button) => button.addEventListener('click', this.showNotSelectedOptions))
- }
- if (this.hasAddToWishlistTarget && this.variantFromOptionsDisabledValue && this.notSelectedOptions.length === 0) {
- this.addToWishlistTarget.disabled = true
+ if (notSelectedOptions.length === 0) {
+ this.addToWishlistTarget.disabled = true
+ }
}
if (this.hasDesktopMediaGalleryTarget && this.hasProductDetailsTarget) {
@@ -56,14 +48,14 @@ export default class extends Controller {
this.submitTargets.forEach((button) => button.addEventListener('turbo-stream-form:submit-end', this.enableForm))
}
- disconnect() {
- this.submitTargets.forEach((button) => button.removeEventListener('click', this.showNotSelectedOptions))
- }
-
showNotSelectedOptions = (e) => {
+ const notSelectedOptions = this.getNotSelectedOptions()
+
+ if (!notSelectedOptions.length) return
+
e.preventDefault()
- this.notSelectedOptions.forEach((option, index) => {
+ notSelectedOptions.forEach((option, index) => {
const fieldSetElement = this.element.querySelector(`[data-option-id="${option}"]`)
if (index === 0) {
const toggleElement = fieldSetElement.querySelector('[data-controller="dropdown"]')
@@ -133,4 +125,15 @@ export default class extends Controller {
enableForm = () => {
this.disabledValue = false
}
+
+ getNotSelectedOptions() {
+ const selectedOptions = new Set()
+ this.optionTargets.forEach((option) => {
+ if (option.checked) {
+ selectedOptions.add(option.dataset.optionId)
+ }
+ })
+ const requiredOptions = new Set(this.requiredOptionsValue)
+ return [...requiredOptions].filter((x) => !selectedOptions.has(x))
+ }
}
diff --git a/storefront/app/views/themes/default/spree/products/_add_to_cart_button.html.erb b/storefront/app/views/themes/default/spree/products/_add_to_cart_button.html.erb
index 345732e289..a5d4327a41 100644
--- a/storefront/app/views/themes/default/spree/products/_add_to_cart_button.html.erb
+++ b/storefront/app/views/themes/default/spree/products/_add_to_cart_button.html.erb
@@ -16,7 +16,10 @@ variant_not_available = selected_variant.nil? ||
<%= button_tag type: (variant_not_available || not_all_options_selected ? 'button' : 'submit'),
class: 'btn-primary btn-icon w-full h-12 add-to-cart-button',
data: {
- action: ('modal#open' if variant_not_available),
+ action: [
+ 'click->product-form#showNotSelectedOptions',
+ ('click->modal#open' if variant_not_available)
+ ].compact.join(' '),
product_form_target: 'submit'
},
disabled: product.price_in(current_currency).zero? do %>
@@ -38,7 +41,10 @@ variant_not_available = selected_variant.nil? ||
<%= button_tag type: (variant_not_available || not_all_options_selected ? 'button' : 'submit'),
class: 'btn-primary btn-icon w-full h-12 add-to-cart-button',
data: {
- action: ('modal#open' if variant_not_available),
+ action: [
+ 'click->product-form#showNotSelectedOptions',
+ ('click->modal#open' if variant_not_available)
+ ].compact.join(' '),
product_form_target: 'submit'
},
disabled: product.price_in(current_currency).zero? do %>