Skip to content

Commit

Permalink
Refetch entire page on product swap
Browse files Browse the repository at this point in the history
Remove unused unsubscriber function
  • Loading branch information
lhoffbeck committed May 23, 2024
1 parent 45f65d3 commit 4f3b617
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
4 changes: 0 additions & 4 deletions assets/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -1104,10 +1104,6 @@ class ProductRecommendations extends HTMLElement {
this.initializeRecommendations(this.dataset.productId);
}

disconnectedCallback() {
this.unsubscribeFromSectionRefresh();
}

initializeRecommendations(productId) {
this.observer?.unobserve(this);
this.observer = new IntersectionObserver(
Expand Down
38 changes: 22 additions & 16 deletions assets/product-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ if (!customElements.get('product-info')) {
productForm?.handleErrorMessage();

let callback = () => {};
let productUrl = this.#getProductInfoUrl(targetUrl, variant?.id);
if (this.dataset.url !== targetUrl) {
this.#updateURL(targetUrl, variant?.id);
this.#updateShareUrl(targetUrl, variant?.id);
callback = this.#handleSwapProduct();
productUrl = this.#getProductInfoUrl(targetUrl, variant?.id, true);
} else if (!variant) {
this.#setUnavailable();
callback = (html) => {
Expand All @@ -90,26 +92,21 @@ if (!customElements.get('product-info')) {
callback = this.#handleUpdateProductInfo(variant);
}

this.#renderProductInfo(targetUrl, variant?.id, target.id, callback);
this.#renderProductInfo(productUrl, target.id, callback);
}

#handleSwapProduct() {
return (html) => {
this.productModal?.remove();
const newProduct = html.querySelector('product-info');
this.swapProductUtility.viewTransition(this, newProduct);
this.relatedProducts?.initializeRecommendations(newProduct.dataset.productId);
this.quickOrderList?.refresh();
this.swapProductUtility.viewTransition(document.querySelector('main'), html.querySelector('main'));
};
}

#renderProductInfo(url, variantId, targetId, callback) {
#renderProductInfo(productUrl, targetId, callback) {
this.abortController?.abort();
this.abortController = new AbortController();

fetch(this.#getProductInfoUrl(url, variantId), {
signal: this.abortController.signal,
})
fetch(productUrl, { signal: this.abortController.signal })
.then((response) => response.text())
.then((responseText) => {
const html = new DOMParser().parseFromString(responseText, 'text/html');
Expand All @@ -121,18 +118,21 @@ if (!customElements.get('product-info')) {
});
}

#getProductInfoUrl(url, variantId) {
let params;
#getProductInfoUrl(url, variantId, shouldFetchFullPage = false) {
const params = [];

!shouldFetchFullPage && params.push(`section_id=${this.sectionId}`);

if (variantId) {
params = `variant=${variantId}`;
params.push(`variant=${variantId}`);
} else {
const optionValues = this.variantSelectors.selectedOptionValues;
if (optionValues.length) {
params = `option_values=${optionValues.join(',')}`;
params.push(`option_values=${optionValues.join(',')}`);
}
}

return `${url}?section_id=${this.sectionId}&${params}`;
return `${url}?${params.join('&')}`;
}

#updateOptionValues(html) {
Expand Down Expand Up @@ -362,12 +362,18 @@ if (!customElements.get('product-info')) {
}

get relatedProducts() {
const relatedProductsSectionId = SectionId.getIdForSection(SectionId.parseId(this.sectionId), 'related-products');
const relatedProductsSectionId = SectionId.getIdForSection(
SectionId.parseId(this.sectionId),
'related-products'
);
return document.querySelector(`product-recommendations[data-section-id^="${relatedProductsSectionId}"]`);
}

get quickOrderList() {
const quickOrderListSectionId = SectionId.getIdForSection(SectionId.parseId(this.sectionId), 'quick_order_list');
const quickOrderListSectionId = SectionId.getIdForSection(
SectionId.parseId(this.sectionId),
'quick_order_list'
);
return document.querySelector(`quick-order-list[data-id^="${quickOrderListSectionId}"]`);
}

Expand Down

0 comments on commit 4f3b617

Please sign in to comment.