Created
June 16, 2024 14:35
-
-
Save PaulieScanlon/561469b25830dae797b27ee776a63f59 to your computer and use it in GitHub Desktop.
Vote Server Action
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// submit-vote.ts | |
'use server'; | |
import { redirect } from 'next/navigation'; | |
import { doc } from '../services/google-spreadsheet'; | |
export default async function submitVote(id: string) { | |
if (!id) { | |
redirect('/?error=true'); | |
} | |
try { | |
await doc.loadInfo(); | |
const sheet = doc.sheetsByIndex[0]; | |
const rows = await sheet.getRows(); | |
rows[0].set(id, Number(rows[0].get(id)) + 1); | |
await rows[0].save(); | |
} catch (error) { | |
console.error(error); | |
redirect('/?error=true'); | |
} | |
redirect('/results'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment