x
This commit is contained in:
parent
ae357d2ab8
commit
1b3c0db99f
2 changed files with 63 additions and 59 deletions
118
index.html
118
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 = `
|
||||
<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
|
||||
|
|
|
|||
|
|
@ -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, {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue