File size: 1,345 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
import UpvoteBtn from "@/components/UpvoteBtn"
import db from "@/utils/db"
import { cookies } from "next/headers"
import Link from "next/link"

export default async function Dataset() {
  const cookiesList = cookies()

  const logged = cookiesList.has("token")

  // get prompts with selected != true joined with sum of votes for each
  const promptsWithVotes =
    await db`SELECT prompts.*, COUNT(votes.id) AS votes FROM prompts LEFT JOIN votes ON prompts.id = votes.prompt WHERE prompts.selected IS NOT TRUE GROUP BY prompts.id ORDER BY votes DESC`

  return (
    <>
      <table
        border="0"
        cellPadding="0"
        cellSpacing="0"
        style={{ maxWidth: 600 }}
      >
        <tbody>
          {promptsWithVotes.map((prompt, i) => (
            <tr key={i}>
              <td width={30}>{i + 1}</td>
              <td>
                <span>{prompt.votes} points</span>
                <br />
                {logged ? (
                  <UpvoteBtn id={prompt.id} />
                ) : (
                  <Link href="/login">
                    <small>upvote</small>
                  </Link>
                )}
              </td>
              <td>
                <pre style={{ maxWidth: 800 }}>{prompt.text}</pre>
              </td>
            </tr>
          ))}
        </tbody>
      </table>
    </>
  )
}