import { Form, redirect, Link } from "react-router"; import { auth } from "~/lib/auth.server"; import type { Route } from "./+types/i.$inviteCode"; import { findSeasonByInviteCode, findLeagueById, findCommissionersByLeagueId, findAvailableTeams, claimTeam, isUserLeagueMember, findUserById, } from "~/models"; import { prependOwnerToTeamName } from "~/utils/team-names"; import { Button } from "~/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "~/components/ui/card"; export function meta(): Route.MetaDescriptors { return [{ title: "Join League - Brackt" }]; } export async function loader(args: Route.LoaderArgs) { const { params } = args; const { inviteCode } = params; const session = await auth.api.getSession({ headers: args.request.headers }); const userId = session?.user.id ?? null; // Find season by invite code const season = await findSeasonByInviteCode(inviteCode); if (!season) { throw new Response("Invalid invite code", { status: 404 }); } // Find the league const league = await findLeagueById(season.leagueId); if (!league) { throw new Response("League not found", { status: 404 }); } // Find commissioners to get the commissioner's name const commissioners = await findCommissionersByLeagueId(league.id); const firstCommissioner = commissioners[0]; // Check if user is already a member let isAlreadyMember = false; if (userId) { isAlreadyMember = await isUserLeagueMember(league.id, userId); } return { league, season, inviteCode, firstCommissioner, isAlreadyMember, isLoggedIn: !!userId, }; } export async function action(args: Route.ActionArgs) { const { params } = args; const { inviteCode } = 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 to join a league", { status: 401 }); } // Find season by invite code const season = await findSeasonByInviteCode(inviteCode); if (!season) { throw new Response("Invalid invite code", { status: 404 }); } // Check if user is already a member const isAlreadyMember = await isUserLeagueMember(season.leagueId, userId); if (isAlreadyMember) { // Already a member, redirect to league page return redirect(`/leagues/${season.leagueId}`); } // Find an available team const availableTeams = await findAvailableTeams(season.id); if (availableTeams.length === 0) { throw new Response("No available teams in this league", { status: 400 }); } // Look up user before claiming to fail fast and get their name const user = await findUserById(userId); if (!user) { throw new Response("User account not found. Please try again.", { status: 500 }); } // Claim the first available team, setting owner and name atomically const team = availableTeams[0]; const username = user.username ?? user.displayName ?? "Member"; const teamName = prependOwnerToTeamName(team.name, username); await claimTeam(team.id, userId, teamName); // Redirect to league page return redirect(`/leagues/${season.leagueId}?joined=true`); } export default function InvitePage({ loaderData }: Route.ComponentProps) { const { league, firstCommissioner, isAlreadyMember, isLoggedIn } = loaderData; return (
You're Invited! {firstCommissioner ? "A commissioner" : "Someone"} has invited you to join{" "} {league.name}

League Details

League: {league.name}

Created:{" "} {new Date(league.createdAt).toLocaleDateString()}

{isAlreadyMember ? (

You're already a member of this league!

) : isLoggedIn ? (

Click the button below to join this league and claim your team.

) : (

You'll need to sign in or create an account to join this league.

)}
); }