|
1 |
| -import { json, LoaderFunctionArgs } from "@remix-run/node"; |
| 1 | +import { json, LoaderFunctionArgs, redirect } from "@remix-run/node"; |
2 | 2 | import { useLoaderData } from "@remix-run/react";
|
3 | 3 | import invariant from "tiny-invariant";
|
4 | 4 | import { readSheet } from "~/models/sheet.server";
|
5 | 5 | import { authenticator } from "~/services/auth.server";
|
6 | 6 | import StatusSelector from "./components/StatusSelector";
|
7 | 7 | import EditSheet from "./components/EditSheet";
|
8 | 8 | import MarkSheet from "./components/MarkSheet";
|
| 9 | +import { readSailor } from "~/models/sailor.server"; |
| 10 | +import SheetStats from "./components/SheetStats"; |
| 11 | +import { readSheetSubmissions } from "~/models/submission.server"; |
9 | 12 |
|
10 | 13 | export const loader = async ({ params, request }: LoaderFunctionArgs) => {
|
11 |
| - await authenticator.isAuthenticated(request, { |
| 14 | + const sailorId = await authenticator.isAuthenticated(request, { |
12 | 15 | failureRedirect: "/login",
|
13 | 16 | });
|
14 | 17 | invariant(params.sheetId, `params.sheetId is required`);
|
15 |
| - // todo: redirect if not an admin |
| 18 | + const sailor = await readSailor(sailorId); |
| 19 | + if (!sailor || !sailor.admin) return redirect("/"); |
16 | 20 | const sheet = await readSheet(params.sheetId);
|
| 21 | + const submissions = await readSheetSubmissions(params.sheetId); |
17 | 22 | invariant(!!sheet, "No such sheet");
|
18 |
| - return json({ sheet }); |
| 23 | + return json({ sheet, submissions }); |
19 | 24 | };
|
20 | 25 |
|
21 | 26 | export default function SheetEdit() {
|
22 |
| - const { sheet } = useLoaderData<typeof loader>(); |
| 27 | + const { sheet, submissions } = useLoaderData<typeof loader>(); |
23 | 28 | return (
|
24 | 29 | <>
|
25 | 30 | <StatusSelector sheet={sheet} />
|
26 | 31 | {sheet.status === "DRAFT" && <EditSheet sheet={sheet} />}
|
27 |
| - {sheet.status === "OPEN" && <span>Currently accepting submissions</span>} |
| 32 | + {sheet.status === "OPEN" && <SheetStats submissions={submissions} />} |
28 | 33 | {sheet.status === "CLOSED" && <MarkSheet sheet={sheet} />}
|
29 | 34 | </>
|
30 | 35 | );
|
|
0 commit comments