I'm working with an API and i need to display movies and like them but i need to like them once. I tried to add a boolean and I had something like this:
let beenLiked = false;
const movieLike = document.getElementById("likeButton")
movieLike.addEventListener('click', () => {
console.log("ok like")
axios.patch(`myUrl`)
beenLiked = true
}, true)
the problem is when I like a movie, i can't like another one because of my boolean, how can I modify it to solve my problem?
If you can get the name of the movie that is supposed to be liked (I don't have your html file to do it for you), you can track the names and do it like this:
let beenLiked = []
const movieLike = document.getElementById("likeButton")
movieLike.addEventListener('click', () => {
const movie = "The Movie" // Get the movie name or id which has to be liked
if(beenLiked.indexOf(movie) === -1){
console.log("ok like")
axios.patch(`myUrl`)
beenLiked.push(movie)
return true
}
console.log("Already liked once")
return false
})
here the simplified version without html:
let beenLiked = []
function a() {
const movie = "The Movie" // Get the movie the likeButton is attached to
if (beenLiked.indexOf(movie) === -1) {
beenLiked.push(movie)
return true
}
return false
}
console.log(a())
console.log(a())