Skip to content

Commit 29b9ef1

Browse files
committed
Fixed voting state not being rendered
1 parent 06279f7 commit 29b9ef1

File tree

1 file changed

+70
-20
lines changed

1 file changed

+70
-20
lines changed

app/api/vote/batch/route.ts

Lines changed: 70 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -77,29 +77,62 @@ export async function POST(request: NextRequest) {
7777
}
7878
})
7979

80-
// Fetch all votes for the user in a single call
80+
// Fetch votes for the specific resources requested
8181
try {
82-
const votes = await databases.listDocuments(
83-
DATABASE_ID,
84-
VOTES_COLLECTION_ID,
85-
[
86-
Query.equal('userId', user.$id),
87-
Query.limit(100) // Limit to prevent too many results
88-
]
89-
)
90-
91-
// Filter votes by the requested resources and set the vote types
92-
votes.documents.forEach((vote: any) => {
93-
const resourceId = vote.resourceId
94-
const resourceType = vote.resourceType
82+
// Separate posts and comments to fetch votes efficiently
83+
const postIds = resources.filter(r => r.type === 'post').map(r => r.id)
84+
const commentIds = resources.filter(r => r.type === 'comment').map(r => r.id)
85+
86+
// Fetch all votes for posts and comments in parallel
87+
const [postVotesResult, commentVotesResult] = await Promise.all([
88+
// Fetch post votes
89+
postIds.length > 0 ? databases.listDocuments(
90+
DATABASE_ID,
91+
VOTES_COLLECTION_ID,
92+
[
93+
Query.equal('userId', user.$id),
94+
Query.equal('resourceType', 'post'),
95+
Query.equal('resourceId', postIds)
96+
]
97+
).catch(error => {
98+
console.error('Error fetching post votes:', error)
99+
return { documents: [] }
100+
}) : Promise.resolve({ documents: [] }),
95101

96-
// Check if this vote matches any of the requested resources
97-
const matchingResource = resources.find(r => r.id === resourceId && r.type === resourceType)
98-
if (matchingResource) {
102+
// Fetch comment votes
103+
commentIds.length > 0 ? databases.listDocuments(
104+
DATABASE_ID,
105+
VOTES_COLLECTION_ID,
106+
[
107+
Query.equal('userId', user.$id),
108+
Query.equal('resourceType', 'comment'),
109+
Query.equal('resourceId', commentIds)
110+
]
111+
).catch(error => {
112+
console.error('Error fetching comment votes:', error)
113+
return { documents: [] }
114+
}) : Promise.resolve({ documents: [] })
115+
])
116+
117+
// Update vote map with post votes
118+
if (postVotesResult.documents.length > 0) {
119+
console.log(`🔍 Found ${postVotesResult.documents.length} post votes`)
120+
postVotesResult.documents.forEach((vote: any) => {
99121
const voteType = vote.count === 1 ? 'up' : 'down'
100-
voteMap[resourceId].currentVote = voteType
101-
}
102-
})
122+
voteMap[vote.resourceId].currentVote = voteType
123+
console.log(`✅ Set post vote for ${vote.resourceId}: ${voteType}`)
124+
})
125+
}
126+
127+
// Update vote map with comment votes
128+
if (commentVotesResult.documents.length > 0) {
129+
console.log(`🔍 Found ${commentVotesResult.documents.length} comment votes`)
130+
commentVotesResult.documents.forEach((vote: any) => {
131+
const voteType = vote.count === 1 ? 'up' : 'down'
132+
voteMap[vote.resourceId].currentVote = voteType
133+
console.log(`✅ Set comment vote for ${vote.resourceId}: ${voteType}`)
134+
})
135+
}
103136
} catch (error) {
104137
console.error('Error fetching votes:', error)
105138
// Return empty vote map on error
@@ -120,6 +153,8 @@ export async function POST(request: NextRequest) {
120153
[Query.equal('$id', postIds)]
121154
)
122155

156+
console.log('📊 Fetched posts:', posts.documents.length)
157+
123158
// Update vote map with post data
124159
posts.documents.forEach((post: any) => {
125160
voteMap[post.$id].countUp = post.countUp || 0
@@ -140,6 +175,8 @@ export async function POST(request: NextRequest) {
140175
[Query.equal('$id', commentIds)]
141176
)
142177

178+
console.log('📊 Fetched comments:', comments.documents.length)
179+
143180
// Update vote map with comment data
144181
comments.documents.forEach((comment: any) => {
145182
voteMap[comment.$id].countUp = comment.countUp || 0
@@ -155,6 +192,19 @@ export async function POST(request: NextRequest) {
155192
// Continue with default values if the query fails
156193
}
157194

195+
console.log('📊 Final vote map being returned:', voteMap)
196+
197+
// Log each resource's final state for debugging
198+
resources.forEach(resource => {
199+
const state = voteMap[resource.id]
200+
console.log(`📋 Final state for ${resource.id} (${resource.type}):`, {
201+
currentVote: state.currentVote,
202+
count: state.count,
203+
countUp: state.countUp,
204+
countDown: state.countDown
205+
})
206+
})
207+
158208
return NextResponse.json({ voteMap })
159209

160210
} catch (error) {

0 commit comments

Comments
 (0)