dsg
INSERT INTO `cp_filejs` (`id`,`app`,`path`,`modul`,`file_name`,`content`) VALUES ('217','1','u2/creator','auth','js/homepage.js-','import app, {\r\n showToast\r\n} from \\\"./app.js\\\"\r\nimport store from \\\"./store.js\\\"\r\n\r\nimport {\r\n formatPostDate\r\n} from \\\'./utils.js\\\'\r\nimport {\r\n fetchComments,\r\n maybeLikePost,\r\n maybeLikeComment,\r\n addComment,\r\n deletePost,\r\n deleteComment\r\n} from \\\'./api/posts.js\\\'\r\nimport {\r\n getSessionUser\r\n} from \\\"./api/auth.js\\\"\r\nimport {\r\n renderPost\r\n} from \\\"./post-view.js\\\"\r\n\r\nvar $ = Dom7\r\nvar currentPostsPage = 1\r\nvar currentFollowingPostsPage = 1\r\n\r\nvar postsStore = store.getters.posts\r\nvar followingPostsStore = store.getters.followingPosts\r\n\r\nvar totalPostPages = 0\r\nvar totalFPostPages = 0\r\n\r\n// Infinite Scroll Event\r\nvar isFetchingPosts = false\r\nvar activeTab = \\\'latest\\\'\r\nvar refreshed = false\r\n\r\n//screen width\r\nvar containerWidth = window.innerWidth\r\n\r\npostsStore.onUpdated((data) => {\r\n totalPostPages = data.total_pages\r\n //console.log(\\\'total: \\\'+totalPostPages)\r\n //console.log(\\\'data-length: \\\'+data.data.length)\r\n //console.log(\\\'homapege: data.page: \\\'+data.page)\r\n //console.log(\\\'homapege: data.total_pages: \\\'+data.total_pages)\r\n //console.log(new_data)\r\n\r\n if ((data.page == data.total_pages) || (data.new_data.length == 0)) {\r\n $(\\\'.infinite-scroll-preloader.home-posts\\\').hide()\r\n\r\n if (data.data.length == 0) {\r\n $(\\\'#tab-latest .data\\\').html(\\\'

No posts

\\\')\r\n return;\r\n }\r\n }\r\n\r\n displayPosts(data.new_data)\r\n})\r\n\r\nfollowingPostsStore.onUpdated((data) => {\r\n totalFPostPages = data.total_pages\r\n\r\n if ((data.page == data.total_pages) || (data.new_data.length == 0)) {\r\n $(\\\'.infinite-scroll-preloader.home-following-posts\\\').hide()\r\n if (data.data.length == 0) {\r\n $(\\\'#tab-following .data\\\').html(\\\'

No posts

\\\')\r\n return;\r\n }\r\n }\r\n\r\n displayPosts(data.new_data, true)\r\n})\r\n/*\r\nori:\r\n$(document).on(\\\'infinite\\\', \\\'.infinite-scroll-content.home-page\\\', async function () {\r\n if (isFetchingPosts) return\r\n\r\n const totalPages = activeTab === \\\'following\\\' ? totalFPostPages : totalPostPages\r\n const storeName = activeTab === \\\'following\\\' ? \\\'getFollowingPosts\\\' : \\\'getPosts\\\'\r\n\r\n console.log(\\\'---- start event -------------\\\')\r\n console.log(\\\'infinite activeTab: \\\'+activeTab)\r\n console.log(\\\'infinite totalPages: \\\'+totalPages)\r\n console.log(\\\'infinite storeName: \\\'+storeName)\r\n console.log(\\\'infinite totalPostPages: \\\'+totalPostPages)\r\n\r\n if (activeTab === \\\'following\\\') {\r\n currentFollowingPostsPage++\r\n } else {\r\n currentPostsPage++\r\n }\r\n\r\n const currentPage = activeTab === \\\'following\\\' ? currentFollowingPostsPage : currentPostsPage\r\n if (currentPage >= totalPages) {\r\n return\r\n }\r\n\r\n isFetchingPosts = true\r\n\r\n console.log(\\\'infinite currentPostsPage: \\\'+currentPostsPage)\r\n console.log(\\\'infinite currentPage: \\\'+currentPage)\r\n await store.dispatch(storeName, currentPage)\r\n isFetchingPosts = false\r\n})\r\n\r\n*/\r\n\r\n$(document).on(\\\'infinite\\\', \\\'.infinite-scroll-content.home-page\\\', async function () {\r\n if (isFetchingPosts) return\r\n\r\n const totalPages = activeTab === \\\'following\\\' ? totalFPostPages : totalPostPages\r\n const storeName = activeTab === \\\'following\\\' ? \\\'getFollowingPosts\\\' : \\\'getPosts\\\'\r\n\r\n //console.log(\\\'---- start event -------------\\\')\r\n //console.log(\\\'infinite activeTab: \\\'+activeTab)\r\n //console.log(\\\'infinite totalPages: \\\'+totalPages)\r\n //console.log(\\\'infinite storeName: \\\'+storeName)\r\n //console.log(\\\'infinite totalPostPages: \\\'+totalPostPages)\r\n\r\n if (activeTab === \\\'latest\\\') {\r\n currentFollowingPostsPage++\r\n } else {\r\n currentPostsPage++\r\n }\r\n\r\n //console.log(\\\'infinite currentPostsPage 0: \\\'+currentPostsPage)\r\n\r\n const currentPage = activeTab === \\\'latest\\\' ? currentFollowingPostsPage : currentPostsPage\r\n //console.log(\\\'infinite currentPage 1: \\\'+currentPage)\r\n \r\n if(currentPage<=totalPages) {\r\n //console.log(\\\'infinite currentPostsPage 2: \\\'+currentPostsPage)\r\n //console.log(\\\'infinite currentPage 2: \\\'+currentPage)\r\n isFetchingPosts = true\r\n await store.dispatch(storeName, currentPage)\r\n isFetchingPosts = false\r\n }\r\n \r\n})\r\n\r\n$(document).on(\\\'page:beforein\\\', \\\'.page[data-name=\\\"social\\\"]\\\', function (e) {\r\n const ptrContent = app.ptr.get(\\\'.ptr-content.home-page\\\')\r\n ptrContent.on(\\\'refresh\\\', async function () {\r\n refreshed = true\r\n const storeName = activeTab === \\\'following\\\' ? \\\'getFollowingPosts\\\' : \\\'getPosts\\\'\r\n\r\n if (isFetchingPosts) return\r\n\r\n isFetchingPosts = true\r\n\r\n if (activeTab === \\\'following\\\') {\r\n currentFollowingPostsPage = 1\r\n } else {\r\n currentPostsPage = 1\r\n }\r\n\r\n await store.dispatch(storeName, 1)\r\n\r\n isFetchingPosts = false\r\n app.ptr.done()\r\n })\r\n\r\n app.toolbar.show(\\\'.toolbar.toolbar-bottom\\\', true)\r\n})\r\n\r\n/* Based on this http://jsfiddle.net/brettwp/J4djY/*/\r\nexport function detectDoubleTapClosure(callback) {\r\n let lastTap = 0\r\n let timeout\r\n\r\n return function detectDoubleTap(event) {\r\n\r\n const curTime = new Date().getTime()\r\n const tapLen = curTime - lastTap\r\n if (tapLen < 500 && tapLen > 0) {\r\n event.preventDefault()\r\n\r\n // pass the event target to the callback\r\n callback(event.target)\r\n } else {\r\n timeout = setTimeout(() => {\r\n clearTimeout(timeout)\r\n }, 500)\r\n }\r\n\r\n lastTap = curTime\r\n }\r\n}\r\n\r\n// event listener for tab change\r\n$(document).on(\\\'click\\\', \\\'.social-tabs .tab-link\\\', async function (e) {\r\n const type = this.getAttribute(\\\'data-type\\\')\r\n activeTab = type\r\n})\r\n\r\nasync function displayPosts(posts, following = false) {\r\n const postsContainer = $(following ? \\\'#tab-following .data\\\' : \\\'#tab-latest .data\\\');\r\n\r\n if (refreshed) {\r\n postsContainer.html(\\\'\\\')\r\n refreshed = false\r\n }\r\n\r\n const user = await getSessionUser()\r\n\r\n posts.forEach(post => {\r\n let post_actions = `\r\n
\r\n
\r\n ${post.is_liked ? \\\'heart_fill\\\' : \\\'heart\\\'}\r\n
\r\n
\r\n chat_bubble\r\n
\r\n
\r\n paperplane\r\n
\r\n `;\r\n\r\n if (post.user_id == user.id) {\r\n post_actions += `\r\n
\r\n gear_alt\r\n
\r\n `;\r\n }\r\n\r\n post_actions += `
`;\r\n\r\n const date = formatPostDate(post.post_date);\r\n const maxDescriptionLength = 200; // Set your character limit here\r\n const isLongDescription = post.caption.length > maxDescriptionLength;\r\n const shortDescription = isLongDescription ? post.caption.slice(0, maxDescriptionLength) : post.caption;\r\n\r\n let imageHeight = 400;\r\n\r\n if (post.media.length > 0) {\r\n const intrinsicWidth = post.media[0].media_width;\r\n const intrinsicHeight = post.media[0].media_height;\r\n\r\n // Calculate intrinsic aspect ratio\r\n const intrinsicRatio = intrinsicWidth / intrinsicHeight;\r\n\r\n // Calculate the rendered height based on the container width\r\n const renderedHeight = containerWidth / intrinsicRatio;\r\n\r\n // Use either the rendered height or the fallback height\r\n if (renderedHeight > 0) {\r\n\r\n if (renderedHeight > 500) {\r\n imageHeight = 500\r\n } else {\r\n imageHeight = renderedHeight\r\n }\r\n }\r\n\r\n }\r\n\r\n\r\n let profile_link;\r\n\r\n if (post.user_id == user.id) {\r\n profile_link = `\r\n \r\n
\r\n
${post.username}
\r\n \r\n
`\r\n } else {\r\n profile_link = `\r\n \r\n
\r\n
${post.username}
\r\n \r\n
`\r\n }\r\n\r\n const postItem = `\r\n
\r\n
\r\n ${profile_link}\r\n
\r\n
\r\n
\r\n ${post.media.map(mediaItem => `\r\n
\r\n ${mediaItem.media_type === \\\'video\\\' ? \r\n // `\r\n // \r\n // `\r\n \\\'Disabled for testing\\\'\r\n : `\r\n \\\"${mediaItem.caption\r\n `}\r\n
\r\n `).join(\\\'\\\')}\r\n
\r\n
\r\n
\r\n
\r\n ${post_actions}\r\n
${post.likes_count} likes
\r\n
\r\n ${post.username}
${shortDescription}\r\n ${post.caption}\r\n ${isLongDescription ? `... more` : \\\'\\\'}\r\n
\r\n ${post.comments_count > 0 ? `
View ${post.comments_count} comments
` : \\\'\\\'}\r\n
\r\n
\r\n `;\r\n\r\n postsContainer.append(postItem);\r\n });\r\n}\r\n\r\nexport function togglePostLikeV1(postId, single = false) {\r\n // Find the post element and its like icon\r\n let container = single ? `.media-post.single[data-post-id=\\\"${postId}\\\"]` : `.media-post[data-post-id=\\\"${postId}\\\"]`\r\n const postElement = document.querySelector(container)\r\n const likeIcon = postElement.querySelector(\\\'.media-post-like i\\\')\r\n const isLiked = postElement.getAttribute(\\\'data-is-liked\\\') === \\\'true\\\'\r\n const likeCountElem = postElement.querySelector(\\\'.media-post-likecount\\\')\r\n let likeCount = parseInt(likeCountElem.getAttribute(\\\'data-like-count\\\'))\r\n\r\n // Toggle the like state\r\n if (isLiked) {\r\n likeIcon.classList.remove(\\\'text-red\\\')\r\n likeIcon.innerText = \\\'heart\\\'\r\n likeCount--\r\n postElement.setAttribute(\\\'data-is-liked\\\', \\\'false\\\')\r\n } else {\r\n likeIcon.classList.add(\\\'text-red\\\')\r\n likeIcon.innerText = \\\'heart_fill\\\'\r\n likeCount++\r\n postElement.setAttribute(\\\'data-is-liked\\\', \\\'true\\\')\r\n }\r\n\r\n // Update like count\r\n likeCountElem.innerText = `${likeCount} likes`\r\n likeCountElem.setAttribute(\\\'data-like-count\\\', likeCount)\r\n\r\n // Optionally, make an API call to update the like status on the server\r\n // fetch(`/api/posts/${postId}/like`, { method: \\\'POST\\\' });\r\n maybeLikePost(postId)\r\n}\r\n\r\nexport function togglePostLike(postId, single = false) {\r\n // Find all post elements with the specified postId\r\n let container = single ? `.media-post.single[data-post-id=\\\"${postId}\\\"]` : `.media-post[data-post-id=\\\"${postId}\\\"]`\r\n const postElements = document.querySelectorAll(container)\r\n\r\n // Iterate through all matching post elements and update them\r\n postElements.forEach(postElement => {\r\n const likeIcon = postElement.querySelector(\\\'.media-post-like i\\\')\r\n const isLiked = postElement.getAttribute(\\\'data-is-liked\\\') === \\\'true\\\'\r\n const likeCountElem = postElement.querySelector(\\\'.media-post-likecount\\\')\r\n let likeCount = parseInt(likeCountElem.getAttribute(\\\'data-like-count\\\'))\r\n\r\n // Toggle the like state\r\n if (isLiked) {\r\n likeIcon.classList.remove(\\\'text-red\\\')\r\n likeIcon.innerText = \\\'heart\\\'\r\n likeCount--\r\n postElement.setAttribute(\\\'data-is-liked\\\', \\\'false\\\')\r\n } else {\r\n likeIcon.classList.add(\\\'text-red\\\')\r\n likeIcon.innerText = \\\'heart_fill\\\'\r\n likeCount++\r\n postElement.setAttribute(\\\'data-is-liked\\\', \\\'true\\\')\r\n }\r\n\r\n // Update like count\r\n likeCountElem.innerText = `${likeCount} likes`\r\n likeCountElem.setAttribute(\\\'data-like-count\\\', likeCount)\r\n })\r\n\r\n // Optionally, make an API call to update the like status on the server\r\n maybeLikePost(postId)\r\n}\r\n\r\n\r\nfunction displayComments(comments, postId) {\r\n const user = store.getters.user.value\r\n\r\n const commentsContainer = document.getElementById(\\\'comments-list\\\')\r\n // reset the comments container\r\n commentsContainer.innerHTML = \\\'\\\'\r\n const commentForm = document.getElementById(\\\'comment-form\\\')\r\n commentForm.setAttribute(\\\'data-post-id\\\', postId)\r\n\r\n if (!comments.length) {\r\n commentsContainer.innerHTML = \\\'
No comments found
\\\'\r\n return\r\n }\r\n\r\n comments.forEach(comment => {\r\n const replyItems = comment.replies.length > 0 ? `\r\n
\r\n \r\n Show ${comment.replies.length} ${comment.replies.length > 1 ? \\\'replies\\\' : \\\'reply\\\'}\r\n \r\n
\r\n ${comment.replies.map(reply => {\r\n\r\n // Determine the delete button visibility\r\n const deleteButton = reply.user_id == user.id ? \r\n `
\r\n trash\r\n
` : \r\n \\\'\\\';\r\n\r\n return `\r\n
\r\n\r\n \r\n \r\n\r\n
\r\n
\r\n \r\n ${reply.user_login}\r\n \r\n ${formatPostDate(reply.comment_date)}\r\n
\r\n \r\n
${reply.comment}
\r\n
\r\n
\r\n \r\n ${reply.liked ? \\\'heart_fill\\\' : \\\'heart\\\'}\r\n \r\n \r\n ${reply.likes_count}\r\n \r\n
\r\n
\r\n chat_bubble Reply\r\n
\r\n ${deleteButton}\r\n
\r\n
\r\n
\r\n
`;\r\n }).join(\\\'\\\')}\r\n
\r\n
` : \\\'\\\';\r\n\r\n let commenter_link = `/profile-view/${comment.user_id}`;\r\n\r\n if (comment.user_id == user.id) {\r\n commenter_link = \\\'/profile/\\\';\r\n }\r\n\r\n const deleteButton = comment.user_id == user.id ?\r\n `
trash
` : \\\'\\\';\r\n\r\n const commentItem = `\r\n
\r\n\r\n \r\n \r\n
\r\n
\r\n \r\n ${comment.user_login}\r\n \r\n ${formatPostDate(comment.comment_date)}\r\n
\r\n
${comment.comment}
\r\n
\r\n
\r\n ${comment.liked ? \\\'heart_fill\\\' : \\\'heart\\\'} \r\n \r\n ${comment.likes_count}\r\n \r\n
\r\n
\r\n chat_bubble Reply\r\n
\r\n ${deleteButton}\r\n
\r\n ${replyItems}\r\n
\r\n
\r\n
\r\n `\r\n commentsContainer.insertAdjacentHTML(\\\'beforeend\\\', commentItem)\r\n })\r\n\r\n // Add click event listener for liking a comment\r\n const likeButtons = document.querySelectorAll(\\\'.comment-like\\\')\r\n likeButtons.forEach(button => {\r\n button.addEventListener(\\\'click\\\', (event) => {\r\n const commentId = event.currentTarget.closest(\\\'.comment\\\').getAttribute(\\\'data-comment-id\\\')\r\n const ownerId = event.currentTarget.closest(\\\'.comment\\\').getAttribute(\\\'data-owner-id\\\')\r\n toggleCommentLike(commentId, ownerId)\r\n })\r\n })\r\n}\r\n\r\nfunction toggleCommentLike(commentId, ownerId) {\r\n // Find the comment element and its like icon\r\n const commentElement = document.querySelector(`.comment[data-comment-id=\\\"${commentId}\\\"]`)\r\n const likeIcon = commentElement.querySelector(\\\'.comment-like i\\\')\r\n const isLiked = commentElement.getAttribute(\\\'data-is-liked\\\') === \\\'true\\\'\r\n const likeCountElem = commentElement.querySelector(\\\'.comment-likes-count\\\')\r\n let likeCount = parseInt(likeCountElem.getAttribute(\\\'data-likes-count\\\'))\r\n\r\n // Toggle the like state\r\n if (isLiked) {\r\n likeIcon.classList.remove(\\\'text-red\\\')\r\n likeIcon.innerText = \\\'heart\\\'\r\n likeCount--\r\n commentElement.setAttribute(\\\'data-is-liked\\\', \\\'false\\\')\r\n } else {\r\n likeIcon.classList.add(\\\'text-red\\\')\r\n likeIcon.innerText = \\\'heart_fill\\\'\r\n likeCount++\r\n commentElement.setAttribute(\\\'data-is-liked\\\', \\\'true\\\')\r\n }\r\n\r\n // Update like count\r\n likeCountElem.innerText = likeCount\r\n likeCountElem.setAttribute(\\\'data-likes-count\\\', likeCount)\r\n\r\n maybeLikeComment(commentId, ownerId)\r\n}\r\n\r\n$(document).on(\\\'click\\\', \\\'.media-post-readmore\\\', function () {\r\n const postDescription = this.previousElementSibling.previousElementSibling; // The short description\r\n const fullDescription = this.previousElementSibling; // The full description\r\n\r\n if (fullDescription.classList.contains(\\\'hidden\\\')) {\r\n postDescription.classList.add(\\\'hidden\\\');\r\n fullDescription.classList.remove(\\\'hidden\\\');\r\n this.textContent = \\\'... less\\\';\r\n } else {\r\n postDescription.classList.remove(\\\'hidden\\\');\r\n fullDescription.classList.add(\\\'hidden\\\');\r\n this.textContent = \\\'... more\\\';\r\n }\r\n});\r\n\r\n$(document).on(\\\'click\\\', \\\'.media-post-like i\\\', (e) => {\r\n const postId = e.target.getAttribute(\\\'data-post-id\\\')\r\n\r\n const parent = e.target.closest(\\\'.media-post\\\')\r\n const isSingle = parent.classList.contains(\\\'single\\\') ? true : false\r\n\r\n togglePostLike(postId, isSingle)\r\n})\r\n\r\n// set the post id as a data attribute from the edit post popup\r\n$(document).on(\\\'click\\\', \\\'.media-post-edit\\\', function () {\r\n const postId = $(this).closest(\\\'.media-post\\\').attr(\\\'data-post-id\\\')\r\n const isSingleView = $(this).closest(\\\'.media-post\\\').hasClass(\\\'single\\\')\r\n $(\\\'.edit-post-popup\\\').attr(\\\'data-post-id\\\', postId)\r\n $(\\\'.edit-post-popup\\\').attr(\\\'data-is-single\\\', isSingleView)\r\n})\r\n\r\n$(document).on(\\\'click\\\', \\\'#delete-post\\\', function () {\r\n var view = app.views.current\r\n\r\n // set the post id as a data attribute from the edit post popup\r\n const postId = $(\\\'.edit-post-popup\\\').attr(\\\'data-post-id\\\')\r\n const isSingleView = $(\\\'.edit-post-popup\\\').attr(\\\'data-is-single\\\')\r\n\r\n app.dialog.confirm(\\\'Are you sure you want to delete this post?\\\', \\\'Delete Post\\\', async () => {\r\n const response = await deletePost(postId)\r\n\r\n if (response) {\r\n store.dispatch(\\\'getMyPosts\\\', {\r\n page: 1,\r\n clear: true\r\n })\r\n store.dispatch(\\\'getMyTags\\\', {\r\n page: 1,\r\n clear: true\r\n })\r\n\r\n if (isSingleView) {\r\n view.router.navigate(\\\'/profile/\\\')\r\n }\r\n\r\n showToast(\\\'Post deleted successfully\\\')\r\n // remove the post from the DOM\r\n $(`.media-post[data-post-id=\\\"${postId}\\\"]`).remove()\r\n app.popup.close(\\\'.edit-post-popup\\\')\r\n } else {\r\n showToast(\\\'Failed to delete post\\\')\r\n }\r\n })\r\n})\r\n\r\n$(document).on(\\\'click\\\', \\\'#edit-post\\\', function () {\r\n var view = app.views.current\r\n\r\n // set the post id as a data attribute from the edit post popup\r\n const postId = $(\\\'.edit-post-popup\\\').attr(\\\'data-post-id\\\')\r\n view.router.navigate(`/post-edit/${postId}`, {\r\n force: true\r\n })\r\n\r\n app.popup.close(\\\'.edit-post-popup\\\')\r\n})\r\n\r\n$(document).on(\\\'touchstart\\\', \\\'.media-post-content .swiper-wrapper\\\', detectDoubleTapClosure((e) => {\r\n return // Disable double tap for now\r\n\r\n const parent = e.closest(\\\'.media-post\\\')\r\n const postId = parent.getAttribute(\\\'data-post-id\\\')\r\n const isLiked = parent.getAttribute(\\\'data-is-liked\\\') === \\\'true\\\'\r\n\r\n if (isLiked) {\r\n return\r\n }\r\n\r\n togglePostLike(postId)\r\n}), {\r\n passive: false\r\n})\r\n\r\n// media-post-video click\r\n$(document).on(\\\'click\\\', \\\'.media-post-video\\\', function () {\r\n if (this.paused) {\r\n this.play()\r\n } else {\r\n this.pause()\r\n }\r\n})\r\n\r\n// on .popup-open click\r\n$(document).on(\\\'click\\\', \\\'.media-post-comment, .media-post-commentcount\\\', async function () {\r\n const postId = this.getAttribute(\\\'data-post-id\\\')\r\n\r\n if (!postId) {\r\n return\r\n }\r\n\r\n document.getElementById(\\\'comments-list\\\').innerHTML = \\\'
\\\'\r\n document.getElementById(\\\'comment-form\\\').reset()\r\n\r\n // update the post id in the comment form\r\n document.getElementById(\\\'comment-form\\\').setAttribute(\\\'data-post-id\\\', \\\'\\\')\r\n document.getElementById(\\\'comment-form\\\').removeAttribute(\\\'data-comment-id\\\')\r\n\r\n document.getElementById(\\\'comment-form\\\').querySelector(\\\'.replying-to\\\').innerHTML = \\\'\\\'\r\n document.getElementById(\\\'comment-form\\\').querySelector(\\\'.replying-to\\\').classList.add(\\\'hidden\\\')\r\n\r\n try {\r\n const comments = await fetchComments(postId)\r\n displayComments(comments, postId)\r\n } catch (error) {\r\n app.notification.create({\r\n titleRightText: \\\'now\\\',\r\n subtitle: \\\'Oops, something went wrong\\\',\r\n text: error.message || \\\'Failed to fetch comments\\\',\r\n }).open()\r\n }\r\n})\r\n\r\n$(document).on(\\\'click\\\', \\\'.media-post-share\\\', function () {\r\n // set the post id as a data attribute \r\n const postId = $(this).closest(\\\'.media-post\\\').attr(\\\'data-post-id\\\')\r\n $(\\\'.share-popup\\\').attr(\\\'data-post-id\\\', postId)\r\n $(\\\'#copy-link\\\').attr(\\\'data-clipboard-text\\\', `${window.location.origin}/post-view/${postId}`)\r\n})\r\n\r\n$(document).on(\\\'click\\\', \\\'#share-post-email\\\', function () {\r\n const postId = $(this).closest(\\\'.popup\\\').attr(\\\'data-post-id\\\')\r\n const postLink = `${window.location.origin}/post-view/${postId}`\r\n\r\n // open the email composer\r\n window.open(`mailto:?subject=Check out this post&body=${postLink}`)\r\n})\r\n\r\n// data-clipboard-text click\r\n$(document).on(\\\'click\\\', \\\'#copy-link\\\', function () {\r\n const copyText = $(this).attr(\\\'data-clipboard-text\\\')\r\n navigator.clipboard.writeText(copyText)\r\n\r\n app.toast.create({\r\n text: \\\'Link copied to clipboard\\\',\r\n closeTimeout: 2000\r\n }).open()\r\n})\r\n\r\n// on .comment-replies-toggle click\r\n$(document).on(\\\'click\\\', \\\'.comment-replies-toggle\\\', function () {\r\n const commentRepliesContainer = this.nextElementSibling\r\n commentRepliesContainer.classList.toggle(\\\'show\\\')\r\n const repliesCount = this.getAttribute(\\\'data-replies-count\\\')\r\n\r\n this.innerText = this.innerText === `Show ${repliesCount} replies` ? `Hide ${repliesCount} replies` : `Show ${repliesCount} replies`\r\n})\r\n\r\n// on comment form submit\r\n$(\\\'#comment-form\\\').on(\\\'submit\\\', async function (e) {\r\n e.preventDefault()\r\n\r\n const postId = this.getAttribute(\\\'data-post-id\\\')\r\n const commentId = this.getAttribute(\\\'data-comment-id\\\')\r\n const comment = this.comment.value\r\n\r\n if (!comment) {\r\n // app.dialog.alert(\\\'Please enter a comment\\\')\r\n return\r\n }\r\n\r\n app.preloader.show()\r\n\r\n try {\r\n const response = await addComment(postId, comment, commentId)\r\n\r\n app.preloader.hide()\r\n\r\n if (response) {\r\n this.reset()\r\n this.removeAttribute(\\\'data-comment-id\\\')\r\n this.querySelector(\\\'.replying-to\\\').innerHTML = \\\'\\\'\r\n this.querySelector(\\\'.replying-to\\\').classList.add(\\\'hidden\\\')\r\n const comments = await fetchComments(postId)\r\n displayComments(comments, postId)\r\n } else {\r\n app.notification.create({\r\n text: \\\'Failed to add comment\\\',\r\n titleRightText: \\\'now\\\',\r\n subtitle: \\\'Oops, something went wrong\\\',\r\n }).open()\r\n }\r\n } catch (error) {\r\n app.notification.create({\r\n titleRightText: \\\'now\\\',\r\n subtitle: \\\'Oops, something went wrong\\\',\r\n text: error.message || \\\'Failed to add comment\\\',\r\n }).open()\r\n app.preloader.hide()\r\n }\r\n})\r\n\r\n//.comment-reply click\r\n$(document).on(\\\'click\\\', \\\'.comment-reply\\\', function () {\r\n // get the comment id, and comment owner id\r\n const commentId = this.closest(\\\'.comment\\\').getAttribute(\\\'data-comment-id\\\')\r\n const ownerId = this.closest(\\\'.comment\\\').getAttribute(\\\'data-owner-id\\\')\r\n const ownerName = this.closest(\\\'.comment\\\').getAttribute(\\\'data-owner-name\\\')\r\n\r\n // add something above the comment form to show the user they are replying to a comment\r\n // add the comment id to the form\r\n document.getElementById(\\\'comment-form\\\').setAttribute(\\\'data-comment-id\\\', commentId)\r\n document.getElementById(\\\'comment-form\\\').comment.focus()\r\n\r\n // add the owner name to the form\r\n // Replying to m88xrk\r\n const replyingTo = document.getElementById(\\\'comment-form\\\').querySelector(\\\'.replying-to\\\')\r\n replyingTo.innerHTML = `Replying to ${ownerName}`\r\n replyingTo.classList.remove(\\\'hidden\\\')\r\n document.getElementById(\\\'comment-form\\\').prepend(replyingTo)\r\n})\r\n\r\n$(document).on(\\\'click\\\', \\\'.comment-delete\\\', async function () {\r\n app.dialog.confirm(\\\'Are you sure you want to delete this comment? This will remove all replies to this comment\\\', \\\'Delete Comment\\\', async () => {\r\n try {\r\n const commentId = this.getAttribute(\\\'data-comment-id\\\')\r\n const response = await deleteComment(commentId)\r\n\r\n if (response && response.success) {\r\n // remove the comment from the DOM\r\n $(`.comment[data-comment-id=\\\"${commentId}\\\"]`).remove()\r\n showToast(\\\'Comment deleted successfully\\\')\r\n }\r\n } catch (error) {\r\n app.dialog.alert(\\\'Failed to delete comment\\\')\r\n }\r\n })\r\n})\r\n\r\n$(document).on(\\\'click\\\', \\\'.comment a\\\', function (e) {\r\n var view = app.views.current\r\n // hide the comments popup\r\n app.popup.close()\r\n\r\n // get the href attribute\r\n const href = this.getAttribute(\\\'data-url\\\')\r\n\r\n if (!href || href === \\\'#\\\') {\r\n return\r\n }\r\n\r\n // prevent the default action\r\n e.preventDefault()\r\n\r\n view.router.navigate(href, {\r\n force: true\r\n })\r\n})');
Copy This

Warning: file_get_contents(/home/u340524018/domains/agniaga.com/public_html/sub/u2/creator/js/homepage.js-): Failed to open stream: No such file or directory in /home/u340524018/domains/agniaga.com/public_html/sub/u2/cp/inc.filejs.php on line 190
import app, {
  showToast
} from \"./app.js\"
import store from \"./store.js\"

import {
  formatPostDate
} from \'./utils.js\'
import {
  fetchComments,
  maybeLikePost,
  maybeLikeComment,
  addComment,
  deletePost,
  deleteComment
} from \'./api/posts.js\'
import {
  getSessionUser
} from \"./api/auth.js\"
import {
  renderPost
} from \"./post-view.js\"

var $ = Dom7
var currentPostsPage = 1
var currentFollowingPostsPage = 1

var postsStore = store.getters.posts
var followingPostsStore = store.getters.followingPosts

var totalPostPages = 0
var totalFPostPages = 0

// Infinite Scroll Event
var isFetchingPosts = false
var activeTab = \'latest\'
var refreshed = false

//screen width
var containerWidth = window.innerWidth

postsStore.onUpdated((data) => {
  totalPostPages = data.total_pages
  //console.log(\'total: \'+totalPostPages)
  //console.log(\'data-length: \'+data.data.length)
  //console.log(\'homapege: data.page: \'+data.page)
  //console.log(\'homapege: data.total_pages: \'+data.total_pages)
  //console.log(new_data)

  if ((data.page == data.total_pages) || (data.new_data.length == 0)) {
    $(\'.infinite-scroll-preloader.home-posts\').hide()

    if (data.data.length == 0) {
      $(\'#tab-latest .data\').html(\'<p class=\"text-center\">No posts</p>\')
      return;
    }
  }

  displayPosts(data.new_data)
})

followingPostsStore.onUpdated((data) => {
  totalFPostPages = data.total_pages

  if ((data.page == data.total_pages) || (data.new_data.length == 0)) {
    $(\'.infinite-scroll-preloader.home-following-posts\').hide()
    if (data.data.length == 0) {
      $(\'#tab-following .data\').html(\'<p class=\"text-center\">No posts</p>\')
      return;
    }
  }

  displayPosts(data.new_data, true)
})
/*
ori:
$(document).on(\'infinite\', \'.infinite-scroll-content.home-page\', async function () {
  if (isFetchingPosts) return

  const totalPages = activeTab === \'following\' ? totalFPostPages : totalPostPages
  const storeName = activeTab === \'following\' ? \'getFollowingPosts\' : \'getPosts\'

  console.log(\'---- start event -------------\')
  console.log(\'infinite activeTab: \'+activeTab)
  console.log(\'infinite totalPages: \'+totalPages)
  console.log(\'infinite storeName: \'+storeName)
  console.log(\'infinite totalPostPages: \'+totalPostPages)

  if (activeTab === \'following\') {
    currentFollowingPostsPage++
  } else {
    currentPostsPage++
  }

  const currentPage = activeTab === \'following\' ? currentFollowingPostsPage : currentPostsPage
  if (currentPage >= totalPages) {
    return
  }

  isFetchingPosts = true

  console.log(\'infinite currentPostsPage: \'+currentPostsPage)
  console.log(\'infinite currentPage: \'+currentPage)
  await store.dispatch(storeName, currentPage)
  isFetchingPosts = false
})

*/

$(document).on(\'infinite\', \'.infinite-scroll-content.home-page\', async function () {
  if (isFetchingPosts) return

  const totalPages = activeTab === \'following\' ? totalFPostPages : totalPostPages
  const storeName = activeTab === \'following\' ? \'getFollowingPosts\' : \'getPosts\'

  //console.log(\'---- start event -------------\')
  //console.log(\'infinite activeTab: \'+activeTab)
  //console.log(\'infinite totalPages: \'+totalPages)
  //console.log(\'infinite storeName: \'+storeName)
  //console.log(\'infinite totalPostPages: \'+totalPostPages)

  if (activeTab === \'latest\') {
    currentFollowingPostsPage++
  } else {
    currentPostsPage++
  }

  //console.log(\'infinite currentPostsPage 0: \'+currentPostsPage)

  const currentPage = activeTab === \'latest\' ? currentFollowingPostsPage : currentPostsPage
  //console.log(\'infinite currentPage 1: \'+currentPage)
  
  if(currentPage<=totalPages) {
    //console.log(\'infinite currentPostsPage 2: \'+currentPostsPage)
    //console.log(\'infinite currentPage 2: \'+currentPage)
    isFetchingPosts = true
    await store.dispatch(storeName, currentPage)
    isFetchingPosts = false
  }
  
})

$(document).on(\'page:beforein\', \'.page[data-name=\"social\"]\', function (e) {
  const ptrContent = app.ptr.get(\'.ptr-content.home-page\')
  ptrContent.on(\'refresh\', async function () {
    refreshed = true
    const storeName = activeTab === \'following\' ? \'getFollowingPosts\' : \'getPosts\'

    if (isFetchingPosts) return

    isFetchingPosts = true

    if (activeTab === \'following\') {
      currentFollowingPostsPage = 1
    } else {
      currentPostsPage = 1
    }

    await store.dispatch(storeName, 1)

    isFetchingPosts = false
    app.ptr.done()
  })

  app.toolbar.show(\'.toolbar.toolbar-bottom\', true)
})

/* Based on this http://jsfiddle.net/brettwp/J4djY/*/
export function detectDoubleTapClosure(callback) {
  let lastTap = 0
  let timeout

  return function detectDoubleTap(event) {

    const curTime = new Date().getTime()
    const tapLen = curTime - lastTap
    if (tapLen < 500 && tapLen > 0) {
      event.preventDefault()

      // pass the event target to the callback
      callback(event.target)
    } else {
      timeout = setTimeout(() => {
        clearTimeout(timeout)
      }, 500)
    }

    lastTap = curTime
  }
}

// event listener for tab change
$(document).on(\'click\', \'.social-tabs .tab-link\', async function (e) {
  const type = this.getAttribute(\'data-type\')
  activeTab = type
})

async function displayPosts(posts, following = false) {
  const postsContainer = $(following ? \'#tab-following .data\' : \'#tab-latest .data\');

  if (refreshed) {
    postsContainer.html(\'\')
    refreshed = false
  }

  const user = await getSessionUser()

  posts.forEach(post => {
    let post_actions = `
      <div class=\"media-post-actions\">
        <div class=\"media-post-like\" data-post-id=\"${post.id}\">
          <i class=\"icon f7-icons ${post.is_liked ? \'text-red\' : \'\'}\" data-post-id=\"${post.id}\">${post.is_liked ? \'heart_fill\' : \'heart\'}</i>
        </div>
        <div class=\"media-post-comment popup-open\" data-popup=\".comments-popup\" data-post-id=\"${post.id}\">
          <i class=\"icon f7-icons\">chat_bubble</i>
        </div>
        <div class=\"media-post-share popup-open\" data-popup=\".share-popup\">
          <i class=\"icon f7-icons\">paperplane</i>
        </div>
    `;

    if (post.user_id == user.id) {
      post_actions += `
        <div class=\"media-post-edit popup-open\" data-popup=\".edit-post-popup\" data-post-id=\"${post.id}\">
          <i class=\"icon f7-icons\">gear_alt</i>
        </div>
      `;
    }

    post_actions += `</div>`;

    const date = formatPostDate(post.post_date);
    const maxDescriptionLength = 200; // Set your character limit here
    const isLongDescription = post.caption.length > maxDescriptionLength;
    const shortDescription = isLongDescription ? post.caption.slice(0, maxDescriptionLength) : post.caption;

    let imageHeight = 400;

    if (post.media.length > 0) {
      const intrinsicWidth = post.media[0].media_width;
      const intrinsicHeight = post.media[0].media_height;

      // Calculate intrinsic aspect ratio
      const intrinsicRatio = intrinsicWidth / intrinsicHeight;

      // Calculate the rendered height based on the container width
      const renderedHeight = containerWidth / intrinsicRatio;

      // Use either the rendered height or the fallback height
      if (renderedHeight > 0) {

        if (renderedHeight > 500) {
          imageHeight = 500
        } else {
          imageHeight = renderedHeight
        }
      }

    }


    let profile_link;

    if (post.user_id == user.id) {
      profile_link = `
      <a href=\"#\" class=\"view-profile media-post-header\">
        <div class=\"media-post-avatar\" style=\"background-image: url(\'${post.user_profile_image || \'assets/img/profile-placeholder.jpg\'}\');\"></div>
        <div class=\"media-post-user\">${post.username}</div>
        <div class=\"media-post-date\">${date}</div>
      </a>`
    } else {
      profile_link = `
      <a href=\"/profile-view/${post.user_id}\" class=\"media-post-header\">
        <div class=\"media-post-avatar\" style=\"background-image: url(\'${post.user_profile_image || \'assets/img/profile-placeholder.jpg\'}\');\"></div>
        <div class=\"media-post-user\">${post.username}</div>
        <div class=\"media-post-date\">${date}</div>
      </a>`
    }

    const postItem = `
      <div class=\"media-post\" data-post-id=\"${post.id}\" data-is-liked=\"${post.is_liked}\">
        <div class=\"media-post-content\">
          ${profile_link}
          <div class=\"media-post-content\">
            <div class=\"swiper-container\">
              <div class=\"swiper-wrapper\">
                ${post.media.map(mediaItem => `
                  <div class=\"swiper-slide post-media\" style=\"height: ${imageHeight}px; \">
                    ${mediaItem.media_type === \'video\' ? 
                    // `
                    //   <video autoplay loop muted playsinline class=\"video-background media-post-video\" id=\"${mediaItem.id}\">
                    //     <source src=\"${mediaItem.media_url}\" type=\"${mediaItem.media_mime_type}\" />
                    //   </video>
                    // `
                    \'Disabled for testing\'
                     : `
                      <img 
                        src=\"${mediaItem.media_url}\" 
                        alt=\"${mediaItem.caption || post.username + \'s post\'}\"
                        loading=\"lazy\"
                        style=\"text-align: center;\"
                        onerror = \"this.style.display=\'none\';\"
                      />
                    `}
                  </div>
                `).join(\'\')}
              </div>
              <div class=\"swiper-pagination\"></div>
            </div>
          </div>
          ${post_actions}
          <div class=\"media-post-likecount\" data-like-count=\"${post.likes_count}\">${post.likes_count} likes</div>
          <div class=\"media-post-description\">
            <strong>${post.username}</strong> <br/> <span class=\"post-caption\">${shortDescription}</span>
            <span class=\"full-description hidden\">${post.caption}</span>
            ${isLongDescription ? `<span class=\"media-post-readmore\">... more</span>` : \'\'}
          </div>
          ${post.comments_count > 0 ? `<div class=\"media-post-commentcount popup-open\" data-popup=\".comments-popup\" data-post-id=\"${post.id}\">View ${post.comments_count} comments</div>` : \'\'}
        </div>
      </div>
    `;

    postsContainer.append(postItem);
  });
}

export function togglePostLikeV1(postId, single = false) {
  // Find the post element and its like icon
  let container = single ? `.media-post.single[data-post-id=\"${postId}\"]` : `.media-post[data-post-id=\"${postId}\"]`
  const postElement = document.querySelector(container)
  const likeIcon = postElement.querySelector(\'.media-post-like i\')
  const isLiked = postElement.getAttribute(\'data-is-liked\') === \'true\'
  const likeCountElem = postElement.querySelector(\'.media-post-likecount\')
  let likeCount = parseInt(likeCountElem.getAttribute(\'data-like-count\'))

  // Toggle the like state
  if (isLiked) {
    likeIcon.classList.remove(\'text-red\')
    likeIcon.innerText = \'heart\'
    likeCount--
    postElement.setAttribute(\'data-is-liked\', \'false\')
  } else {
    likeIcon.classList.add(\'text-red\')
    likeIcon.innerText = \'heart_fill\'
    likeCount++
    postElement.setAttribute(\'data-is-liked\', \'true\')
  }

  // Update like count
  likeCountElem.innerText = `${likeCount} likes`
  likeCountElem.setAttribute(\'data-like-count\', likeCount)

  // Optionally, make an API call to update the like status on the server
  // fetch(`/api/posts/${postId}/like`, { method: \'POST\' });
  maybeLikePost(postId)
}

export function togglePostLike(postId, single = false) {
  // Find all post elements with the specified postId
  let container = single ? `.media-post.single[data-post-id=\"${postId}\"]` : `.media-post[data-post-id=\"${postId}\"]`
  const postElements = document.querySelectorAll(container)

  // Iterate through all matching post elements and update them
  postElements.forEach(postElement => {
    const likeIcon = postElement.querySelector(\'.media-post-like i\')
    const isLiked = postElement.getAttribute(\'data-is-liked\') === \'true\'
    const likeCountElem = postElement.querySelector(\'.media-post-likecount\')
    let likeCount = parseInt(likeCountElem.getAttribute(\'data-like-count\'))

    // Toggle the like state
    if (isLiked) {
      likeIcon.classList.remove(\'text-red\')
      likeIcon.innerText = \'heart\'
      likeCount--
      postElement.setAttribute(\'data-is-liked\', \'false\')
    } else {
      likeIcon.classList.add(\'text-red\')
      likeIcon.innerText = \'heart_fill\'
      likeCount++
      postElement.setAttribute(\'data-is-liked\', \'true\')
    }

    // Update like count
    likeCountElem.innerText = `${likeCount} likes`
    likeCountElem.setAttribute(\'data-like-count\', likeCount)
  })

  // Optionally, make an API call to update the like status on the server
  maybeLikePost(postId)
}


function displayComments(comments, postId) {
  const user = store.getters.user.value

  const commentsContainer = document.getElementById(\'comments-list\')
  // reset the comments container
  commentsContainer.innerHTML = \'\'
  const commentForm = document.getElementById(\'comment-form\')
  commentForm.setAttribute(\'data-post-id\', postId)

  if (!comments.length) {
    commentsContainer.innerHTML = \'<div class=\"no-comments\">No comments found</div>\'
    return
  }

  comments.forEach(comment => {
    const replyItems = comment.replies.length > 0 ? `
        <div class=\"comment-replies\">
          <span class=\"comment-replies-toggle\" data-replies-count=\"${comment.replies.length}\">
            Show ${comment.replies.length} ${comment.replies.length > 1 ? \'replies\' : \'reply\'}
          </span>
          <div class=\"comment-replies-container\">
            ${comment.replies.map(reply => {

              // Determine the delete button visibility
              const deleteButton = reply.user_id == user.id ? 
                `<div class=\"comment-delete\" data-comment-id=\"${reply.id}\">
                <i class=\"icon f7-icons text-red\">trash</i>
                </div>` : 
                \'\';

              return `
                <div class=\"comment\" data-comment-id=\"${reply.id}\" data-is-liked=\"${reply.liked}\" data-owner-id=\"${reply.user_id}\"
                  data-owner-name=\"${reply.user_login}\">

                  <a href=\"#\" data-url=\"${reply.user_id == user.id ? \'#\' : `/profile-view/${reply.user_id}`}\" class=\"${reply.user_id == user.id ? \'view-profile\' : \'\'} comment-profile-img\" style=\"background-image:url(\'${reply.profile_image || \'assets/img/profile-placeholder.jpg\'}\');\">
                  </a>

                  <div class=\"comment-content-container\">
                    <div class=\"comment-username\">
                      <a href=\"#\" data-url=\"${reply.user_id == user.id ? \'#\' : `/profile-view/${reply.user_id}`}\" class=\"${reply.user_id == user.id ? \'view-profile\' : \'a\'}\">
                        ${reply.user_login}
                      </a>
                      <span class=\"date\">${formatPostDate(reply.comment_date)}</span>
                    </div>
                    
                    <div class=\"comment-content\">${reply.comment}</div>
                    <div class=\"comment-actions\">
                      <div class=\"comment-like\">
                        <i class=\"icon f7-icons ${reply.liked ? \'text-red\' : \'\'}\">
                          ${reply.liked ? \'heart_fill\' : \'heart\'}
                        </i> 
                        <span class=\"comment-likes-count\" data-likes-count=\"${reply.likes_count}\">
                          ${reply.likes_count}
                        </span>
                      </div>
                      <div class=\"comment-reply\">
                        <i class=\"icon f7-icons\">chat_bubble</i> <span>Reply</span>
                      </div>
                      ${deleteButton}
                    </div>
                  </div>
                  <div class=\"clearfix\"></div>
                </div>`;
            }).join(\'\')}
          </div>
        </div>` : \'\';

    let commenter_link = `/profile-view/${comment.user_id}`;

    if (comment.user_id == user.id) {
      commenter_link = \'/profile/\';
    }

    const deleteButton = comment.user_id == user.id ?
      `<div class=\"comment-delete\" data-comment-id=\"${comment.id}\"><i class=\"icon f7-icons text-red\">trash</i></div>` : \'\';

    const commentItem = `
      <div class=\"comment\" 
        data-comment-id=\"${comment.id}\" 
        data-is-liked=\"${comment.liked}\" 
        data-owner-id=\"${comment.user_id}\"
        data-owner-name=\"${comment.user_login}\">

         <a href=\"#\" data-url=\"${comment.user_id == user.id ? \'#\' : `/profile-view/${comment.user_id}`}\" class=\"${comment.user_id == user.id ? \'view-profile\' : \'\'} comment-profile-img\" 
         style=\"background-image:url(\'${comment.profile_image || \'assets/img/profile-placeholder.jpg\'}\');\">
         </a>
        <div class=\"comment-content-container\">
          <div class=\"comment-username\">
            <a href=\"#\" data-url=\"${comment.user_id == user.id ? \'#\' : `/profile-view/${comment.user_id}`}\" class=\"${comment.user_id == user.id ? \'view-profile\' : \'a\'}\">
                ${comment.user_login}
            </a>
            <span class=\"date\">${formatPostDate(comment.comment_date)}</span>
          </div>
          <div class=\"comment-content\">${comment.comment}</div>
          <div class=\"comment-actions\">
            <div class=\"comment-like\">
              <i class=\"icon f7-icons ${comment.liked && \'text-red\'}\">${comment.liked ? \'heart_fill\' : \'heart\'}</i> 
              <span class=\"comment-likes-count\" data-likes-count=\"${comment.likes_count}\">
                ${comment.likes_count}
              </span>
            </div>
            <div class=\"comment-reply\">
              <i class=\"icon f7-icons\">chat_bubble</i> <span>Reply</span>
            </div>
            ${deleteButton}
          </div>
          ${replyItems}
        </div>
        <div class=\"clearfix\"></div>
      </div>
    `
    commentsContainer.insertAdjacentHTML(\'beforeend\', commentItem)
  })

  // Add click event listener for liking a comment
  const likeButtons = document.querySelectorAll(\'.comment-like\')
  likeButtons.forEach(button => {
    button.addEventListener(\'click\', (event) => {
      const commentId = event.currentTarget.closest(\'.comment\').getAttribute(\'data-comment-id\')
      const ownerId = event.currentTarget.closest(\'.comment\').getAttribute(\'data-owner-id\')
      toggleCommentLike(commentId, ownerId)
    })
  })
}

function toggleCommentLike(commentId, ownerId) {
  // Find the comment element and its like icon
  const commentElement = document.querySelector(`.comment[data-comment-id=\"${commentId}\"]`)
  const likeIcon = commentElement.querySelector(\'.comment-like i\')
  const isLiked = commentElement.getAttribute(\'data-is-liked\') === \'true\'
  const likeCountElem = commentElement.querySelector(\'.comment-likes-count\')
  let likeCount = parseInt(likeCountElem.getAttribute(\'data-likes-count\'))

  // Toggle the like state
  if (isLiked) {
    likeIcon.classList.remove(\'text-red\')
    likeIcon.innerText = \'heart\'
    likeCount--
    commentElement.setAttribute(\'data-is-liked\', \'false\')
  } else {
    likeIcon.classList.add(\'text-red\')
    likeIcon.innerText = \'heart_fill\'
    likeCount++
    commentElement.setAttribute(\'data-is-liked\', \'true\')
  }

  // Update like count
  likeCountElem.innerText = likeCount
  likeCountElem.setAttribute(\'data-likes-count\', likeCount)

  maybeLikeComment(commentId, ownerId)
}

$(document).on(\'click\', \'.media-post-readmore\', function () {
  const postDescription = this.previousElementSibling.previousElementSibling; // The short description
  const fullDescription = this.previousElementSibling; // The full description

  if (fullDescription.classList.contains(\'hidden\')) {
    postDescription.classList.add(\'hidden\');
    fullDescription.classList.remove(\'hidden\');
    this.textContent = \'... less\';
  } else {
    postDescription.classList.remove(\'hidden\');
    fullDescription.classList.add(\'hidden\');
    this.textContent = \'... more\';
  }
});

$(document).on(\'click\', \'.media-post-like i\', (e) => {
  const postId = e.target.getAttribute(\'data-post-id\')

  const parent = e.target.closest(\'.media-post\')
  const isSingle = parent.classList.contains(\'single\') ? true : false

  togglePostLike(postId, isSingle)
})

// set the post id as a data attribute from the edit post popup
$(document).on(\'click\', \'.media-post-edit\', function () {
  const postId = $(this).closest(\'.media-post\').attr(\'data-post-id\')
  const isSingleView = $(this).closest(\'.media-post\').hasClass(\'single\')
  $(\'.edit-post-popup\').attr(\'data-post-id\', postId)
  $(\'.edit-post-popup\').attr(\'data-is-single\', isSingleView)
})

$(document).on(\'click\', \'#delete-post\', function () {
  var view = app.views.current

  // set the post id as a data attribute from the edit post popup
  const postId = $(\'.edit-post-popup\').attr(\'data-post-id\')
  const isSingleView = $(\'.edit-post-popup\').attr(\'data-is-single\')

  app.dialog.confirm(\'Are you sure you want to delete this post?\', \'Delete Post\', async () => {
    const response = await deletePost(postId)

    if (response) {
      store.dispatch(\'getMyPosts\', {
        page: 1,
        clear: true
      })
      store.dispatch(\'getMyTags\', {
        page: 1,
        clear: true
      })

      if (isSingleView) {
        view.router.navigate(\'/profile/\')
      }

      showToast(\'Post deleted successfully\')
      // remove the post from the DOM
      $(`.media-post[data-post-id=\"${postId}\"]`).remove()
      app.popup.close(\'.edit-post-popup\')
    } else {
      showToast(\'Failed to delete post\')
    }
  })
})

$(document).on(\'click\', \'#edit-post\', function () {
  var view = app.views.current

  // set the post id as a data attribute from the edit post popup
  const postId = $(\'.edit-post-popup\').attr(\'data-post-id\')
  view.router.navigate(`/post-edit/${postId}`, {
    force: true
  })

  app.popup.close(\'.edit-post-popup\')
})

$(document).on(\'touchstart\', \'.media-post-content .swiper-wrapper\', detectDoubleTapClosure((e) => {
  return // Disable double tap for now

  const parent = e.closest(\'.media-post\')
  const postId = parent.getAttribute(\'data-post-id\')
  const isLiked = parent.getAttribute(\'data-is-liked\') === \'true\'

  if (isLiked) {
    return
  }

  togglePostLike(postId)
}), {
  passive: false
})

// media-post-video click
$(document).on(\'click\', \'.media-post-video\', function () {
  if (this.paused) {
    this.play()
  } else {
    this.pause()
  }
})

// on .popup-open click
$(document).on(\'click\', \'.media-post-comment, .media-post-commentcount\', async function () {
  const postId = this.getAttribute(\'data-post-id\')

  if (!postId) {
    return
  }

  document.getElementById(\'comments-list\').innerHTML = \'<div class=\"preloader\"></div>\'
  document.getElementById(\'comment-form\').reset()

  // update the post id in the comment form
  document.getElementById(\'comment-form\').setAttribute(\'data-post-id\', \'\')
  document.getElementById(\'comment-form\').removeAttribute(\'data-comment-id\')

  document.getElementById(\'comment-form\').querySelector(\'.replying-to\').innerHTML = \'\'
  document.getElementById(\'comment-form\').querySelector(\'.replying-to\').classList.add(\'hidden\')

  try {
    const comments = await fetchComments(postId)
    displayComments(comments, postId)
  } catch (error) {
    app.notification.create({
      titleRightText: \'now\',
      subtitle: \'Oops, something went wrong\',
      text: error.message || \'Failed to fetch comments\',
    }).open()
  }
})

$(document).on(\'click\', \'.media-post-share\', function () {
  // set the post id as a data attribute 
  const postId = $(this).closest(\'.media-post\').attr(\'data-post-id\')
  $(\'.share-popup\').attr(\'data-post-id\', postId)
  $(\'#copy-link\').attr(\'data-clipboard-text\', `${window.location.origin}/post-view/${postId}`)
})

$(document).on(\'click\', \'#share-post-email\', function () {
  const postId = $(this).closest(\'.popup\').attr(\'data-post-id\')
  const postLink = `${window.location.origin}/post-view/${postId}`

  // open the email composer
  window.open(`mailto:?subject=Check out this post&body=${postLink}`)
})

// data-clipboard-text click
$(document).on(\'click\', \'#copy-link\', function () {
  const copyText = $(this).attr(\'data-clipboard-text\')
  navigator.clipboard.writeText(copyText)

  app.toast.create({
    text: \'Link copied to clipboard\',
    closeTimeout: 2000
  }).open()
})

// on .comment-replies-toggle click
$(document).on(\'click\', \'.comment-replies-toggle\', function () {
  const commentRepliesContainer = this.nextElementSibling
  commentRepliesContainer.classList.toggle(\'show\')
  const repliesCount = this.getAttribute(\'data-replies-count\')

  this.innerText = this.innerText === `Show ${repliesCount} replies` ? `Hide ${repliesCount} replies` : `Show ${repliesCount} replies`
})

// on comment form submit
$(\'#comment-form\').on(\'submit\', async function (e) {
  e.preventDefault()

  const postId = this.getAttribute(\'data-post-id\')
  const commentId = this.getAttribute(\'data-comment-id\')
  const comment = this.comment.value

  if (!comment) {
    // app.dialog.alert(\'Please enter a comment\')
    return
  }

  app.preloader.show()

  try {
    const response = await addComment(postId, comment, commentId)

    app.preloader.hide()

    if (response) {
      this.reset()
      this.removeAttribute(\'data-comment-id\')
      this.querySelector(\'.replying-to\').innerHTML = \'\'
      this.querySelector(\'.replying-to\').classList.add(\'hidden\')
      const comments = await fetchComments(postId)
      displayComments(comments, postId)
    } else {
      app.notification.create({
        text: \'Failed to add comment\',
        titleRightText: \'now\',
        subtitle: \'Oops, something went wrong\',
      }).open()
    }
  } catch (error) {
    app.notification.create({
      titleRightText: \'now\',
      subtitle: \'Oops, something went wrong\',
      text: error.message || \'Failed to add comment\',
    }).open()
    app.preloader.hide()
  }
})

//.comment-reply click
$(document).on(\'click\', \'.comment-reply\', function () {
  // get the comment id, and comment owner id
  const commentId = this.closest(\'.comment\').getAttribute(\'data-comment-id\')
  const ownerId = this.closest(\'.comment\').getAttribute(\'data-owner-id\')
  const ownerName = this.closest(\'.comment\').getAttribute(\'data-owner-name\')

  // add something above the comment form to show the user they are replying to a comment
  // add the comment id to the form
  document.getElementById(\'comment-form\').setAttribute(\'data-comment-id\', commentId)
  document.getElementById(\'comment-form\').comment.focus()

  // add the owner name to the form
  //  <span class=\"replying-to\">Replying to <strong>m88xrk</strong></span>
  const replyingTo = document.getElementById(\'comment-form\').querySelector(\'.replying-to\')
  replyingTo.innerHTML = `Replying to <strong>${ownerName}</strong>`
  replyingTo.classList.remove(\'hidden\')
  document.getElementById(\'comment-form\').prepend(replyingTo)
})

$(document).on(\'click\', \'.comment-delete\', async function () {
  app.dialog.confirm(\'Are you sure you want to delete this comment? This will remove all replies to this comment\', \'Delete Comment\', async () => {
    try {
      const commentId = this.getAttribute(\'data-comment-id\')
      const response = await deleteComment(commentId)

      if (response && response.success) {
        // remove the comment from the DOM
        $(`.comment[data-comment-id=\"${commentId}\"]`).remove()
        showToast(\'Comment deleted successfully\')
      }
    } catch (error) {
      app.dialog.alert(\'Failed to delete comment\')
    }
  })
})

$(document).on(\'click\', \'.comment a\', function (e) {
  var view = app.views.current
  // hide the comments popup
  app.popup.close()

  // get the href attribute
  const href = this.getAttribute(\'data-url\')

  if (!href || href === \'#\') {
    return
  }

  // prevent the default action
  e.preventDefault()

  view.router.navigate(href, {
    force: true
  })
})
Add JS Function Name