From 0b6a831765d8b7f1d669fea6723ebd7d090bd751 Mon Sep 17 00:00:00 2001 From: "Kalle (Sendou)" <38327916+Sendouc@users.noreply.github.com> Date: Sat, 25 Dec 2021 22:46:44 +0200 Subject: [PATCH] manage-roster page to local action function --- app/components/tournament/TeamRoster.tsx | 26 +++++++++---------- .../$teamId/remove-player.$playerId.ts | 17 ------------ .../manage-roster.tsx | 23 ++++++++++++---- app/utils/index.ts | 4 +-- 4 files changed, 32 insertions(+), 38 deletions(-) delete mode 100644 app/routes/api/tournamentTeam/$teamId/remove-player.$playerId.ts diff --git a/app/components/tournament/TeamRoster.tsx b/app/components/tournament/TeamRoster.tsx index db8960ec7..c7e8314a7 100644 --- a/app/components/tournament/TeamRoster.tsx +++ b/app/components/tournament/TeamRoster.tsx @@ -1,4 +1,5 @@ -import { useFetcher } from "remix"; +import { Form, useTransition } from "remix"; +import { ManageRosterAction } from "~/routes/to/$organization.$tournament/manage-roster"; import { useUser } from "~/utils/hooks"; import { Button } from "../Button"; @@ -76,20 +77,19 @@ function DeleteFromRosterButton({ playerId: string; teamId: string; }) { - const fetcher = useFetcher(); + const transition = useTransition(); return ( - - - + ); } diff --git a/app/routes/api/tournamentTeam/$teamId/remove-player.$playerId.ts b/app/routes/api/tournamentTeam/$teamId/remove-player.$playerId.ts deleted file mode 100644 index 7b6cc9491..000000000 --- a/app/routes/api/tournamentTeam/$teamId/remove-player.$playerId.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { ActionFunction } from "@remix-run/server-runtime"; -import { removePlayerFromTeam } from "~/services/tournament"; -import { requireUser } from "~/utils"; - -export const action: ActionFunction = async ({ params, context }) => { - const teamId = params.teamId!; - const playerId = params.playerId!; - - const user = requireUser(context); - - await removePlayerFromTeam({ - captainId: user.id, - playerId, - teamId, - }); - return new Response("Player deleted from team", { status: 200 }); -}; diff --git a/app/routes/to/$organization.$tournament/manage-roster.tsx b/app/routes/to/$organization.$tournament/manage-roster.tsx index 7217c7153..4fb7290d0 100644 --- a/app/routes/to/$organization.$tournament/manage-roster.tsx +++ b/app/routes/to/$organization.$tournament/manage-roster.tsx @@ -31,6 +31,11 @@ export const links: LinksFunction = () => { return [{ rel: "stylesheet", href: styles }]; }; +export enum ManageRosterAction { + ADD_PLAYER = "ADD_PLAYER", + DELETE_PLAYER = "DELETE_PLAYER", +} + type ActionData = { fieldErrors?: { userId?: string | undefined }; fields?: { @@ -45,11 +50,12 @@ export const action: ActionFunction = async ({ const data = Object.fromEntries(await request.formData()); invariant(typeof data.userId === "string", "Invalid type for userId"); invariant(typeof data.teamId === "string", "Invalid type for teamId"); + invariant(typeof data._action === "string", "Invalid type for _action"); const user = requireUser(context); - switch (request.method) { - case "POST": + switch (data._action) { + case ManageRosterAction.ADD_PLAYER: { try { await putPlayerToTeam({ teamId: data.teamId, @@ -69,18 +75,21 @@ export const action: ActionFunction = async ({ } return new Response("Added player to team", { status: 200 }); - case "DELETE": + } + case ManageRosterAction.DELETE_PLAYER: { await removePlayerFromTeam({ captainId: user.id, playerId: data.userId, teamId: data.teamId, }); return new Response("Player deleted from team", { status: 200 }); - default: + } + default: { return new Response(undefined, { status: 405, headers: { Allow: "POST, DELETE" }, }); + } } }; @@ -158,6 +167,11 @@ export default function ManageRosterPage() { {trustingUsers.length > 0 && (
+