{"version":3,"file":"likeButton.6491d7d1.js","sources":["../../../src/js/modules/like-button.js"],"sourcesContent":["import FetchWrapper from './fetch-wrapper.js';\nconst API = new FetchWrapper( window.origin );\n// API.get( endpoint );\n\nfunction initialiseLikeButtons() { // Sets the state etc for all \"like buttons\" on the page\n\tconst likeButtons = document.querySelectorAll('button.likeButton');\n\tlet likedEntries = [];\n\n\tif ( localStorage.getItem('likedEntries') ) {\n\t\tlikedEntries = JSON.parse( localStorage.getItem('likedEntries') );\n\t}\n\n\tlikeButtons.forEach(likeButton => {\n\t\tif(!likeButton.dataset.entryId) {\n\t\t\tconsole.error(`button missing data-entry-id`);\n\t\t\treturn null;\n\t\t}\n\n\t\tif (likedEntries.includes(likeButton.dataset.entryId)) {\n\t\t\t// The button we're looking at already has its associated entry in localStorage\n\t\t\tlikeButton.dataset.state = 'liked';\n\t\t\tlikeButton.setAttribute('aria-label', 'Un-like this');\n\t\t\tconsole.log(likeButton.querySelector('.buttonMessage'));\n\t\t\tlikeButton.querySelector('.buttonMessage').textContent = 'Remove property';\n\t\t} else {\n\t\t\tlikeButton.dataset.state = null;\n\t\t\tlikeButton.setAttribute('aria-label', 'Like this');\n\t\t\tlikeButton.querySelector('.buttonMessage').textContent = 'Save property';\n\t\t}\n\n\t\tlikeButton.addEventListener('click', handleLikeButtonClick);\n\t});\n}\n\nfunction handleLikeButtonClick(event) { // Hand off to the correct action function when clicked\n\tconst clickedButton = event.currentTarget;\n\n\tif (!clickedButton.dataset.state || !clickedButton.dataset.entryId) {\n\t\tconsole.error(`Button missing required data attributes`);\n\t\treturn false;\n\t}\n\n\tif (clickedButton.dataset.state == 'liked') {\n\t\tunlike(clickedButton);\n\t} else {\n\t\tlike(clickedButton);\n\t}\n\n\tlet toggleMyLikes = document.querySelector('.toggleMyLikes');\n\ttoggleMyLikes.classList.add('uc_grabAttention');\n\n\tsetTimeout(\n\t\t() => {\n\t\t\ttoggleMyLikes.classList.remove('uc_grabAttention');\n\t\t},\n\t\t600\n\t);\n}\n\nfunction like( clickedButton ) { // Do what we need when a button is clicked to store a \"like\"\n\t// we know the button has the attributes we need as the handleLikeClick function has enforced that\n\tconst entryId = clickedButton.dataset.entryId;\n\tlet likedEntries = [];\n\n\tif ( localStorage.getItem('likedEntries') ) {\n\t\tlikedEntries = JSON.parse( localStorage.getItem('likedEntries') );\n\t}\n\n\tif( likedEntries.includes( entryId ) ) {\n\t\tconsole.error(`Entry is already liked`);\n\t\treturn false;\n\t}\n\n\tlikedEntries.push( entryId );\n\tlocalStorage.setItem('likedEntries', JSON.stringify( likedEntries) );\n\n\tclickedButton.dataset.state = 'liked';\n\tclickedButton.setAttribute('aria-label', `Remove from liked`);\n\tclickedButton.querySelector('.buttonMessage').textContent = 'Remove property';\n\n\tlocalStorage.setItem('myLikesStatus', 'stale');\n\tpopulateMyLikesDrawer();\n}\n\nfunction unlike( clickedButton ) { // Do what we need when a button is clicked to remove a \"like\"\n\t// we know the button has the attributes we need as the handleLikeClick function has enforced that\n\tconst entryId = clickedButton.dataset.entryId;\n\tlet likedEntries = [];\n\n\tif ( localStorage.getItem('likedEntries') ) {\n\t\tlikedEntries = JSON.parse( localStorage.getItem('likedEntries') );\n\t}\n\n\tif( ! likedEntries.includes( entryId ) ) {\n\t\tconsole.error(`Entry is not in the liked list`);\n\t\treturn false;\n\t}\n\n\tlikedEntries.splice( likedEntries.indexOf( entryId ), 1 );\n\tlocalStorage.setItem('likedEntries', JSON.stringify( likedEntries) );\n\n\tclickedButton.dataset.state = null;\n\tclickedButton.setAttribute('aria-label', `Add to liked`);\n\tclickedButton.querySelector('.buttonMessage').textContent = 'Save property';\n\n\t// let parent = clickedButton.closest('.card');\n\t// parent.classList.add('justRemoved');\n\n\tlocalStorage.setItem('myLikesStatus', 'stale');\n\tpopulateMyLikesDrawer();\n}\n\nfunction populateMyLikesDrawer() {\n\tlet entryIds = JSON.parse( localStorage.getItem('likedEntries') ).join(',');\n\tconsole.log(`/my-likes?entryIds=${entryIds}`);\n\n\tif (!entryIds) {\n\t\tconsole.log('No entries in local storage');\n\t\treturn;\n\t}\n\n\tAPI.getHtml(`/my-likes?entryIds=${entryIds}`).then(response => {\n\t\tlet responseAsDOM = document.createRange().createContextualFragment(response);\n\t\tdocument.querySelector('#myFavRow .wrapper').replaceChildren(responseAsDOM);\n\n\t\tinitialiseLikeButtons(); // Re-initialise, because the loaded content can itself might have like buttons inside\n\t\tlocalStorage.setItem('myLikesStatus', 'fresh');\n\t});\n}\n\nfunction toggleMyLikesDrawer() {\n\tlet myFavRow = document.querySelector('#myFavRow');\n\tmyFavRow.classList.toggle('active');\n}\n\nfunction initialiseMyLikesDrawer() {\n\tconst showHideToggles = document.querySelectorAll('button.toggleMyLikes');\n\n\tlocalStorage.setItem('myLikesStatus', 'stale'); // initial load ensure state management knows we have no items dynamically loaded yet\n\n\tshowHideToggles.forEach( toggle => {\n\t\ttoggle.addEventListener('click', handleMyLikesButtonClick);\n\t});\n}\n\nfunction handleMyLikesButtonClick() {\n\tlet likedEntries = [];\n\n\tif (localStorage.getItem('likedEntries')) {\n\t\tlikedEntries = JSON.parse(localStorage.getItem('likedEntries'));\n\t}\n\n\tif (\n\t\tlikedEntries.length > 0\n\t\t&&\n\t\tlocalStorage.getItem('myLikesStatus') != 'fresh'\n\t) {\n\t\tconsole.log('You have items saved, but they need to be loaded via fetch');\n\t\tpopulateMyLikesDrawer();\n\t\ttoggleMyLikesDrawer();\n\t} else {\n\t\tconsole.log('just show/hide whatever content is in the div at the moment');\n\t\ttoggleMyLikesDrawer();\n\t}\n}\n\ninitialiseLikeButtons(); // the heart toggles on various Entry listing items\ninitialiseMyLikesDrawer(); // the panel full of liked items\n"],"names":["API","FetchWrapper","initialiseLikeButtons","likeButtons","likedEntries","likeButton","handleLikeButtonClick","event","clickedButton","unlike","like","toggleMyLikes","entryId","populateMyLikesDrawer","entryIds","response","responseAsDOM","toggleMyLikesDrawer","initialiseMyLikesDrawer","showHideToggles","toggle","handleMyLikesButtonClick"],"mappings":"+CACA,M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