Skip to content

Commit

Permalink
Deduplicate the option value IDs after option value selection changes…
Browse files Browse the repository at this point in the history
… in quick add modal
  • Loading branch information
lhoffbeck committed Jun 6, 2024
1 parent cd57192 commit f620161
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
17 changes: 3 additions & 14 deletions assets/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,16 @@ class SectionId {
}

class HTMLUpdateUtility {
#preProcessCallbacks = [];
#postProcessCallbacks = [];

constructor() {}

addPreProcessCallback(callback) {
this.#preProcessCallbacks.push(callback);
}

addPostProcessCallback(callback) {
this.#postProcessCallbacks.push(callback);
}

/**
* Used to swap an HTML node with a new node.
* The new node is inserted as a previous sibling to the old node, the old node is hidden, and then the old node is removed.
*
* The function currently uses a double buffer approach, but this should be replaced by a view transition once it is more widely supported https://developer.mozilla.org/en-US/docs/Web/API/View_Transitions_API
*/
viewTransition(oldNode, newContent) {
this.#preProcessCallbacks.forEach((callback) => callback(newContent));
viewTransition(oldNode, newContent, preProcessCallbacks = [], postProcessCallbacks = []) {
preProcessCallbacks?.forEach((callback) => callback(newContent));

const newNodeWrapper = document.createElement('div');
HTMLUpdateUtility.setInnerHTML(newNodeWrapper, newContent.outerHTML);
Expand All @@ -62,7 +51,7 @@ class HTMLUpdateUtility {
oldNode.parentNode.insertBefore(newNode, oldNode);
oldNode.style.display = 'none';

this.#postProcessCallbacks.forEach((callback) => callback(newNode));
postProcessCallbacks?.forEach((callback) => callback(newNode));

setTimeout(() => oldNode.remove(), 500);
}
Expand Down
35 changes: 27 additions & 8 deletions assets/product-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ if (!customElements.get('product-info')) {
quantityForm = undefined;
onVariantChangeUnsubscriber = undefined;
cartUpdateUnsubscriber = undefined;
swapProductUtility = undefined;
htmlUpdateUtility = undefined;
abortController = undefined;
pendingRequestUrl = null;
preProcessHtmlCallbacks = [];
postProcessHtmlCallbacks = [];

constructor() {
super();
Expand All @@ -29,7 +31,7 @@ if (!customElements.get('product-info')) {
}

addPreProcessCallback(callback) {
this.swapProductUtility.addPreProcessCallback(callback);
this.preProcessHtmlCallbacks.push(callback);
}

initQuantityHandlers() {
Expand All @@ -50,11 +52,11 @@ if (!customElements.get('product-info')) {
}

initializeProductSwapUtility() {
this.swapProductUtility = new HTMLUpdateUtility();
this.swapProductUtility.addPreProcessCallback((html) =>
this.htmlUpdateUtility = new HTMLUpdateUtility();
this.preProcessHtmlCallbacks.push((html) =>
html.querySelectorAll('.scroll-trigger').forEach((element) => element.classList.add('scroll-trigger--cancel'))
);
this.swapProductUtility.addPostProcessCallback((newNode) => {
this.postProcessHtmlCallbacks.push((newNode) => {
window?.Shopify?.PaymentButton?.init();
window?.ProductModel?.loadShopifyXR();
publish(PUB_SUB_EVENTS.sectionRefreshed, {
Expand Down Expand Up @@ -106,11 +108,21 @@ if (!customElements.get('product-info')) {
// If we are in an embedded context (quick add, featured product, etc), only swap product info.
// Otherwise, refresh the entire page content and sibling sections.
if (this.dataset.updateUrl === 'false') {
this.swapProductUtility.viewTransition(this, html.querySelector('product-info'));
this.htmlUpdateUtility.viewTransition(
this,
html.querySelector('product-info'),
this.preProcessHtmlCallbacks,
this.postProcessHtmlCallbacks
);
} else {
document.querySelector('head title').innerHTML = html.querySelector('head title').innerHTML;

this.swapProductUtility.viewTransition(document.querySelector('main'), html.querySelector('main'));
this.htmlUpdateUtility.viewTransition(
document.querySelector('main'),
html.querySelector('main'),
this.preProcessHtmlCallbacks,
this.postProcessHtmlCallbacks
);
}
};
}
Expand Down Expand Up @@ -159,7 +171,13 @@ if (!customElements.get('product-info')) {

updateOptionValues(html) {
const variantSelects = html.querySelector('variant-selects');
if (variantSelects) this.variantSelectors.innerHTML = variantSelects.innerHTML;
if (variantSelects) {
this.htmlUpdateUtility.viewTransition(
this.variantSelectors,
variantSelects,
this.preProcessHtmlCallbacks,
);
}
}

handleUpdateProductInfo(productUrl) {
Expand All @@ -180,6 +198,7 @@ if (!customElements.get('product-info')) {

const updateSourceFromDestination = (id, shouldHide = (source) => false) => {
const source = html.getElementById(`${id}-${this.sectionId}`);
debugger;
const destination = this.querySelector(`#${id}-${this.dataset.section}`);
if (source && destination) {
destination.innerHTML = source.innerHTML;
Expand Down
2 changes: 1 addition & 1 deletion assets/quick-add.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ if (!customElements.get('quick-add-modal')) {

updateImageSizes(productElement) {
const product = productElement.querySelector('.product');
const desktopColumns = product.classList.contains('product--columns');
const desktopColumns = product?.classList.contains('product--columns');
if (!desktopColumns) return;

const mediaImages = product.querySelectorAll('.product__media img');
Expand Down

0 comments on commit f620161

Please sign in to comment.