Skip to content

Commit 91385fe

Browse files
authored
Add wishlist event hooks (#19) (#20)
1 parent 023bff1 commit 91385fe

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

assets/Wishlist.js

+44-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
var LOCAL_STORAGE_WISHLIST_KEY = 'shopify-wishlist';
22
var LOCAL_STORAGE_DELIMITER = ',';
33
var BUTTON_ACTIVE_CLASS = 'active';
4+
var GRID_LOADED_CLASS = 'loaded';
45

56
var selectors = {
67
button: '[button-wishlist]',
78
grid: '[grid-wishlist]',
89
};
910

1011
document.addEventListener('DOMContentLoaded', function () {
11-
var buttons = document.querySelectorAll(selectors.button) || [];
12-
if (buttons.length) setupButtons(buttons);
12+
initButtons();
13+
initGrid();
14+
});
1315

14-
var grid = document.querySelector(selectors.grid) || false;
15-
if (grid) setupGrid(grid);
16+
document.addEventListener('shopify-wishlist:updated', function (event) {
17+
console.log('[Shopify Wishlist] Wishlist Updated ✅', event.detail.wishlist);
18+
initGrid();
19+
});
20+
21+
document.addEventListener('shopify-wishlist:init-product-grid', function (event) {
22+
console.log('[Shopify Wishlist] Wishlist Product List Loaded ✅', event.detail.wishlist);
23+
});
24+
25+
document.addEventListener('shopify-wishlist:init-buttons', function (event) {
26+
console.log('[Shopify Wishlist] Wishlist Buttons Loaded ✅', event.detail.wishlist);
1627
});
1728

1829
var setupGrid = function (grid) {
@@ -26,15 +37,20 @@ var setupGrid = function (grid) {
2637
Promise.all(requests).then(function (responses) {
2738
var wishlistProductCards = responses.join('');
2839
grid.innerHTML = wishlistProductCards;
29-
var buttons = grid.querySelectorAll(selectors.button) || [];
30-
if (buttons.length) setupButtons(buttons);
40+
grid.classList.add(GRID_LOADED_CLASS);
41+
initButtons();
42+
43+
var event = new CustomEvent('shopify-wishlist:init-product-grid', {
44+
detail: { wishlist: wishlist }
45+
});
46+
document.dispatchEvent(event);
3147
});
3248
};
3349

3450
var setupButtons = function (buttons) {
3551
buttons.forEach(function (button) {
3652
var productHandle = button.dataset.productHandle || false;
37-
if (!productHandle) return console.error('[Wishlist] Missing `data-product-handle` attribute. Failed to update the wishlist.');
53+
if (!productHandle) return console.error('[Shopify Wishlist] Missing `data-product-handle` attribute. Failed to update the wishlist.');
3854
if (wishlistContains(productHandle)) button.classList.add(BUTTON_ACTIVE_CLASS);
3955
button.addEventListener('click', function () {
4056
updateWishlist(productHandle);
@@ -43,6 +59,21 @@ var setupButtons = function (buttons) {
4359
});
4460
};
4561

62+
var initGrid = function () {
63+
var grid = document.querySelector(selectors.grid) || false;
64+
if (grid) setupGrid(grid);
65+
};
66+
67+
var initButtons = function () {
68+
var buttons = document.querySelectorAll(selectors.button) || [];
69+
if (buttons.length) setupButtons(buttons);
70+
else return;
71+
var event = new CustomEvent('shopify-wishlist:init-buttons', {
72+
detail: { wishlist: getWishlist() }
73+
});
74+
document.dispatchEvent(event);
75+
};
76+
4677
var getWishlist = function () {
4778
var wishlist = localStorage.getItem(LOCAL_STORAGE_WISHLIST_KEY) || false;
4879
if (wishlist) return wishlist.split(LOCAL_STORAGE_DELIMITER);
@@ -53,6 +84,12 @@ var setWishlist = function (array) {
5384
var wishlist = array.join(LOCAL_STORAGE_DELIMITER);
5485
if (array.length) localStorage.setItem(LOCAL_STORAGE_WISHLIST_KEY, wishlist);
5586
else localStorage.removeItem(LOCAL_STORAGE_WISHLIST_KEY);
87+
88+
var event = new CustomEvent('shopify-wishlist:updated', {
89+
detail: { wishlist: array }
90+
});
91+
document.dispatchEvent(event);
92+
5693
return wishlist;
5794
};
5895

tasks/copy.js

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const liquidPaths = [
88
'*snippets/**/*',
99
'*sections/**/*',
1010
'*templates/**/*',
11+
'*layout/**/*',
1112
];
1213

1314
function renameHiddenFiles (path) {

0 commit comments

Comments
 (0)