From c88f5f763bf0621fa8a494b8ad69c4c69e2c56e2 Mon Sep 17 00:00:00 2001 From: Clay Nicholson Date: Tue, 20 Aug 2024 19:08:39 -0400 Subject: [PATCH] api update --- pages/api/arcade/showcase/vote.js | 84 +++++++++++++++++++++---------- pages/arcade/showcase/test.js | 55 +++++++++++--------- 2 files changed, 88 insertions(+), 51 deletions(-) diff --git a/pages/api/arcade/showcase/vote.js b/pages/api/arcade/showcase/vote.js index ff77f759..c7e2747b 100644 --- a/pages/api/arcade/showcase/vote.js +++ b/pages/api/arcade/showcase/vote.js @@ -24,40 +24,72 @@ export default async function handler(req, res) { } try { - const { authorization, points, 'project-id': projectId } = req.headers; + + const { authorization} = req.headers; + const { rank1, rank2, rank3 } = req.body; + return res.status(500).json({ error: req.body }); - if (!authorization || !points || !projectId) { - return res.status(400).json({ error: 'Missing required headers' }); + const pointsDistribution = [5, 4, 3, 2, 1]; + + for (let i = 0; i < rank1.length; i++) { + const project = rank1[i]; + const points = pointsDistribution[i]; + + addVote(project, points, authorization); } - const users = await usersTable.read({ - filterByFormula: `{auth_token} = '${authorization}'` - }); - - if (users.length === 0) { - return res.status(404).json({ error: 'User not found' }); + for (let i = 0; i < rank2.length; i++) { + const project = rank2[i]; + const points = pointsDistribution[i]; + + addVote(project, points, authorization); } - const user = users[0]; + for (let i = 0; i < rank3.length; i++) { + const project = rank3[i]; + const points = pointsDistribution[i]; + + addVote(project, points, authorization); + } - const vote = await votesTable.create({ - points: parseInt(points, 10), - voter: [user.id] - }); - - const project = await showcaseTable.find(projectId); - - const updatedVotes = project.fields.Votes - ? [...project.fields.Votes, vote.id] - : [vote.id]; - - await showcaseTable.update(projectId, { - Votes: updatedVotes - }); - - res.status(200).json({ success: true, vote }); + { success: true, vote } + + res.status(200).json(); } catch (error) { console.error('Error creating vote:', error); res.status(500).json({ error: 'Internal Server Error' }); } } + +const addVote = async (projectId, points, authorization) => { + if (!authorization || !points || !projectId) { + return res.status(400).json({ error: 'Missing required headers' }); + } + + const users = await usersTable.read({ + filterByFormula: `{Auth Token} = '${authorization}'` + }); + + if (users.length === 0) { + return res.status(404).json({ error: 'User not found' }); + } + + const user = users[0]; + console.log(user); + + const vote = await votesTable.create({ + Points: parseInt(points, 10), + Voter: [user.id] + }); + + const project = await showcaseTable.find(projectId); + + const updatedVotes = project.fields.Votes + ? [...project.fields.Votes, vote.id] + : [vote.id]; + + await showcaseTable.update(projectId, { + Votes: updatedVotes + }); + +} \ No newline at end of file diff --git a/pages/arcade/showcase/test.js b/pages/arcade/showcase/test.js index 32c767e7..26e1097d 100644 --- a/pages/arcade/showcase/test.js +++ b/pages/arcade/showcase/test.js @@ -1,10 +1,10 @@ -import React from 'react' -import { useEffect } from 'react'; +import React, { useEffect } from 'react'; -const test = () => { +const Test = () => { + - async function submitVote(points, projectId) { - const authToken = window.localStorage.getItem('arcade.authToken') + const submitVote = async (rank1, rank2, rank3) => { + const authToken = window.localStorage.getItem('arcade.authToken'); try { const response = await fetch('/api/arcade/showcase/vote', { @@ -12,30 +12,35 @@ const test = () => { headers: { 'Content-Type': 'application/json', 'Authorization': authToken, - 'Points': points.toString(), - 'Project-ID': projectId - } - }); - - if (!response.ok) { - throw new Error(`HTTP error! Status: ${response.status}`); - } - + }, + body: JSON.stringify({ + rank1, + rank2, + rank3, + }), + }) + + const data = await response.json(); return data; } catch (error) { console.error('Error submitting vote:', error); throw error; } - } - - useEffect(() => { - submitVote(5, "recnSpjM91mCKJjlo"); - }, []); - -return ( -
test
- ) -} + }; -export default test \ No newline at end of file + useEffect(() => { + + const rank1 = ["recnSpjM91mCKJjlo", "recnSpjM91mCKJjlo", "recnSpjM91mCKJjlo", "recnSpjM91mCKJjlo", "recnSpjM91mCKJjlo"]; + const rank2 = ["recnSpjM91mCKJjlo", "recnSpjM91mCKJjlo", "recnSpjM91mCKJjlo", "recnSpjM91mCKJjlo", "recnSpjM91mCKJjlo"]; + const rank3 = ["recnSpjM91mCKJjlo", "recnSpjM91mCKJjlo", "recnSpjM91mCKJjlo", "recnSpjM91mCKJjlo", "recnSpjM91mCKJjlo"]; + + submitVote(rank1, rank2, rank3); + }, []); + + return ( +
test
+ ); +}; + +export default Test;