diff --git a/index.html b/index.html index c22c173..54e7484 100644 --- a/index.html +++ b/index.html @@ -2223,70 +2223,70 @@ function formatCommentText(text) { } // Function to submit a new comment -async function submitComment(proposalId, form) { - const commentInput = form.querySelector('.comment-input'); - const submitButton = form.querySelector('.comment-submit'); - - const commentText = commentInput.value.trim(); - - if (!commentText) return; - - // Disable form while submitting - commentInput.disabled = true; - submitButton.disabled = true; - - try { - const response = await safeFetch(`${API_BASE_URL}/comments`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ - proposalId: proposalId, - userId: currentUser.id, - commentText: commentText - }) - }); +async function submitComment(proposalId, commentText) { + // If commentText is an element, extract the value (backward compatibility) + if (typeof commentText !== 'string') { + const form = commentText; + const commentInput = form.querySelector('.comment-input'); + commentText = commentInput.value.trim(); - if (!response.ok) { - throw new Error('Failed to submit comment'); + // Disable form while submitting + commentInput.disabled = true; + const submitButton = form.querySelector('.comment-submit'); + submitButton.disabled = true; + + // Enable form when done + const enableForm = () => { + commentInput.disabled = false; + submitButton.disabled = false; + }; + + // Return early if empty + if (!commentText) { + enableForm(); + return; } - // Clear input + // Clear input optimistically commentInput.value = ''; - - // Reload comments to show the new one - const commentsContainer = document.querySelector(`.comments-container[data-proposal-id="${proposalId}"]`); - const commentsContent = commentsContainer.querySelector('.comments-content'); - - // Add new comment to the top (optimistic update) - const newComment = await response.json(); - const tempCommentHtml = ` -
-
-
${newComment.userName}
-
just now
-
-
${formatCommentText(newComment.commentText)}
-
- `; - - // If there are no comments yet, replace the "no comments" message - if (commentsContent.querySelector('.no-comments')) { - commentsContent.innerHTML = tempCommentHtml; - } else { - // Otherwise, prepend to existing comments - commentsContent.insertAdjacentHTML('afterbegin', tempCommentHtml); - } - - } catch (error) { - console.error('Error posting comment:', error); - showToastMessage('Failed to post comment. Please try again.', '#ff0099'); - } finally { - // Re-enable form - commentInput.disabled = false; - submitButton.disabled = false; } + + // Ensure we have text to submit + if (!commentText || !commentText.trim()) { + console.error('Empty comment text'); + return; + } + + console.log(`Submitting comment for proposal ${proposalId}: ${commentText.substring(0, 20)}...`); + + return safeFetch(`${API_BASE_URL}/comments`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + proposalId: proposalId, + userId: currentUser.id, + commentText: commentText + }) + }) + .then(response => { + if (!response.ok) { + throw new Error('Failed to post comment'); + } + return response.json(); + }) + .then(data => { + loadComments(proposalId); + updateCommentCount(proposalId); + showToastMessage('Comment posted!', '#11cc77'); + return data; + }) + .catch(error => { + console.error('Error posting comment:', error); + showToastMessage('Failed to post comment', '#ff0099'); + throw error; + }); } // Function to check if comment input is not empty diff --git a/worker.js b/worker.js index d312f90..96ca891 100644 --- a/worker.js +++ b/worker.js @@ -6,6 +6,10 @@ const corsHeaders = { 'Access-Control-Max-Age': '86400' }; +// Define pagination constants +const DEFAULT_PAGE_SIZE = 20; +const MAX_PAGE_SIZE = 100; + // Handle OPTIONS request for CORS preflight function handleOptions() { return new Response(null, {