|
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'; |
5 | 5 |
|
6 |
| -var selectors = { |
| 6 | +const selectors = { |
7 | 7 | button: '[button-wishlist]',
|
8 | 8 | grid: '[grid-wishlist]',
|
9 | 9 | productCard: '.product-card',
|
10 | 10 | };
|
11 | 11 |
|
12 |
| -document.addEventListener('DOMContentLoaded', function () { |
| 12 | +document.addEventListener('DOMContentLoaded', () => { |
13 | 13 | initButtons();
|
14 | 14 | initGrid();
|
15 | 15 | });
|
16 | 16 |
|
17 |
| -document.addEventListener('shopify-wishlist:updated', function (event) { |
| 17 | +document.addEventListener('shopify-wishlist:updated', (event) => { |
18 | 18 | console.log('[Shopify Wishlist] Wishlist Updated ✅', event.detail.wishlist);
|
19 | 19 | initGrid();
|
20 | 20 | });
|
21 | 21 |
|
22 |
| -document.addEventListener('shopify-wishlist:init-product-grid', function (event) { |
| 22 | +document.addEventListener('shopify-wishlist:init-product-grid', (event) => { |
23 | 23 | console.log('[Shopify Wishlist] Wishlist Product List Loaded ✅', event.detail.wishlist);
|
24 | 24 | });
|
25 | 25 |
|
26 |
| -document.addEventListener('shopify-wishlist:init-buttons', function (event) { |
| 26 | +document.addEventListener('shopify-wishlist:init-buttons', (event) => { |
27 | 27 | console.log('[Shopify Wishlist] Wishlist Buttons Loaded ✅', event.detail.wishlist);
|
28 | 28 | });
|
29 | 29 |
|
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 } |
54 | 55 | });
|
| 56 | + document.dispatchEvent(event); |
55 | 57 | };
|
56 | 58 |
|
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; |
60 | 62 | if (!productHandle) return console.error('[Shopify Wishlist] Missing `data-product-handle` attribute. Failed to update the wishlist.');
|
61 | 63 | if (wishlistContains(productHandle)) button.classList.add(BUTTON_ACTIVE_CLASS);
|
62 |
| - button.addEventListener('click', function () { |
| 64 | + button.addEventListener('click', () => { |
63 | 65 | updateWishlist(productHandle);
|
64 | 66 | button.classList.toggle(BUTTON_ACTIVE_CLASS);
|
65 | 67 | });
|
66 | 68 | });
|
67 | 69 | };
|
68 | 70 |
|
69 |
| -var initGrid = function () { |
70 |
| - var grid = document.querySelector(selectors.grid) || false; |
| 71 | +const initGrid = () => { |
| 72 | + const grid = document.querySelector(selectors.grid) || false; |
71 | 73 | if (grid) setupGrid(grid);
|
72 | 74 | };
|
73 | 75 |
|
74 |
| -var initButtons = function () { |
75 |
| - var buttons = document.querySelectorAll(selectors.button) || []; |
| 76 | +const initButtons = () => { |
| 77 | + const buttons = document.querySelectorAll(selectors.button) || []; |
76 | 78 | if (buttons.length) setupButtons(buttons);
|
77 | 79 | else return;
|
78 |
| - var event = new CustomEvent('shopify-wishlist:init-buttons', { |
| 80 | + const event = new CustomEvent('shopify-wishlist:init-buttons', { |
79 | 81 | detail: { wishlist: getWishlist() }
|
80 | 82 | });
|
81 | 83 | document.dispatchEvent(event);
|
82 | 84 | };
|
83 | 85 |
|
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; |
86 | 88 | if (wishlist) return wishlist.split(LOCAL_STORAGE_DELIMITER);
|
87 | 89 | return [];
|
88 | 90 | };
|
89 | 91 |
|
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); |
92 | 94 | if (array.length) localStorage.setItem(LOCAL_STORAGE_WISHLIST_KEY, wishlist);
|
93 | 95 | else localStorage.removeItem(LOCAL_STORAGE_WISHLIST_KEY);
|
94 | 96 |
|
95 |
| - var event = new CustomEvent('shopify-wishlist:updated', { |
| 97 | + const event = new CustomEvent('shopify-wishlist:updated', { |
96 | 98 | detail: { wishlist: array }
|
97 | 99 | });
|
98 | 100 | document.dispatchEvent(event);
|
99 | 101 |
|
100 | 102 | return wishlist;
|
101 | 103 | };
|
102 | 104 |
|
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); |
106 | 108 | if (indexInWishlist === -1) wishlist.push(handle);
|
107 | 109 | else wishlist.splice(indexInWishlist, 1);
|
108 | 110 | return setWishlist(wishlist);
|
109 | 111 | };
|
110 | 112 |
|
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); |
114 | 116 | };
|
115 | 117 |
|
116 |
| -var resetWishlist = function () { |
| 118 | +const resetWishlist = () => { |
117 | 119 | return setWishlist([]);
|
118 | 120 | };
|
0 commit comments