di usage in website
This commit is contained in:
100
apps/website/lib/hooks/useLeagueStewarding.ts
Normal file
100
apps/website/lib/hooks/useLeagueStewarding.ts
Normal file
@@ -0,0 +1,100 @@
|
||||
'use client';
|
||||
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { useInject } from '@/lib/di/hooks/useInject';
|
||||
import { LEAGUE_STEWARDING_SERVICE_TOKEN, LEAGUE_MEMBERSHIP_SERVICE_TOKEN } from '@/lib/di/tokens';
|
||||
import { LeagueRoleUtility } from '@/lib/utilities/LeagueRoleUtility';
|
||||
import { useCurrentDriver } from '@/hooks/driver/useCurrentDriver';
|
||||
|
||||
/**
|
||||
* Hook for league stewarding data with admin check
|
||||
*/
|
||||
export function useLeagueStewarding(leagueId: string) {
|
||||
const leagueStewardingService = useInject(LEAGUE_STEWARDING_SERVICE_TOKEN);
|
||||
const leagueMembershipService = useInject(LEAGUE_MEMBERSHIP_SERVICE_TOKEN);
|
||||
const { data: currentDriver } = useCurrentDriver();
|
||||
const currentDriverId = currentDriver?.id;
|
||||
|
||||
// Check admin status
|
||||
const adminQuery = useQuery({
|
||||
queryKey: ['leagueMembership', leagueId, currentDriverId],
|
||||
queryFn: async () => {
|
||||
if (!currentDriverId) return false;
|
||||
const membership = await leagueMembershipService.getMembership(leagueId, currentDriverId);
|
||||
return membership ? LeagueRoleUtility.isLeagueAdminOrHigherRole(membership.role) : false;
|
||||
},
|
||||
staleTime: 5 * 60 * 1000, // 5 minutes
|
||||
gcTime: 10 * 60 * 1000, // 10 minutes
|
||||
enabled: !!leagueId && !!currentDriverId,
|
||||
});
|
||||
|
||||
// Load stewarding data (only if admin)
|
||||
const stewardingQuery = useQuery({
|
||||
queryKey: ['leagueStewarding', leagueId],
|
||||
queryFn: async () => {
|
||||
return await leagueStewardingService.getLeagueStewardingData(leagueId);
|
||||
},
|
||||
staleTime: 2 * 60 * 1000, // 2 minutes
|
||||
gcTime: 5 * 60 * 1000, // 5 minutes
|
||||
enabled: !!leagueId && adminQuery.data === true,
|
||||
});
|
||||
|
||||
return {
|
||||
isAdmin: adminQuery.data,
|
||||
adminLoading: adminQuery.isLoading,
|
||||
adminError: adminQuery.error,
|
||||
|
||||
stewardingData: stewardingQuery.data,
|
||||
stewardingLoading: stewardingQuery.isLoading,
|
||||
stewardingError: stewardingQuery.error,
|
||||
|
||||
refetchStewarding: stewardingQuery.refetch,
|
||||
refetchAdmin: adminQuery.refetch,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook for league stewarding mutations
|
||||
*/
|
||||
export function useLeagueStewardingMutations(leagueId: string) {
|
||||
const leagueStewardingService = useInject(LEAGUE_STEWARDING_SERVICE_TOKEN);
|
||||
const { data: currentDriver } = useCurrentDriver();
|
||||
const currentDriverId = currentDriver?.id;
|
||||
|
||||
const reviewProtest = async (input: { protestId: string; decision: string; decisionNotes: string }) => {
|
||||
if (!currentDriverId) throw new Error('No current driver');
|
||||
return await leagueStewardingService.reviewProtest({
|
||||
protestId: input.protestId,
|
||||
stewardId: currentDriverId,
|
||||
decision: input.decision,
|
||||
decisionNotes: input.decisionNotes,
|
||||
});
|
||||
};
|
||||
|
||||
const applyPenalty = async (input: {
|
||||
raceId: string;
|
||||
driverId: string;
|
||||
type: string;
|
||||
value: number;
|
||||
reason: string;
|
||||
protestId: string;
|
||||
notes: string;
|
||||
}) => {
|
||||
if (!currentDriverId) throw new Error('No current driver');
|
||||
return await leagueStewardingService.applyPenalty({
|
||||
raceId: input.raceId,
|
||||
driverId: input.driverId,
|
||||
stewardId: currentDriverId,
|
||||
type: input.type,
|
||||
value: input.value,
|
||||
reason: input.reason,
|
||||
protestId: input.protestId,
|
||||
notes: input.notes,
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
reviewProtest,
|
||||
applyPenalty,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user