Skip to content

Commit 821fbbb

Browse files
authored
Upgrade to ES6+ syntax (#25)
1 parent 09b9a09 commit 821fbbb

File tree

1 file changed

+56
-54
lines changed

1 file changed

+56
-54
lines changed

assets/Wishlist.js

+56-54
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,120 @@
1-
var LOCAL_STORAGE_WISHLIST_KEY = 'shopify-wishlist';
2-
var LOCAL_STORAGE_DELIMITER = ',';
3-
var BUTTON_ACTIVE_CLASS = 'active';
4-
var GRID_LOADED_CLASS = 'loaded';
1+
const LOCAL_STORAGE_WISHLIST_KEY = 'shopify-wishlist';
2+
const LOCAL_STORAGE_DELIMITER = ',';
3+
const BUTTON_ACTIVE_CLASS = 'active';
4+
const GRID_LOADED_CLASS = 'loaded';
55

6-
var selectors = {
6+
const selectors = {
77
button: '[button-wishlist]',
88
grid: '[grid-wishlist]',
99
productCard: '.product-card',
1010
};
1111

12-
document.addEventListener('DOMContentLoaded', function () {
12+
document.addEventListener('DOMContentLoaded', () => {
1313
initButtons();
1414
initGrid();
1515
});
1616

17-
document.addEventListener('shopify-wishlist:updated', function (event) {
17+
document.addEventListener('shopify-wishlist:updated', (event) => {
1818
console.log('[Shopify Wishlist] Wishlist Updated ✅', event.detail.wishlist);
1919
initGrid();
2020
});
2121

22-
document.addEventListener('shopify-wishlist:init-product-grid', function (event) {
22+
document.addEventListener('shopify-wishlist:init-product-grid', (event) => {
2323
console.log('[Shopify Wishlist] Wishlist Product List Loaded ✅', event.detail.wishlist);
2424
});
2525

26-
document.addEventListener('shopify-wishlist:init-buttons', function (event) {
26+
document.addEventListener('shopify-wishlist:init-buttons', (event) => {
2727
console.log('[Shopify Wishlist] Wishlist Buttons Loaded ✅', event.detail.wishlist);
2828
});
2929

30-
var setupGrid = function (grid) {
31-
var wishlist = getWishlist();
32-
var requests = wishlist.map(function (handle) {
33-
var productTileTemplateUrl = '/products/' + handle + '?view=card';
34-
return fetch(productTileTemplateUrl)
35-
.then(function (res) { return res.text() })
36-
.then(function (res) {
37-
var text = res;
38-
var parser = new DOMParser();
39-
var htmlDocument = parser.parseFromString(text, "text/html");
40-
var productCard = htmlDocument.documentElement.querySelector(selectors.productCard);
41-
return productCard.outerHTML;
42-
});
43-
});
44-
Promise.all(requests).then(function (responses) {
45-
var wishlistProductCards = responses.join('');
46-
grid.innerHTML = wishlistProductCards;
47-
grid.classList.add(GRID_LOADED_CLASS);
48-
initButtons();
49-
50-
var event = new CustomEvent('shopify-wishlist:init-product-grid', {
51-
detail: { wishlist: wishlist }
52-
});
53-
document.dispatchEvent(event);
30+
const fetchProductCardHTML = (handle) => {
31+
const productTileTemplateUrl = `/products/${handle}?view=card`;
32+
return fetch(productTileTemplateUrl)
33+
.then((res) => res.text())
34+
.then((res) => {
35+
const text = res;
36+
const parser = new DOMParser();
37+
const htmlDocument = parser.parseFromString(text, 'text/html');
38+
const productCard = htmlDocument.documentElement.querySelector(selectors.productCard);
39+
return productCard.outerHTML;
40+
})
41+
.catch((err) => console.error(`[Shopify Wishlist] Failed to load content for handle: ${handle}`, err));
42+
};
43+
44+
const setupGrid = async (grid) => {
45+
const wishlist = getWishlist();
46+
const requests = wishlist.map(fetchProductCardHTML);
47+
const responses = await Promise.all(requests);
48+
const wishlistProductCards = responses.join('');
49+
grid.innerHTML = wishlistProductCards;
50+
grid.classList.add(GRID_LOADED_CLASS);
51+
initButtons();
52+
53+
const event = new CustomEvent('shopify-wishlist:init-product-grid', {
54+
detail: { wishlist: wishlist }
5455
});
56+
document.dispatchEvent(event);
5557
};
5658

57-
var setupButtons = function (buttons) {
58-
buttons.forEach(function (button) {
59-
var productHandle = button.dataset.productHandle || false;
59+
const setupButtons = (buttons) => {
60+
buttons.forEach((button) => {
61+
const productHandle = button.dataset.productHandle || false;
6062
if (!productHandle) return console.error('[Shopify Wishlist] Missing `data-product-handle` attribute. Failed to update the wishlist.');
6163
if (wishlistContains(productHandle)) button.classList.add(BUTTON_ACTIVE_CLASS);
62-
button.addEventListener('click', function () {
64+
button.addEventListener('click', () => {
6365
updateWishlist(productHandle);
6466
button.classList.toggle(BUTTON_ACTIVE_CLASS);
6567
});
6668
});
6769
};
6870

69-
var initGrid = function () {
70-
var grid = document.querySelector(selectors.grid) || false;
71+
const initGrid = () => {
72+
const grid = document.querySelector(selectors.grid) || false;
7173
if (grid) setupGrid(grid);
7274
};
7375

74-
var initButtons = function () {
75-
var buttons = document.querySelectorAll(selectors.button) || [];
76+
const initButtons = () => {
77+
const buttons = document.querySelectorAll(selectors.button) || [];
7678
if (buttons.length) setupButtons(buttons);
7779
else return;
78-
var event = new CustomEvent('shopify-wishlist:init-buttons', {
80+
const event = new CustomEvent('shopify-wishlist:init-buttons', {
7981
detail: { wishlist: getWishlist() }
8082
});
8183
document.dispatchEvent(event);
8284
};
8385

84-
var getWishlist = function () {
85-
var wishlist = localStorage.getItem(LOCAL_STORAGE_WISHLIST_KEY) || false;
86+
const getWishlist = () => {
87+
const wishlist = localStorage.getItem(LOCAL_STORAGE_WISHLIST_KEY) || false;
8688
if (wishlist) return wishlist.split(LOCAL_STORAGE_DELIMITER);
8789
return [];
8890
};
8991

90-
var setWishlist = function (array) {
91-
var wishlist = array.join(LOCAL_STORAGE_DELIMITER);
92+
const setWishlist = (array) => {
93+
const wishlist = array.join(LOCAL_STORAGE_DELIMITER);
9294
if (array.length) localStorage.setItem(LOCAL_STORAGE_WISHLIST_KEY, wishlist);
9395
else localStorage.removeItem(LOCAL_STORAGE_WISHLIST_KEY);
9496

95-
var event = new CustomEvent('shopify-wishlist:updated', {
97+
const event = new CustomEvent('shopify-wishlist:updated', {
9698
detail: { wishlist: array }
9799
});
98100
document.dispatchEvent(event);
99101

100102
return wishlist;
101103
};
102104

103-
var updateWishlist = function (handle) {
104-
var wishlist = getWishlist();
105-
var indexInWishlist = wishlist.indexOf(handle);
105+
const updateWishlist = (handle) => {
106+
const wishlist = getWishlist();
107+
const indexInWishlist = wishlist.indexOf(handle);
106108
if (indexInWishlist === -1) wishlist.push(handle);
107109
else wishlist.splice(indexInWishlist, 1);
108110
return setWishlist(wishlist);
109111
};
110112

111-
var wishlistContains = function (handle) {
112-
var wishlist = getWishlist();
113-
return wishlist.indexOf(handle) !== -1;
113+
const wishlistContains = (handle) => {
114+
const wishlist = getWishlist();
115+
return wishlist.includes(handle);
114116
};
115117

116-
var resetWishlist = function () {
118+
const resetWishlist = () => {
117119
return setWishlist([]);
118120
};

0 commit comments

Comments
 (0)