diff --git a/assets/js/likes.js b/assets/js/likes.js index 907a8f7..3803deb 100644 --- a/assets/js/likes.js +++ b/assets/js/likes.js @@ -6,10 +6,20 @@ likesLoader.style.display = "none" let likesAmount = likesCount.innerHTML; -function apiCall(url) { +function onLoadAndClick(){ + likesLoader.style.display = "inline-flex" + likesCount.style.display = "none"; + heartIcon.style.pointerEvents = 'none'; +} + +function apiCall(url, event) { + onLoadAndClick(); ajax(url) .then(function (result) { console.log(result); + if(event === "click"){ + heartIcon.classList.toggle("liked"); + } likesCount.innerHTML = result; likesLoader.style.display = "none" likesCount.style.display = "inline-flex"; @@ -17,7 +27,7 @@ function apiCall(url) { }) .catch(function () { likesLoader.style.display = "none"; - likesCount.style.display = "inline-block"; + likesCount.style.display = "inline-flex"; }); } @@ -26,16 +36,11 @@ var apiUrl = serviceUrl + "/GetPostLikes?siteId=" + siteId + "&postId=" + postId apiCall(apiUrl); heartIcon.addEventListener("click", () => { - likesLoader.style.display = "inline-flex" - likesCount.style.display = "none"; - heartIcon.style.pointerEvents = 'none'; - - heartIcon.classList.toggle("liked"); - if (heartIcon.classList.contains("liked")) { + if (!heartIcon.classList.contains("liked")) { apiUrl = serviceUrl + "/LikePost?siteId=" + siteId + "&postId=" + postId; } else { apiUrl = serviceUrl + "/DislikePost?siteId=" + siteId + "&postId=" + postId; } - apiCall(apiUrl); +apiCall(apiUrl, "click"); }); \ No newline at end of file