forked from Shopify/dawn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'shopify/main' into next
* shopify/main: Gift cards/add recipient (Shopify#2412) Update 1 translation file (Shopify#2453) [Slideshow] Add ambient movement to background (Shopify#2383) Wrap calls to search results ind collection counts in paginate to reduce additional requests (Shopify#2421) [Header] Add app block in the header section (Shopify#2238) [Image with Text] Add ambient movement to image (Shopify#2385) Update 1 translation file (Shopify#2450) [Blog post section] Fix slides size on mobile (Shopify#2368) Duplicated scrollbar in drawer menu (Shopify#2400) [Header locales] Support gradients (Shopify#2386) [Image banner] Add ambient movement to background (Shopify#2342)
- Loading branch information
Showing
74 changed files
with
2,093 additions
and
709 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
if (!customElements.get('recipient-form')) { | ||
customElements.define('recipient-form', class RecipientForm extends HTMLElement { | ||
constructor() { | ||
super(); | ||
this.checkboxInput = this.querySelector(`#Recipient-Checkbox-${ this.dataset.sectionId }`); | ||
this.checkboxInput.disabled = false; | ||
this.hiddenControlField = this.querySelector(`#Recipient-Control-${ this.dataset.sectionId }`); | ||
this.hiddenControlField.disabled = true; | ||
this.emailInput = this.querySelector(`#Recipient-email-${ this.dataset.sectionId }`); | ||
this.nameInput = this.querySelector(`#Recipient-name-${ this.dataset.sectionId }`); | ||
this.messageInput = this.querySelector(`#Recipient-message-${ this.dataset.sectionId }`); | ||
this.errorMessageWrapper = this.querySelector('.product-form__recipient-error-message-wrapper'); | ||
this.errorMessageList = this.errorMessageWrapper?.querySelector('ul'); | ||
this.errorMessage = this.errorMessageWrapper?.querySelector('.error-message'); | ||
this.defaultErrorHeader = this.errorMessage?.innerText; | ||
this.currentProductVariantId = this.dataset.productVariantId; | ||
this.addEventListener('change', this.onChange.bind(this)); | ||
} | ||
|
||
cartUpdateUnsubscriber = undefined; | ||
variantChangeUnsubscriber = undefined; | ||
cartErrorUnsubscriber = undefined; | ||
|
||
connectedCallback() { | ||
this.cartUpdateUnsubscriber = subscribe(PUB_SUB_EVENTS.cartUpdate, (event) => { | ||
if (event.source === 'product-form' && event.productVariantId.toString() === this.currentProductVariantId) { | ||
this.resetRecipientForm(); | ||
} | ||
}); | ||
|
||
this.variantChangeUnsubscriber = subscribe(PUB_SUB_EVENTS.variantChange, (event) => { | ||
if (event.data.sectionId === this.dataset.sectionId) { | ||
this.currentProductVariantId = event.data.variant.id.toString(); | ||
} | ||
}); | ||
|
||
this.cartUpdateUnsubscriber = subscribe(PUB_SUB_EVENTS.cartError, (event) => { | ||
if (event.source === 'product-form' && event.productVariantId.toString() === this.currentProductVariantId) { | ||
this.displayErrorMessage(event.message, event.errors); | ||
} | ||
}); | ||
} | ||
|
||
disconnectedCallback() { | ||
if (this.cartUpdateUnsubscriber) { | ||
this.cartUpdateUnsubscriber(); | ||
} | ||
|
||
if (this.variantChangeUnsubscriber) { | ||
this.variantChangeUnsubscriber(); | ||
} | ||
|
||
if (this.cartErrorUnsubscriber) { | ||
this.cartErrorUnsubscriber(); | ||
} | ||
} | ||
|
||
onChange() { | ||
if (!this.checkboxInput.checked) { | ||
this.clearInputFields(); | ||
this.clearErrorMessage(); | ||
} | ||
} | ||
|
||
clearInputFields() { | ||
this.emailInput.value = ''; | ||
this.nameInput.value = ''; | ||
this.messageInput.value = ''; | ||
} | ||
|
||
displayErrorMessage(title, body) { | ||
this.clearErrorMessage(); | ||
this.errorMessageWrapper.hidden = false; | ||
if (typeof body === 'object') { | ||
this.errorMessage.innerText = this.defaultErrorHeader; | ||
return Object.entries(body).forEach(([key, value]) => { | ||
const errorMessageId = `RecipientForm-${ key }-error-${ this.dataset.sectionId }` | ||
const fieldSelector = `#Recipient-${ key }-${ this.dataset.sectionId }`; | ||
const placeholderElement = this.querySelector(`${fieldSelector}`); | ||
const label = placeholderElement?.getAttribute('placeholder') || key; | ||
const message = `${label} ${value}`; | ||
const errorMessageElement = this.querySelector(`#${errorMessageId}`); | ||
const errorTextElement = errorMessageElement?.querySelector('.error-message') | ||
if (!errorTextElement) return; | ||
|
||
if (this.errorMessageList) { | ||
this.errorMessageList.appendChild(this.createErrorListItem(fieldSelector, message)); | ||
} | ||
|
||
errorTextElement.innerText = `${message}.`; | ||
errorMessageElement.classList.remove('hidden'); | ||
|
||
const inputElement = this[`${key}Input`]; | ||
if (!inputElement) return; | ||
|
||
inputElement.setAttribute('aria-invalid', true); | ||
inputElement.setAttribute('aria-describedby', errorMessageId); | ||
}); | ||
} | ||
|
||
this.errorMessage.innerText = body; | ||
} | ||
|
||
createErrorListItem(target, message) { | ||
const li = document.createElement('li'); | ||
const a = document.createElement('a'); | ||
a.setAttribute('href', target); | ||
a.innerText = message; | ||
li.appendChild(a); | ||
li.className = "error-message"; | ||
return li; | ||
} | ||
|
||
clearErrorMessage() { | ||
this.errorMessageWrapper.hidden = true; | ||
|
||
if (this.errorMessageList) this.errorMessageList.innerHTML = ''; | ||
|
||
this.querySelectorAll('.recipient-fields .form__message').forEach(field => { | ||
field.classList.add('hidden'); | ||
const textField = field.querySelector('.error-message'); | ||
if (textField) textField.innerText = ''; | ||
}); | ||
|
||
[this.emailInput, this.messageInput, this.nameInput].forEach(inputElement => { | ||
inputElement.setAttribute('aria-invalid', false); | ||
inputElement.removeAttribute('aria-describedby'); | ||
}); | ||
} | ||
|
||
resetRecipientForm() { | ||
if (this.checkboxInput.checked) { | ||
this.checkboxInput.checked = false; | ||
this.clearInputFields(); | ||
this.clearErrorMessage(); | ||
} | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.