diff --git a/index.html b/index.html index 5150aad..a3a2f74 100644 --- a/index.html +++ b/index.html @@ -759,6 +759,187 @@ async function safeFetch(url, options = {}) { } } + // Function to render all proposals + function renderProposals(response) { + const proposalsContainer = document.getElementById('proposals-container'); + proposalsContainer.innerHTML = ''; + + // Clear loading indicator + hideLoadingIndicator(); + + // Handle empty or missing data + if (!response || !response.data || !Array.isArray(response.data) || response.data.length === 0) { + proposalsContainer.innerHTML = ` +
+

No petitions found

+

Be the first to launch a rebellion!

+
+ `; + return; + } + + // Access the data array from the response + const proposals = response.data; + + // Create and append proposal cards + proposals.forEach(proposal => { + const proposalCard = createProposalCard(proposal); + proposalsContainer.appendChild(proposalCard); + }); + + // Activate share buttons + initializeShareButtons(); + + // Start CTA cycling for comment buttons + startCTACycling(); + } + + // Function to create a proposal card from proposal data + function createProposalCard(proposal) { + const netVotes = proposal.upvotes - proposal.downvotes; + const voteClass = netVotes > 0 ? 'positive' : netVotes < 0 ? 'negative' : ''; + + const proposalButton = document.createElement('a'); + proposalButton.href = getShareableLink(proposal.id); + proposalButton.addEventListener('click', e => { + e.stopPropagation(); + }); + + const proposalCard = document.createElement('div'); + proposalCard.className = 'proposal-card'; + proposalButton.appendChild(proposalCard); + + // Badge for trending or power proposals + const badgeHtml = proposal.trending ? + `` : + (netVotes > 5 ? `
💯 Popular
` : ''); + + // Add petition signatures info if proposal has upvotes/signatures + const petitionInfo = proposal.upvotes > 0 ? + `
+ ✓ + ${proposal.petition_signatures || proposal.upvotes} + verified signatures +
` : ''; + + // Add meme if available + const memeHtml = proposal.meme_url ? + `
+ Meme for petition +
` : ''; + + proposalCard.innerHTML = ` + ${badgeHtml} +
${proposal.author_name}
+
${proposal.text}
+ ${memeHtml} + ${petitionInfo} +
+
+ + + ${netVotes > 0 ? '+' : ''}${netVotes} +
+ Share +
+
+
${formatDate(proposal.timestamp)}
+
+
+ + +
+ `; + + // Add event listeners for vote buttons + const upvoteButton = proposalCard.querySelector('[data-vote-type="upvote"]'); + const downvoteButton = proposalCard.querySelector('[data-vote-type="downvote"]'); + + if (upvoteButton) { + upvoteButton.addEventListener('click', () => { + handleVote(proposal.id, 'upvote'); + }); + } + + if (downvoteButton) { + downvoteButton.addEventListener('click', () => { + handleVote(proposal.id, 'downvote'); + }); + } + + // Add comments toggle functionality + const commentsToggle = proposalCard.querySelector(`#comments-toggle-${proposal.id}`); + const commentsContainer = proposalCard.querySelector(`#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 = proposalCard.querySelector(`.comment-submit[data-proposal-id="${proposal.id}"]`); + const commentInput = proposalCard.querySelector(`#comment-input-${proposal.id}`); + + if (commentSubmit && commentInput) { + commentSubmit.addEventListener('click', function() { + const commentText = commentInput.value.trim(); + + if (commentText) { + // Store original text and disable button while submitting + const originalText = this.textContent; + this.textContent = "Sending..."; + this.disabled = true; + + submitComment(proposal.id, commentText) + .then(() => { + commentInput.value = ''; + // Set a new random CTA after submission + this.textContent = getRandomCTA(); + this.disabled = false; + }) + .catch(() => { + // Restore original CTA if there was an error + this.textContent = originalText; + this.disabled = false; + }); + } + }); + + // Enable/disable comment button based on input + commentInput.addEventListener('input', function() { + commentSubmit.disabled = this.value.trim() === ''; + }); + } + + return proposalButton; + } + // Handle voting async function handleVote(proposalId, voteType) { currentProposalId = proposalId; @@ -1004,164 +1185,10 @@ async function submitVote(proposalId, voteType, isPetition = false, petitionDeta } // Render proposals - FIXED VERSION - function renderProposals(proposals) { - const proposalsContainer = document.getElementById('proposals-container'); - if (!proposalsContainer) return; - - // Clear container - proposalsContainer.innerHTML = ''; - - // Show empty state if no proposals - if (!proposals || proposals.length === 0) { - proposalsContainer.innerHTML = ` -
-

No petitions yet. Be the first to create one!

-
- `; - return; - } - - // Render each proposal - proposals.forEach(proposal => { - const netVotes = proposal.upvotes - proposal.downvotes; - const voteClass = netVotes > 0 ? 'positive' : netVotes < 0 ? 'negative' : ''; - - const proposalButton = document.createElement('a'); - proposalButton.href = getShareableLink(proposal.id); - - proposalButton.addEventListener('click', e => { - e.stopPropagation(); - }); - - const proposalCard = document.createElement('div'); - proposalCard.className = 'proposal-card'; - proposalButton.appendChild(proposalCard); - - // Badge for trending or power proposals - const badgeHtml = proposal.trending ? - `` : - (netVotes > 5 ? `
💯 Popular
` : ''); - - // Add petition signatures info if proposal has upvotes/signatures - const petitionInfo = proposal.upvotes > 0 ? - `
- ✓ - ${proposal.petition_signatures || proposal.upvotes} - verified signatures -
` : ''; - - // Add meme if available - const memeHtml = proposal.meme_url ? - `
- Meme for petition -
` : ''; - - proposalCard.innerHTML = ` - ${badgeHtml} -
${proposal.author_name}
-
${proposal.text}
- ${memeHtml} - ${petitionInfo} -
-
- - -${netVotes > 0 ? '+' : ''}${netVotes} -
- Share -
-
-
${formatDate(proposal.timestamp)}
-
-
- - -
-`; - - proposalsContainer.appendChild(proposalButton); - - - - // Add comments section to the proposal card - NEW CODE - addCommentsToProposal(proposalCard, proposal.id); - - // Add event listeners for vote buttons - const upvoteButton = proposalCard.querySelector('[data-vote-type="upvote"]'); - const downvoteButton = proposalCard.querySelector('[data-vote-type="downvote"]'); - const shareButton = proposalCard.querySelector('.share-button'); - - if (upvoteButton) { - upvoteButton.addEventListener('click', () => { - handleVote(proposal.id, 'upvote'); - }); - } - - if (downvoteButton) { - downvoteButton.addEventListener('click', () => { - handleVote(proposal.id, 'downvote'); - }); - } - - if (shareButton) { - shareButton.addEventListener('click', async () => { - if ('share' in navigator) await navigator.share({ title: 'What do you think?', text: proposal.text, url: getShareableLink(proposal.id) }); - else 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); - -}); -} + // function renderProposals(proposals) { + // Removing duplicate implementation + // This function has been moved and implemented at line 762 +// } // Create meme upload elements function createMemeUpload() { @@ -1992,7 +2019,7 @@ if (twitterImageElement) { loadModalComments(proposal.id); } -// New function specifically for loading comments in the modal +// Update the loadModalComments function to use the queue async function loadModalComments(proposalId) { const commentsContent = document.getElementById(`modal-comments-content-${proposalId}`); if (!commentsContent) return; @@ -2000,24 +2027,8 @@ async function loadModalComments(proposalId) { // Show loading state commentsContent.innerHTML = `
Loading comments...
`; - try { - // Include the current user ID to get their votes - const response = await safeFetch(`${API_BASE_URL}/comments?proposalId=${proposalId}&userId=${currentUser.id}`); - - if (!response.ok) { - throw new Error('Failed to fetch comments'); - } - - const comments = await response.json(); - renderModalComments(commentsContent, comments); - } catch (error) { - console.error('Error fetching comments:', error); - commentsContent.innerHTML = ` -
- Failed to load comments. Please try again. -
- `; - } + // Queue the comment load + queueCommentLoad(proposalId); } function renderModalComments(container, comments) { @@ -2172,29 +2183,15 @@ function toggleComments(proposalId, button) { // Function to load comments for a proposal async function loadComments(proposalId) { const commentsContainer = document.querySelector(`.comments-container[data-proposal-id="${proposalId}"]`); - const commentsContent = commentsContainer.querySelector('.comments-content'); + const commentsContent = commentsContainer?.querySelector('.comments-content'); + + if (!commentsContent) return; // Show loading state commentsContent.innerHTML = `
Loading comments...
`; - try { - // Include the current user ID to get their votes - const response = await safeFetch(`${API_BASE_URL}/comments?proposalId=${proposalId}&userId=${currentUser.id}`); - - if (!response.ok) { - throw new Error('Failed to fetch comments'); - } - - const comments = await response.json(); - renderComments(commentsContent, comments); - } catch (error) { - console.error('Error fetching comments:', error); - commentsContent.innerHTML = ` -
- Failed to load comments. Please try again. -
- `; - } + // Queue the comment load + queueCommentLoad(proposalId); } // Function to render comments @@ -2674,37 +2671,18 @@ function updateCommentCount(proposalId) { }); } +// Using the queue-based loadComments function to load comments function loadComments(proposalId) { - const commentsList = document.getElementById(`comments-list-${proposalId}`); - if (!commentsList) return; + const commentsContainer = document.querySelector(`.comments-container[data-proposal-id="${proposalId}"]`); + const commentsContent = commentsContainer?.querySelector('.comments-content'); - commentsList.innerHTML = '
Loading comments...
'; + if (!commentsContent) return; - safeFetch(`${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.
'; - }); + // Show loading state + commentsContent.innerHTML = `
Loading comments...
`; + + // Queue the comment load + queueCommentLoad(proposalId); } // Update submitComment function to handle promises @@ -2797,32 +2775,6 @@ function queueCommentLoad(proposalId) { } } -// Update the loadComments function to use the queue -function loadComments(proposalId) { - const commentsContainer = document.querySelector(`.comments-container[data-proposal-id="${proposalId}"]`); - const commentsContent = commentsContainer?.querySelector('.comments-content'); - - if (!commentsContent) return; - - // Show loading state - commentsContent.innerHTML = `
Loading comments...
`; - - // Queue the comment load - queueCommentLoad(proposalId); -} - -// Update the loadModalComments function to use the queue -async function loadModalComments(proposalId) { - const commentsContent = document.getElementById(`modal-comments-content-${proposalId}`); - if (!commentsContent) return; - - // Show loading state - commentsContent.innerHTML = `
Loading comments...
`; - - // Queue the comment load - queueCommentLoad(proposalId); -} -
hit me