This commit is contained in:
Omar Najjar 2025-04-13 04:34:23 +10:00
parent ae357d2ab8
commit 1b3c0db99f
2 changed files with 63 additions and 59 deletions

View file

@ -2223,70 +2223,70 @@ function formatCommentText(text) {
} }
// Function to submit a new comment // Function to submit a new comment
async function submitComment(proposalId, form) { async function submitComment(proposalId, commentText) {
const commentInput = form.querySelector('.comment-input'); // If commentText is an element, extract the value (backward compatibility)
const submitButton = form.querySelector('.comment-submit'); if (typeof commentText !== 'string') {
const form = commentText;
const commentText = commentInput.value.trim(); const commentInput = form.querySelector('.comment-input');
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
})
});
if (!response.ok) { // Disable form while submitting
throw new Error('Failed to submit comment'); 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 = ''; 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 = `
<div class="comment-item" style="animation: fadeIn 0.5s;">
<div class="comment-header">
<div class="comment-author">${newComment.userName}</div>
<div class="comment-time">just now</div>
</div>
<div class="comment-text">${formatCommentText(newComment.commentText)}</div>
</div>
`;
// 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 // Function to check if comment input is not empty

View file

@ -6,6 +6,10 @@ const corsHeaders = {
'Access-Control-Max-Age': '86400' 'Access-Control-Max-Age': '86400'
}; };
// Define pagination constants
const DEFAULT_PAGE_SIZE = 20;
const MAX_PAGE_SIZE = 100;
// Handle OPTIONS request for CORS preflight // Handle OPTIONS request for CORS preflight
function handleOptions() { function handleOptions() {
return new Response(null, { return new Response(null, {