import db from "@/utils/db" import { cookies } from "next/headers" import Link from "next/link" import { redirect } from "next/navigation" import jwt from "jsonwebtoken" export default async function Submit() { // Get user session token async function create(formData) { "use server" const cookiesList = cookies() const token = cookiesList.get("token") console.log("token", token) if (!token) throw new Error("not logged") const { userId } = jwt.verify(token.value, process.env.JWT_SECRET) const [userObj] = await db`SELECT * FROM users WHERE id = ${userId}` if (!userObj) throw new Error("user not found") const text = formData.get("prompt") const slug = formData.get("slug") if (text.length <= 20 || text.length > 1000) throw new Error("prompt too long or too short") const [prompt] = await db`INSERT INTO prompts (text, submitter, slug) VALUES (${text}, ${userObj.id}, ${slug}) RETURNING *` const [vote] = await db`INSERT INTO votes ("user", prompt) VALUES (${userObj.id}, ${prompt.id}) RETURNING *` redirect(`/prompts`) // send email to user to confirm submission } return ( <>
Submit a new prompt to be included to the benchmark.
Each week, the highest rated prompt will become part of the benchmark.
What makes a good prompt: