Skip to content

Commit

Permalink
Update gallary after click
Browse files Browse the repository at this point in the history
  • Loading branch information
KristinaHranovska committed Feb 14, 2024
1 parent 3d93663 commit d8c88d3
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions src/js/add-to-favorites.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const storedArrayAdd = JSON.parse(localStorage.getItem('addKeyID')) || [];
const storedArrayRemove = JSON.parse(localStorage.getItem('addKremoveKeyIDeyID')) || [];
let cardID;

const refs = {
addToFavoritesBtn: document.querySelector('.add-to-favorities'),
removeFromFavoritesBtn: document.querySelector('.js-remove'),
modal: document.querySelector('.modal-window'),
idModul: document.querySelector('.js-gallery'),
}

Expand All @@ -28,10 +26,6 @@ if (!localStorage.getItem('addKeyID')) {
const emptyArrayAdd = [];
localStorage.setItem('addKeyID', JSON.stringify(emptyArrayAdd));
}
if (!localStorage.getItem('removeKeyID')) {
const emptyArrayRemove = [];
localStorage.setItem('removeKeyID', JSON.stringify(emptyArrayRemove));
}

refs.addToFavoritesBtn.addEventListener('click', getIdFavorites);
function getIdFavorites() {
Expand All @@ -43,18 +37,22 @@ function getIdFavorites() {
localStorage.setItem('addKeyID', JSON.stringify(storedArrayFavorites));
}

refs.removeFromFavoritesBtn.addEventListener('click', removeIdFavorites)
refs.removeFromFavoritesBtn.addEventListener('click', removeIdFavorites);

function removeIdFavorites() {
refs.removeFromFavoritesBtn.classList.add('hidden-btn');
refs.addToFavoritesBtn.classList.remove('hidden-btn');

const storedArrayRemove = JSON.parse(localStorage.getItem('removeKeyID'));
storedArrayRemove.push(cardID);
localStorage.setItem('removeKeyID', JSON.stringify(storedArrayRemove));
updateGallery();
}

function updateGallery() {
const savedCards = storedArrayAdd.filter(card => card !== cardID);
localStorage.setItem('addKeyID', JSON.stringify(savedCards));

const cardToRemove = document.querySelector(`.js-id[data-id="${cardID}"]`);

const index = storedArrayAdd.indexOf(cardID);
if (index !== -1) {
storedArrayAdd.splice(index, 1);
localStorage.setItem('addKeyID', JSON.stringify(storedArrayAdd));
if (cardToRemove) {
cardToRemove.remove();
}
}
}

0 comments on commit d8c88d3

Please sign in to comment.