This commit is contained in:
2025-12-04 23:31:55 +01:00
parent 9fa21a488a
commit fb509607c1
96 changed files with 5839 additions and 1609 deletions

View File

@@ -2,14 +2,9 @@
import { useState } from 'react';
import Button from '../ui/Button';
import {
getMembership,
joinLeague,
leaveLeague,
requestToJoin,
getCurrentDriverId,
type MembershipStatus,
} from '@/lib/racingLegacyFacade';
import { getMembership, type MembershipStatus } from '@/lib/leagueMembership';
import { useEffectiveDriverId } from '@/lib/currentDriver';
import { getJoinLeagueUseCase, getLeagueMembershipRepository } from '@/lib/di-container';
interface JoinLeagueButtonProps {
leagueId: string;
@@ -22,9 +17,9 @@ export default function JoinLeagueButton({
isInviteOnly = false,
onMembershipChange,
}: JoinLeagueButtonProps) {
const currentDriverId = getCurrentDriverId();
const currentDriverId = useEffectiveDriverId();
const membership = getMembership(leagueId, currentDriverId);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [showConfirmDialog, setShowConfirmDialog] = useState(false);
@@ -34,11 +29,27 @@ export default function JoinLeagueButton({
setLoading(true);
setError(null);
try {
const membershipRepo = getLeagueMembershipRepository();
if (isInviteOnly) {
requestToJoin(leagueId, currentDriverId);
// For alpha, treat "request to join" as creating a pending membership
const pending = await membershipRepo.getMembership(leagueId, currentDriverId);
if (pending) {
throw new Error('Already a member or have a pending request');
}
await membershipRepo.saveMembership({
leagueId,
driverId: currentDriverId,
role: 'member',
status: 'pending',
joinedAt: new Date(),
});
} else {
joinLeague(leagueId, currentDriverId);
const useCase = getJoinLeagueUseCase();
await useCase.execute({ leagueId, driverId: currentDriverId });
}
onMembershipChange?.();
setShowConfirmDialog(false);
} catch (err) {
@@ -52,7 +63,16 @@ export default function JoinLeagueButton({
setLoading(true);
setError(null);
try {
leaveLeague(leagueId, currentDriverId);
const membershipRepo = getLeagueMembershipRepository();
const existing = await membershipRepo.getMembership(leagueId, currentDriverId);
if (!existing) {
throw new Error('Not a member of this league');
}
if (existing.role === 'owner') {
throw new Error('League owner cannot leave the league');
}
await membershipRepo.removeMembership(leagueId, currentDriverId);
onMembershipChange?.();
setShowConfirmDialog(false);
} catch (err) {