function handleFavorites(){
    
    var fav = document.querySelectorAll('[id^="book-key-"]');
    for(const element of fav){
        element.addEventListener('click', async (event) =>{
            let buttonID = event.target.parentElement.id
            let action;
            if(buttonID.endsWith('active')) {
                action = 'remove'
                buttonID = event.target.parentElement.id.split("_").shift()
                buttonID = buttonID.slice(9);
                buttonID = parseInt(buttonID)
                let toDelete = works[buttonID].workid
                fetch(`/api/favorites/${toDelete}`, {
                    method: "DELETE"
                })
                    .then(res => res.json())
                    .then(() => console.log('success'))
            }else{
                action = 'add'
                buttonID = buttonID.slice(9);
                buttonID = parseInt(buttonID)
                const response = await fetch('/api/action', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        action: action,
                        workid: works[buttonID].workid, 
                        title: works[buttonID].titleweb,
                        author: works[buttonID].authorweb
                    })
                })
                const success = await response.json()
            }
            if(action === 'add') {
                element.id = event.target.parentElement.id + '_active'
                event.target.className = "fa fa-heart"
            }
            if(action === 'remove') {
                element.id = event.target.parentElement.id.split("_").shift()
                event.target.className = "fa fa-heart-o"
            }
        })
    }
}
/**Listens to the favourite button, responds on click, if filled remove, if not filled add */
Listens to the favourite button, responds on click, if filled remove, if not filled add
Listens to the favourite button responds on click if filled remove if not filled add
function handleFavorites(){
    var fav = document.querySelectorAll('[id^="book-key-"]');
    for(const element of fav){
        element.addEventListener('click', async (event) =>{
            let buttonID = event.target.parentElement.id
            let action;
            if(buttonID.endsWith('active')) {
                action = 'remove'
                buttonID = event.target.parentElement.id.split("_").shift()
                buttonID = buttonID.slice(9);
                buttonID = parseInt(buttonID)
                let toDelete = works[buttonID].workid
                fetch(`/api/favorites/${toDelete}`, {
                    method: "DELETE"
                })
                    .then(res => res.json())
                    .then(() => console.log('success'))
            }else{
                action = 'add'
                buttonID = buttonID.slice(9);
                buttonID = parseInt(buttonID)
                const response = await fetch('/api/action', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        action: action,
                        workid: works[buttonID].workid, 
                        title: works[buttonID].titleweb,
                        author: works[buttonID].authorweb
                    })
                })
                const success = await response.json()
            }
            if(action === 'add') {
                element.id = event.target.parentElement.id + '_active'
                event.target.className = "fa fa-heart"
            }
            if(action === 'remove') {
                element.id = event.target.parentElement.id.split("_").shift()
                event.target.className = "fa fa-heart-o"
            }
        })
    }
}
function handleFavorites ( ) { var fav = document . querySelectorAll ( '[id^="book-key-"]' ) ; for ( const element of fav ) { element . addEventListener ( 'click' , async ( event ) => { let buttonID = event . target . parentElement . id let action ; if ( buttonID . endsWith ( 'active' ) ) { action = 'remove' buttonID = event . target . parentElement . id . split ( "_" ) . shift ( ) buttonID = buttonID . slice ( 9 ) ; buttonID = parseInt ( buttonID ) let toDelete = works [ buttonID ] . workid fetch ( ` ${ toDelete } ` , { method : "DELETE" } ) . then ( res => res . json ( ) ) . then ( ( ) => console . log ( 'success' ) ) } else { action = 'add' buttonID = buttonID . slice ( 9 ) ; buttonID = parseInt ( buttonID ) const response = await fetch ( '/api/action' , { method : 'POST' , headers : { 'Content-Type' : 'application/json' } , body : JSON . stringify ( { action : action , workid : works [ buttonID ] . workid , title : works [ buttonID ] . titleweb , author : works [ buttonID ] . authorweb } ) } ) const success = await response . json ( ) } if ( action === 'add' ) { element . id = event . target . parentElement . id + '_active' event . target . className = "fa fa-heart" } if ( action === 'remove' ) { element . id = event . target . parentElement . id . split ( "_" ) . shift ( ) event . target . className = "fa fa-heart-o" } } ) } }
Listens to the favourite button, responds on click, if filled remove, if not filled add
Listens to the favourite button responds on click if filled remove if not filled add