Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Click event is not working #33

Open
blackroostercouk opened this issue Mar 31, 2022 · 4 comments
Open

Click event is not working #33

blackroostercouk opened this issue Mar 31, 2022 · 4 comments

Comments

@blackroostercouk
Copy link

blackroostercouk commented Mar 31, 2022

Click event is not working after dom bind, this is big issue for predictive search or ajax pagination scripts. I've wrote my own click and foreach event problem solved. Over all really usefull, thank you!

@blackroostercouk blackroostercouk changed the title Click event not working Click event is not working Mar 31, 2022
@mayur2010
Copy link

@blackroostercouk Can you share those with us? if possible. We're also having the same issues.

@blackroostercouk
Copy link
Author

@blackroostercouk Can you share those with us? if possible. We're also having the same issues.

sure, you can find Wishlist.js below.

const LOCAL_STORAGE_WISHLIST_KEY = 'shopify-wishlist';
const LOCAL_STORAGE_DELIMITER = ',';
const BUTTON_ACTIVE_CLASS = 'active';
const GRID_LOADED_CLASS = 'loaded';

const selectors = {
button: '[button-wishlist]',
grid: '[grid-wishlist]',
productCard: '.product-card',
};

document.addEventListener('DOMContentLoaded', () => {

initGrid();
});

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

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

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

const fetchProductCardHTML = (handle) => {
const productTileTemplateUrl = /products/${handle}?view=card;
return fetch(productTileTemplateUrl)
.then((res) => res.text())
.then((res) => {
const text = res;
const parser = new DOMParser();
const htmlDocument = parser.parseFromString(text, 'text/html');
const productCard = htmlDocument.documentElement.querySelector(selectors.productCard);
return productCard.outerHTML;
})
.catch((err) => console.error([Shopify Wishlist] Failed to load content for handle: ${handle}, err));
};

const setupGrid = async (grid) => {
const wishlist = getWishlist();
const requests = wishlist.map(fetchProductCardHTML);
const responses = await Promise.all(requests);
const wishlistProductCards = responses.join('');
grid.innerHTML = wishlistProductCards;
grid.classList.add(GRID_LOADED_CLASS);

const event = new CustomEvent('shopify-wishlist:init-product-grid', {
detail: { wishlist: wishlist }
});
document.dispatchEvent(event);
};

$(document).on('click','.whishlist-btn', function (event){
console.log('click');
var datahandle = $(this).attr('data-product-handle');
updateWishlist(datahandle);
if($(this).hasClass('active'))
{
$(this).removeClass('active')
}
else{
$(this).addClass('active');
}
});

const initGrid = () => {
const grid = document.querySelector(selectors.grid) || false;
if (grid) setupGrid(grid);
};

const getWishlist = () => {
const wishlist = localStorage.getItem(LOCAL_STORAGE_WISHLIST_KEY) || false;
if (wishlist) return wishlist.split(LOCAL_STORAGE_DELIMITER);
return [];
};

const setWishlist = (array) => {
const wishlist = array.join(LOCAL_STORAGE_DELIMITER);
if (array.length) localStorage.setItem(LOCAL_STORAGE_WISHLIST_KEY, wishlist);
else localStorage.removeItem(LOCAL_STORAGE_WISHLIST_KEY);

const event = new CustomEvent('shopify-wishlist:updated', {
detail: { wishlist: array }
});
document.dispatchEvent(event);

return wishlist;
};

const updateWishlist = (handle) => {
const wishlist = getWishlist();
const indexInWishlist = wishlist.indexOf(handle);
if (indexInWishlist === -1) wishlist.push(handle);
else wishlist.splice(indexInWishlist, 1);
return setWishlist(wishlist);
};

const wishlistContains = (handle) => {
const wishlist = getWishlist();
return wishlist.includes(handle);
};

const resetWishlist = () => {
return setWishlist([]);
};

$( ".whishlist-btn" ).each(function() {
var datahandle = $(this).attr('data-product-handle');
if (wishlistContains(datahandle)){
$(this).addClass('active');
}
});

function reloadHearth(){
$( ".whishlist-btn" ).each(function() {
var datahandle = $(this).attr('data-product-handle');
if (wishlistContains(datahandle)){
$(this).addClass('active');
}
});
console.log('heartreload');
}

@prinka-goyal
Copy link

[Shopify Wishlist] Failed to load content for handle

@dlerm
Copy link
Owner

dlerm commented Apr 19, 2022

I think the proper way to handle injected DOM elements would be to use delegated event listeners connected to the document. I'll add an example soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants