-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreview.tsx
76 lines (66 loc) · 1.81 KB
/
review.tsx
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
import {
decksWithReviewInfo,
DeckWithReviewInfo,
} from "@/koala/decks/decks-with-review-info";
import { getServersideUser } from "@/koala/get-serverside-user";
import Link from "next/link";
import { GetServerSideProps } from "next/types";
type ReviewPageProps = {
decks: DeckWithReviewInfo[];
};
export const getServerSideProps: GetServerSideProps<ReviewPageProps> = async (
context,
) => {
const dbUser = await getServersideUser(context);
if (!dbUser) {
return { redirect: { destination: "/api/auth/signin", permanent: false } };
}
const { backfillDecks } = await import("@/koala/decks/backfill-decks");
await backfillDecks(dbUser.id);
const decks = await decksWithReviewInfo(dbUser.id);
if (decks.length === 1) {
return {
redirect: {
destination: `/review/${decks[0].id}`,
permanent: false,
},
};
}
return {
props: {
decks,
},
};
};
export default function ReviewPage({ decks }: ReviewPageProps) {
if (decks.length === 0) {
return (
<div>
<h1>Welcome, Please Add Cards First</h1>
<p>
You can add cards <Link href={"/create"}>here</Link>.
</p>
</div>
);
}
const sortedByDue = decks.sort((b, a) => a.quizzesDue - b.quizzesDue);
return (
<div>
<h1>Decks</h1>
<ul>
{sortedByDue.map((deck) => {
const cardsDue = deck.quizzesDue ? `${deck.quizzesDue} due` : "";
const cardsNew = deck.newQuizzes ? `${deck.newQuizzes} new` : "";
const cards = [cardsDue, cardsNew].filter(Boolean).join(", ");
return (
<li key={deck.id}>
<Link href={`/review/${deck.id}`}>
{deck.deckName} {cards ? `(${cards})` : ""}
</Link>
</li>
);
})}
</ul>
</div>
);
}