File size: 2,970 Bytes
7e5cb25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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 (
    <>
      <p>Submit a new prompt to be included to the benchmark.</p>
      <p>
        Each week, the highest rated prompt will become part of the benchmark.
      </p>
      <p>What makes a good prompt:</p>
      <ul>
        <li>
          Can be broke down <Link href="/about">into rubrics</Link> & evaluated
        </li>
        <li>
          Is original and not popular on the internet (unlikely to be already
          part in the benchmark)
        </li>
        <li>Is not too long (max 1000 characters)</li>
      </ul>
      <br />

      <form action={create}>
        <table
          id="hnmain"
          border="0"
          cellPadding="0"
          cellSpacing="0"
          style={{ maxWidth: 680 }}
        >
          <tbody>
            <tr>
              <td>
                <label htmlFor="prompt">Prompt</label>
              </td>
              <td>
                <textarea
                  id="prompt"
                  name="prompt"
                  rows="10"
                  minLength={20}
                  maxLength={1000}
                  cols="50"
                  required
                />
              </td>
            </tr>
            <tr>
              <td>
                <label htmlFor="rubrics">Slug</label>
              </td>
              <td>
                <input
                  type="text"
                  id="slug"
                  name="slug"
                  required
                  pattern="^[a-z0-9]+(?:-[a-z0-9]+)*$"
                />
              </td>
            </tr>
          </tbody>
        </table>
        <br />
        <p>
          To prevent spam, you will receive an email to confirm your submission.
        </p>

        <button type="submit">Submit</button>
      </form>
    </>
  )
}