var loading = false;
$(document).ready(function () {
// make alert button fixed and hide it after a few seconds
$(".alert.msg").addClass("fixed-alert");
setTimeout(function(){ $(".alert.msg").remove(); }, 5000);
// on show replies link click
$(".show-replies").click(function(e){
e.preventDefault();
// check if data is not still loading
if(loading == true) { return false; }
// store self into a var to give it access inside the post
var self = $(this);
// set loading to true and get data from html element attributres
loading = true;
data = $(this).data();
// make an ajax request to get comment replies
$.post("/ajax.php?action=commentReplies&commentId=" + castAsInteger(data.commentid), {})
// data retrieved. try and build the html
.done(function (payload) {
if(payload.error === true) {
// reset loading flag and throw error
loading = false;
throw new Error("Failed to parse ajax payload");
}
// loop data and build replies html
repliesHtml = "";
payload.data.comments.forEach(function(item){
// html data
const commentId = castAsInteger(item.id);
const repliedTo = (item.repliedToUser ? "@" + item.repliedToUser + " " : "" );
const likeBtnClass = item.hasLiked == 1 ? "class='active'" : "";
const dislikeBtnClass = item.hasDisliked == 1 ? "class='active'" : "";
const profileImg = item.profileImgUrl != false ? `` : ``;
//delete form
deleteHtml = "";
if(payload.data.canDelete) {
deleteHtml = `
`;
}
// build replis html (specific to comment type)
repliesHtml += `
${item.profileName} • ${item.datePosted}
${repliedTo} ${item.iv_comment}