Hello, everyone! I am working on a simple plugin for my app. I’ve added the following code to the update function. It mostly works, but for some reason, the addEventListener is triggering twice.
function(instance, properties, context) {
function getLocal() {
const currentId = properties.currentid;
const qtyItem = localStorage.getItem(currentId);
instance.publishState('qty',qtyItem);
}
function plusItem() {
const currentId = properties.currentid;
const qtyItem = localStorage.getItem(currentId);
localStorage.setItem(currentId,Number(qtyItem)+1);
}
function minusItem() {
const currentId = properties.currentid;
const qtyItem = localStorage.getItem(currentId);
if (qtyItem > 1) {
localStorage.setItem(currentId,Number(qtyItem)-1);
}
}
function increaseNumber() {
instance.triggerEvent('changeqty', function() {
plusItem();
getLocal();
});
}
function decreaseNumber() {
instance.triggerEvent('changeqty', function() {
minusItem();
getLocal();
});
}
getLocal();
const buttonPlusElement = document.getElementById(`btnPlus${properties.btnindex}`);
buttonPlusElement.addEventListener('click', function() { console.log('test')});
const buttonMinusElement = document.getElementById(`btnMinus${properties.btnindex}`);
buttonMinusElement.addEventListener('click', decreaseNumber);
}
I managed to resolve my issue by adding Plugin Actions that perform the same tasks as the plusItem and minusItem functions. However, I’m still puzzled by the strange behavior of the addEventListener. Has anyone else encountered such an issue?