|
1 | 1 | import { GetServerSideProps } from "next" |
2 | | -import { DiscordUser } from "../utils/types" |
| 2 | +import { DiscordUser, Reminder } from "../utils/types" |
3 | 3 | import { parseUser } from "../utils/parse-user" |
4 | 4 | import Main from "../components/Main" |
5 | 5 | import Head from "next/head" |
| 6 | +import { config } from "../utils/config" |
| 7 | +import { Component, useState } from "react" |
| 8 | +import Image from "next/image" |
6 | 9 |
|
7 | 10 | interface Props { |
8 | | - user: DiscordUser; |
| 11 | + user: DiscordUser |
| 12 | + reminders: Reminder[] |
9 | 13 | } |
10 | 14 |
|
11 | | -export default function Reminders(props: Props) { |
12 | | - return ( |
13 | | - <Main> |
14 | | - <Head> |
15 | | - <title>Reminders | Hu Tao</title> |
16 | | - <meta name="twitter:card" content="summary" /> |
17 | | - <meta property="og:title" content="Reminders | Hu Tao" /> |
18 | | - <meta property="og:description" content="Change, add and remove your Discord reminders" /> |
19 | | - </Head> |
20 | | - <h1 className="text-5xl font-bold"> |
21 | | - Reminders |
22 | | - </h1> |
23 | | - <div className="text-xs">ID: {props.user.id}</div> |
24 | | - <div>Not yet implemented</div> |
25 | | - </Main> |
26 | | - ) |
| 15 | +export default class Reminders extends Component<Props, { reminders: Reminder[] }> { |
| 16 | + constructor(props: Props) { |
| 17 | + super(props) |
| 18 | + this.state = { |
| 19 | + reminders: this.props.reminders |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + render() { |
| 24 | + const { user } = this.props |
| 25 | + const { reminders } = this.state |
| 26 | + |
| 27 | + return ( |
| 28 | + <Main> |
| 29 | + <Head> |
| 30 | + <title>Reminders | Hu Tao</title> |
| 31 | + <meta name="twitter:card" content="summary" /> |
| 32 | + <meta property="og:title" content="Reminders | Hu Tao" /> |
| 33 | + <meta property="og:description" content="Change, add and remove your Discord reminders" /> |
| 34 | + </Head> |
| 35 | + <h1 className="text-5xl font-bold"> |
| 36 | + Reminders |
| 37 | + </h1> |
| 38 | + <div className="text-base">Logged in as: <DiscordAvatar user={user} /> {user.username}<span className="text-xs">#{user.discriminator}</span></div> |
| 39 | + <div className="text-xl mt-3">{reminders.length} reminder{reminders.length == 1 ? "" : "s"}</div> |
| 40 | + {reminders.map(r => <ReminderCard r={r} key={r.id} onDelete={() => this.setState({ |
| 41 | + reminders: reminders.filter(re => r.id != re.id) |
| 42 | + })} />)} |
| 43 | + </Main> |
| 44 | + ) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +function ReminderCard({ r, onDelete }: { r: Reminder, onDelete: () => void }) { |
| 49 | + const deleteReminder = async () => { |
| 50 | + const res = await fetch("/api/reminders/delete", { |
| 51 | + body: JSON.stringify({ r }), |
| 52 | + headers: { "Content-Type": "application/json" }, |
| 53 | + method: "POST" |
| 54 | + }) |
| 55 | + |
| 56 | + if ((res.status >= 200 && res.status < 300) || res.status == 404) |
| 57 | + onDelete() |
| 58 | + } |
| 59 | + return <div key={r.id} className="border rounded-md p-2 mt-2 text-slate-700 dark:text-slate-300"> |
| 60 | + <div className="text-xl text-slate-900 dark:text-slate-100">#{r.id}: <span>{r.subject}</span></div> |
| 61 | + Will trigger on <span className="text-slate-900 dark:text-slate-100">{new Date(r.timestamp).toLocaleString(undefined, { day: "numeric", year: "numeric", month: "short", hour: "2-digit", minute: "2-digit" })}</span> |
| 62 | + <div className="bg-red-500 text-slate-50 w-16 text-center rounded-lg mt-2 cursor-pointer" onClick={deleteReminder}>Delete</div> |
| 63 | + </div> |
| 64 | +} |
| 65 | + |
| 66 | +function DiscordAvatar({ user }: { user: DiscordUser }) { |
| 67 | + return <Image |
| 68 | + src={user.avatar ? `https://cdn.discordapp.com/avatars/${user.id}/${user.avatar}.webp?size=16` : "https://discord.com/assets/1f0bfc0865d324c2587920a7d80c609b.png"} |
| 69 | + alt="Discord avatar" |
| 70 | + width={16} |
| 71 | + height={16} |
| 72 | + className="rounded-xl p-0 m-0" |
| 73 | + /> |
27 | 74 | } |
28 | 75 |
|
29 | 76 | export const getServerSideProps: GetServerSideProps<Props> = async function (ctx) { |
30 | | - const user = parseUser(ctx) |
31 | | - |
32 | | - if (!user) { |
33 | | - return { |
34 | | - redirect: { |
35 | | - destination: "/api/oauth", |
36 | | - permanent: false, |
37 | | - }, |
38 | | - } |
39 | | - } |
40 | | - console.log(user) |
| 77 | + const user = parseUser(ctx.req.headers.cookie) |
41 | 78 |
|
42 | | - return { props: { user } } |
| 79 | + if (!user) { |
| 80 | + return { |
| 81 | + redirect: { |
| 82 | + destination: "/api/oauth", |
| 83 | + permanent: false, |
| 84 | + }, |
| 85 | + } |
43 | 86 | } |
| 87 | + |
| 88 | + const reminders: Reminder[] = await fetch(`${config.discordUri}/reminders/${user.id}/get`, { |
| 89 | + headers: { Authorization: `${config.discordSecret}` }, |
| 90 | + }).then((res) => res.json()) |
| 91 | + |
| 92 | + return { props: { user, reminders } } |
| 93 | +} |
0 commit comments