diff --git a/index.html b/index.html index 95a362b..278957d 100644 --- a/index.html +++ b/index.html @@ -1824,6 +1824,18 @@ async function submitVote(proposalId, voteType, isPetition = false, petitionDeta
${formatDate(proposal.timestamp)}
+
+ + +
`; proposalsContainer.appendChild(proposalCard); @@ -1856,9 +1868,47 @@ async function submitVote(proposalId, voteType, isPetition = false, petitionDeta copyToClipboard(getShareableLink(proposal.id)); }); } + + +// Add comments toggle functionality +const commentsToggle = document.getElementById(`comments-toggle-${proposal.id}`); +const commentsContainer = document.getElementById(`comments-container-${proposal.id}`); + +if (commentsToggle && commentsContainer) { + commentsToggle.addEventListener('click', function() { + const isVisible = commentsContainer.style.display !== 'none'; + + if (isVisible) { + commentsContainer.style.display = 'none'; + this.querySelector('.comments-toggle-icon').textContent = '▶'; + } else { + commentsContainer.style.display = 'block'; + this.querySelector('.comments-toggle-icon').textContent = '▼'; + loadComments(proposal.id); + } }); } +// Add event listener for comment submission +const commentSubmit = document.querySelector(`.comment-submit[data-proposal-id="${proposal.id}"]`); +if (commentSubmit) { + commentSubmit.addEventListener('click', function() { + const commentInput = document.getElementById(`comment-input-${proposal.id}`); + const commentText = commentInput.value.trim(); + + if (commentText) { + submitComment(proposal.id, commentText); + commentInput.value = ''; + } + }); +} + +// Update comment count +updateCommentCount(proposal.id); + +}); +} + // Create meme upload elements function createMemeUpload() { const uploadContainer = document.createElement('div'); @@ -2793,7 +2843,6 @@ function addCommentsToProposal(proposalCard, proposalId) { const commentsToggle = document.createElement('button'); commentsToggle.className = 'comments-toggle'; commentsToggle.setAttribute('aria-expanded', 'false'); - commentsToggle.innerHTML = ' x comments'; const commentsContainer = document.createElement('div'); commentsContainer.className = 'comments-container'; @@ -2914,6 +2963,80 @@ function addCommentsToProposal(proposalCard, proposalId) { } }); + + // Add these functions outside your renderProposals function + +function updateCommentCount(proposalId) { + const commentsToggle = document.getElementById(`comments-toggle-${proposalId}`); + if (!commentsToggle) return; + + fetch(`${API_BASE_URL}/comments?proposalId=${proposalId}`) + .then(response => response.json()) + .then(comments => { + const commentCount = comments.length; + const commentText = commentCount === 1 ? 'comment' : 'comments'; + commentsToggle.innerHTML = ` ${commentCount} ${commentText}`; + }) + .catch(error => { + commentsToggle.innerHTML = ` 0 comments`; + }); +} + +function loadComments(proposalId) { + const commentsList = document.getElementById(`comments-list-${proposalId}`); + if (!commentsList) return; + + commentsList.innerHTML = '
Loading comments...
'; + + fetch(`${API_BASE_URL}/comments?proposalId=${proposalId}`) + .then(response => response.json()) + .then(comments => { + if (comments.length === 0) { + commentsList.innerHTML = '
No comments yet. Be the first to comment!
'; + return; + } + + commentsList.innerHTML = ''; + comments.forEach(comment => { + const commentElement = document.createElement('div'); + commentElement.className = 'comment'; + commentElement.innerHTML = ` +
+ ${comment.user_name} + ${formatDate(comment.timestamp)} +
+
${comment.comment_text}
+ `; + commentsList.appendChild(commentElement); + }); + }) + .catch(error => { + commentsList.innerHTML = '
Failed to load comments.
'; + }); +} + +function submitComment(proposalId, commentText) { + fetch(`${API_BASE_URL}/comments`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + proposalId: proposalId, + userId: currentUser.id, + commentText: commentText + }) + }) + .then(response => response.json()) + .then(data => { + loadComments(proposalId); + updateCommentCount(proposalId); + showToastMessage('Comment posted!', '#11cc77'); + }) + .catch(error => { + showToastMessage('Failed to post comment', '#ff0099'); + }); +}
hit me