import { Form, redirect, useNavigate } from "react-router"; import { auth } from "~/lib/auth.server"; import type { Route } from "./+types/$teamId.settings"; import { findTeamById, updateTeam, removeTeamOwner, } from "~/models/team"; import { findSeasonById } from "~/models/season"; import { Button } from "~/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "~/components/ui/card"; import { Input } from "~/components/ui/input"; import { Label } from "~/components/ui/label"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from "~/components/ui/alert-dialog"; export function meta({ data }: Route.MetaArgs): Route.MetaDescriptors { return [{ title: `Team Settings — ${data?.team?.name ?? "Team"} - Brackt` }]; } export async function loader(args: Route.LoaderArgs) { const { params } = args; const { teamId } = params; const session = await auth.api.getSession({ headers: args.request.headers }); const userId = session?.user.id ?? null; if (!userId) { throw new Response("You must be logged in", { status: 401 }); } const team = await findTeamById(teamId); if (!team) { throw new Response("Team not found", { status: 404 }); } // Only the team owner can access settings if (team.ownerId !== userId) { throw new Response("You do not have access to this team", { status: 403 }); } const season = await findSeasonById(team.seasonId); if (!season) { throw new Response("Season not found", { status: 404 }); } return { team, season }; } export async function action(args: Route.ActionArgs) { const { params, request } = args; const { teamId } = params; const session = await auth.api.getSession({ headers: args.request.headers }); const userId = session?.user.id ?? null; if (!userId) { throw new Response("You must be logged in", { status: 401 }); } const team = await findTeamById(teamId); if (!team) { throw new Response("Team not found", { status: 404 }); } // Only the team owner can modify settings if (team.ownerId !== userId) { throw new Response("You do not have access to this team", { status: 403 }); } const formData = await request.formData(); const intent = formData.get("intent"); if (intent === "update") { const name = formData.get("name"); const logoUrl = formData.get("logoUrl"); if (!name || typeof name !== "string") { return { error: "Team name is required" }; } await updateTeam(teamId, { name: name.trim(), logoUrl: logoUrl && typeof logoUrl === "string" ? logoUrl.trim() : undefined, }); const season = await findSeasonById(team.seasonId); return redirect(`/leagues/${season?.leagueId}?updated=true`); } if (intent === "leave") { await removeTeamOwner(teamId); return redirect("/?left=true"); } return { error: "Invalid action" }; } export default function TeamSettings({ loaderData }: Route.ComponentProps) { const { team, season } = loaderData; const navigate = useNavigate(); return (
Manage your team settings and preferences