aboutsummaryrefslogtreecommitdiffhomepage
path: root/frontend/app/routes/dashboard.tsx
blob: 3cc8e13b16194c1f5905ce87627462481a99dd49 (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
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
77
78
79
80
81
82
83
84
85
86
87
88
import type { LoaderFunctionArgs, MetaFunction } from "@remix-run/node";
import { redirect } from "@remix-run/node";
import { Link, useLoaderData, Form } from "@remix-run/react";
import { isAuthenticated } from "../.server/auth";
import { apiClient } from "../.server/api/client";

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

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

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

  return (
    <div className="min-h-screen p-8">
      <div className="p-6 rounded shadow-md max-w-4xl mx-auto">
        <h1 className="text-3xl font-bold mb-4">
          {user.username}{" "}
          {user.is_admin && <span className="text-red-500 text-lg">admin</span>}
        </h1>
        <h2 className="text-2xl font-semibold mb-2">User</h2>
        <div className="mb-6">
          <ul className="list-disc list-inside">
            <li>Name: {user.display_name}</li>
          </ul>
        </div>
        <h2 className="text-2xl font-semibold mb-2">Games</h2>
        <div>
          <ul className="list-disc list-inside">
            {games.map((game) => (
              <li key={game.game_id}>
                {game.display_name}{" "}
                {game.state === "closed" || game.state === "finished" ? (
                  <span className="inline-block px-6 py-2 text-gray-400 bg-gray-200 cursor-not-allowed rounded">
                    Entry
                  </span>
                ) : (
                  <Link
                    to={`/golf/${game.game_id}/play`}
                    className="inline-block px-6 py-2 text-white bg-blue-500 hover:bg-blue-700 rounded"
                  >
                    Entry
                  </Link>
                )}
              </li>
            ))}
          </ul>
        </div>
        <div>
          <Form method="post" action="/logout">
            <button
              className="mt-6 px-6 py-2 text-white bg-red-500 hover:bg-red-700 rounded"
              type="submit"
            >
              Logout
            </button>
          </Form>
        </div>
      </div>
    </div>
  );
}