aboutsummaryrefslogtreecommitdiffhomepage
path: root/frontend/app/routes/admin.games.tsx
blob: 085a97e6256d95185c7e4091a15854356d4bced7 (plain)
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
import type { LoaderFunctionArgs, MetaFunction } from "@remix-run/node";
import { useLoaderData, Link } from "@remix-run/react";
import { isAuthenticated } from "../.server/auth";
import { apiClient } from "../.server/api/client";

export const meta: MetaFunction = () => {
  return [{ title: "[Admin] Games | iOSDC Japan 2024 Albatross.swift" }];
};

export async function loader({ request }: LoaderFunctionArgs) {
  const { user, token } = await isAuthenticated(request, {
    failureRedirect: "/login",
  });
  if (!user.is_admin) {
    throw new Error("Unauthorized");
  }
  const { data, error } = await apiClient.GET("/admin/games", {
    params: {
      header: {
        Authorization: `Bearer ${token}`,
      },
    },
  });
  if (error) {
    throw new Error(error.message);
  }
  return { games: data.games };
}

export default function AdminGames() {
  const { games } = useLoaderData<typeof loader>()!;

  return (
    <div>
      <div>
        <h1>[Admin] Games</h1>
        <ul>
          {games.map((game) => (
            <li key={game.game_id}>
              <Link to={`/admin/games/${game.game_id}`}>
                {game.display_name} (id={game.game_id})
              </Link>
            </li>
          ))}
        </ul>
      </div>
    </div>
  );
}