diff --git a/index.html b/index.html index 2aa1bc7..bb033e4 100644 --- a/index.html +++ b/index.html @@ -1488,14 +1488,14 @@ function createProposalViewModal(proposal) { `; modalBody.innerHTML += ` -
+

Comments

-
-
- - -
-
+ @@ -1577,23 +1577,84 @@ function createProposalViewModal(proposal) { } // Handle comment form in modal - const modalCommentForm = modalBody.querySelector('.comment-form'); - const modalCommentInput = modalBody.querySelector('.comment-input'); - const modalCommentSubmit = modalBody.querySelector('.comment-submit'); + const modalCommentInput = document.getElementById(`modal-comment-input-${proposal.id}`); + const modalCommentSubmit = document.getElementById(`modal-comment-submit-${proposal.id}`); - if (modalCommentForm && modalCommentInput && modalCommentSubmit) { + if (modalCommentInput && modalCommentSubmit) { + // Enable/disable submit button based on input content modalCommentInput.addEventListener('input', () => { - checkCommentInput(modalCommentInput, modalCommentSubmit); + modalCommentSubmit.disabled = modalCommentInput.value.trim().length === 0; }); - modalCommentForm.addEventListener('submit', (e) => { - e.preventDefault(); - submitComment(proposal.id, modalCommentForm); + // Handle form submission + modalCommentSubmit.addEventListener('click', () => { + const commentText = modalCommentInput.value.trim(); + if (commentText) { + submitComment(proposal.id, commentText); + modalCommentInput.value = ''; + modalCommentSubmit.disabled = true; + } }); - - // Load comments for the modal - loadComments(proposal.id); } + + // Load comments for the modal + loadModalComments(proposal.id); +} + +// New function specifically for loading comments in the modal +function loadModalComments(proposalId) { + const commentsContent = document.getElementById(`modal-comments-content-${proposalId}`); + if (!commentsContent) return; + + // Show loading state + commentsContent.innerHTML = `
Loading comments...
`; + + fetch(`${API_BASE_URL}/comments?proposalId=${proposalId}`) + .then(response => { + if (!response.ok) { + throw new Error('Failed to fetch comments'); + } + return response.json(); + }) + .then(comments => { + renderModalComments(commentsContent, comments); + }) + .catch(error => { + console.error('Error fetching comments:', error); + commentsContent.innerHTML = ` +
+ Failed to load comments. Please try again. +
+ `; + }); +} + +// New function to render comments in the modal +function renderModalComments(container, comments) { + if (!comments || comments.length === 0) { + container.innerHTML = ` +
+ No comments yet. Be the first to comment! +
+ `; + return; + } + + let commentsHtml = ''; + + comments.forEach(comment => { + commentsHtml += ` +
+
+
${comment.user_name}
+
${formatDate(comment.timestamp)}
+
+
${formatCommentText(comment.comment_text)}
+
+ `; + }); + + container.innerHTML = commentsHtml; } function getShareableImageUrl(proposal) {