page wrapper
This commit is contained in:
@@ -1,20 +0,0 @@
|
||||
import { useQuery, UseQueryOptions } from '@tanstack/react-query';
|
||||
import { useInject } from '@/lib/di/hooks/useInject';
|
||||
import { DASHBOARD_SERVICE_TOKEN } from '@/lib/di/tokens';
|
||||
import { enhanceQueryResult } from '@/lib/di/hooks/useReactQueryWithApiError';
|
||||
import { ApiError } from '@/lib/api/base/ApiError';
|
||||
import { DashboardOverviewViewModel } from '@/lib/view-models/DashboardOverviewViewModel';
|
||||
|
||||
export function useDashboardOverview(
|
||||
options?: Omit<UseQueryOptions<DashboardOverviewViewModel, ApiError>, 'queryKey' | 'queryFn'>
|
||||
) {
|
||||
const dashboardService = useInject(DASHBOARD_SERVICE_TOKEN);
|
||||
|
||||
const queryResult = useQuery({
|
||||
queryKey: ['dashboardOverview'],
|
||||
queryFn: () => dashboardService.getDashboardOverview(),
|
||||
...options,
|
||||
});
|
||||
|
||||
return enhanceQueryResult(queryResult);
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
export { useCurrentDriver } from './useCurrentDriver';
|
||||
export { useDriverLeaderboard } from './useDriverLeaderboard';
|
||||
export { useDriverLeaderboard } from '@/lib/hooks/useDriverLeaderboard';
|
||||
export { useDriverProfile } from './useDriverProfile';
|
||||
export { useUpdateDriverProfile } from './useUpdateDriverProfile';
|
||||
export { useCreateDriver } from './useCreateDriver';
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { useInject } from '@/lib/di/hooks/useInject';
|
||||
import { DRIVER_SERVICE_TOKEN } from '@/lib/di/tokens';
|
||||
import { enhanceQueryResult } from '@/lib/di/hooks/useReactQueryWithApiError';
|
||||
|
||||
export function useDriverLeaderboard() {
|
||||
const driverService = useInject(DRIVER_SERVICE_TOKEN);
|
||||
|
||||
const queryResult = useQuery({
|
||||
queryKey: ['driverLeaderboard'],
|
||||
queryFn: () => driverService.getDriverLeaderboard(),
|
||||
});
|
||||
|
||||
return enhanceQueryResult(queryResult);
|
||||
}
|
||||
47
apps/website/hooks/driver/useDriverProfilePageData.ts
Normal file
47
apps/website/hooks/driver/useDriverProfilePageData.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { usePageDataMultiple } from '@/lib/page/usePageData';
|
||||
import { useInject } from '@/lib/di/hooks/useInject';
|
||||
import { DRIVER_SERVICE_TOKEN, TEAM_SERVICE_TOKEN } from '@/lib/di/tokens';
|
||||
|
||||
export function useDriverProfilePageData(driverId: string) {
|
||||
const driverService = useInject(DRIVER_SERVICE_TOKEN);
|
||||
const teamService = useInject(TEAM_SERVICE_TOKEN);
|
||||
|
||||
return usePageDataMultiple({
|
||||
driverProfile: {
|
||||
queryKey: ['driverProfile', driverId],
|
||||
queryFn: () => driverService.getDriverProfile(driverId),
|
||||
enabled: !!driverId,
|
||||
},
|
||||
teamMemberships: {
|
||||
queryKey: ['teamMemberships', driverId],
|
||||
queryFn: async () => {
|
||||
if (!driverId) return [];
|
||||
|
||||
const allTeams = await teamService.getAllTeams();
|
||||
let teamMemberships: Array<{
|
||||
team: { id: string; name: string };
|
||||
role: string;
|
||||
joinedAt: Date;
|
||||
}> = [];
|
||||
|
||||
for (const team of allTeams) {
|
||||
const teamMembers = await teamService.getTeamMembers(team.id, driverId, '');
|
||||
const membership = teamMembers?.find(member => member.driverId === driverId);
|
||||
if (membership) {
|
||||
teamMemberships.push({
|
||||
team: {
|
||||
id: team.id,
|
||||
name: team.name,
|
||||
},
|
||||
role: membership.role,
|
||||
joinedAt: new Date(membership.joinedAt),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return teamMemberships;
|
||||
},
|
||||
enabled: !!driverId,
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { useInject } from '@/lib/di/hooks/useInject';
|
||||
import { LEAGUE_SERVICE_TOKEN } from '@/lib/di/tokens';
|
||||
import { enhanceQueryResult } from '@/lib/di/hooks/useReactQueryWithApiError';
|
||||
|
||||
export function useAllLeaguesWithSponsors() {
|
||||
const leagueService = useInject(LEAGUE_SERVICE_TOKEN);
|
||||
|
||||
const queryResult = useQuery({
|
||||
queryKey: ['allLeaguesWithSponsors'],
|
||||
queryFn: () => leagueService.getAllLeagues(),
|
||||
});
|
||||
|
||||
return enhanceQueryResult(queryResult);
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
import { useQuery, UseQueryOptions } from '@tanstack/react-query';
|
||||
import { useInject } from '@/lib/di/hooks/useInject';
|
||||
import { LEAGUE_SERVICE_TOKEN } from '@/lib/di/tokens';
|
||||
import { enhanceQueryResult } from '@/lib/di/hooks/useReactQueryWithApiError';
|
||||
import { ApiError } from '@/lib/api/base/ApiError';
|
||||
import type { LeagueDetailPageViewModel } from '@/lib/view-models/LeagueDetailPageViewModel';
|
||||
|
||||
export function useLeagueDetailWithSponsors(
|
||||
leagueId: string,
|
||||
options?: Omit<UseQueryOptions<LeagueDetailPageViewModel | null, ApiError>, 'queryKey' | 'queryFn'>
|
||||
) {
|
||||
const leagueService = useInject(LEAGUE_SERVICE_TOKEN);
|
||||
|
||||
const queryResult = useQuery({
|
||||
queryKey: ['leagueDetailWithSponsors', leagueId],
|
||||
queryFn: () => leagueService.getLeagueDetailPageData(leagueId),
|
||||
...options,
|
||||
});
|
||||
|
||||
return enhanceQueryResult(queryResult);
|
||||
}
|
||||
38
apps/website/hooks/league/useLeagueScheduleAdminPageData.ts
Normal file
38
apps/website/hooks/league/useLeagueScheduleAdminPageData.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { usePageData } from '@/lib/page/usePageData';
|
||||
import { useInject } from '@/lib/di/hooks/useInject';
|
||||
import { LEAGUE_SERVICE_TOKEN, LEAGUE_MEMBERSHIP_SERVICE_TOKEN } from '@/lib/di/tokens';
|
||||
import { LeagueRoleUtility } from '@/lib/utilities/LeagueRoleUtility';
|
||||
|
||||
export function useLeagueAdminStatus(leagueId: string, currentDriverId: string) {
|
||||
const leagueMembershipService = useInject(LEAGUE_MEMBERSHIP_SERVICE_TOKEN);
|
||||
|
||||
return usePageData({
|
||||
queryKey: ['admin-check', leagueId, currentDriverId],
|
||||
queryFn: async () => {
|
||||
await leagueMembershipService.fetchLeagueMemberships(leagueId);
|
||||
const membership = leagueMembershipService.getMembership(leagueId, currentDriverId);
|
||||
return membership ? LeagueRoleUtility.isLeagueAdminOrHigherRole(membership.role) : false;
|
||||
},
|
||||
enabled: !!leagueId && !!currentDriverId,
|
||||
});
|
||||
}
|
||||
|
||||
export function useLeagueSeasons(leagueId: string, isAdmin: boolean) {
|
||||
const leagueService = useInject(LEAGUE_SERVICE_TOKEN);
|
||||
|
||||
return usePageData({
|
||||
queryKey: ['leagueSeasons', leagueId],
|
||||
queryFn: () => leagueService.getLeagueSeasonSummaries(leagueId),
|
||||
enabled: !!leagueId && !!isAdmin,
|
||||
});
|
||||
}
|
||||
|
||||
export function useLeagueAdminSchedule(leagueId: string, selectedSeasonId: string, isAdmin: boolean) {
|
||||
const leagueService = useInject(LEAGUE_SERVICE_TOKEN);
|
||||
|
||||
return usePageData({
|
||||
queryKey: ['adminSchedule', leagueId, selectedSeasonId],
|
||||
queryFn: () => leagueService.getAdminSchedule(leagueId, selectedSeasonId),
|
||||
enabled: !!leagueId && !!selectedSeasonId && !!isAdmin,
|
||||
});
|
||||
}
|
||||
21
apps/website/hooks/league/useLeagueSponsorshipsPageData.ts
Normal file
21
apps/website/hooks/league/useLeagueSponsorshipsPageData.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { usePageDataMultiple } from '@/lib/page/usePageData';
|
||||
import { useInject } from '@/lib/di/hooks/useInject';
|
||||
import { LEAGUE_SERVICE_TOKEN, LEAGUE_MEMBERSHIP_SERVICE_TOKEN } from '@/lib/di/tokens';
|
||||
|
||||
export function useLeagueSponsorshipsPageData(leagueId: string, currentDriverId: string) {
|
||||
const leagueService = useInject(LEAGUE_SERVICE_TOKEN);
|
||||
const leagueMembershipService = useInject(LEAGUE_MEMBERSHIP_SERVICE_TOKEN);
|
||||
|
||||
return usePageDataMultiple({
|
||||
league: {
|
||||
queryKey: ['leagueDetail', leagueId, currentDriverId],
|
||||
queryFn: () => leagueService.getLeagueDetail(leagueId, currentDriverId),
|
||||
},
|
||||
membership: {
|
||||
queryKey: ['leagueMembership', leagueId, currentDriverId],
|
||||
queryFn: () => leagueMembershipService.fetchLeagueMemberships(leagueId).then(() => {
|
||||
return leagueMembershipService.getMembership(leagueId, currentDriverId);
|
||||
}),
|
||||
},
|
||||
});
|
||||
}
|
||||
46
apps/website/hooks/league/useLeagueStewardingMutations.ts
Normal file
46
apps/website/hooks/league/useLeagueStewardingMutations.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { usePageMutation } from '@/lib/page/usePageData';
|
||||
|
||||
export function useLeagueStewardingMutations(onRefetch: () => void) {
|
||||
const acceptProtestMutation = usePageMutation(
|
||||
async (variables: { protestId: string; penaltyType: string; penaltyValue: number; stewardNotes: string; raceId: string; accusedDriverId: string; reason: string }) => {
|
||||
// TODO: Implement protest review and penalty application
|
||||
// await leagueStewardingService.reviewProtest({
|
||||
// protestId: variables.protestId,
|
||||
// stewardId: currentDriverId,
|
||||
// decision: 'uphold',
|
||||
// decisionNotes: variables.stewardNotes,
|
||||
// });
|
||||
|
||||
// await leagueStewardingService.applyPenalty({
|
||||
// raceId: variables.raceId,
|
||||
// driverId: variables.accusedDriverId,
|
||||
// stewardId: currentDriverId,
|
||||
// type: variables.penaltyType,
|
||||
// value: variables.penaltyValue,
|
||||
// reason: variables.reason,
|
||||
// protestId: variables.protestId,
|
||||
// notes: variables.stewardNotes,
|
||||
// });
|
||||
},
|
||||
{
|
||||
onSuccess: () => onRefetch(),
|
||||
}
|
||||
);
|
||||
|
||||
const rejectProtestMutation = usePageMutation(
|
||||
async (variables: { protestId: string; stewardNotes: string }) => {
|
||||
// TODO: Implement protest rejection
|
||||
// await leagueStewardingService.reviewProtest({
|
||||
// protestId: variables.protestId,
|
||||
// stewardId: currentDriverId,
|
||||
// decision: 'dismiss',
|
||||
// decisionNotes: variables.stewardNotes,
|
||||
// });
|
||||
},
|
||||
{
|
||||
onSuccess: () => onRefetch(),
|
||||
}
|
||||
);
|
||||
|
||||
return { acceptProtestMutation, rejectProtestMutation };
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { useInject } from '@/lib/di/hooks/useInject';
|
||||
import { LEAGUE_WALLET_SERVICE_TOKEN } from '@/lib/di/tokens';
|
||||
import { enhanceQueryResult } from '@/lib/di/hooks/useReactQueryWithApiError';
|
||||
|
||||
export function useLeagueWallet(leagueId: string) {
|
||||
const leagueWalletService = useInject(LEAGUE_WALLET_SERVICE_TOKEN);
|
||||
|
||||
const queryResult = useQuery({
|
||||
queryKey: ['leagueWallet', leagueId],
|
||||
queryFn: () => leagueWalletService.getWalletForLeague(leagueId),
|
||||
enabled: !!leagueId,
|
||||
});
|
||||
|
||||
return enhanceQueryResult(queryResult);
|
||||
}
|
||||
47
apps/website/hooks/league/useLeagueWalletPageData.ts
Normal file
47
apps/website/hooks/league/useLeagueWalletPageData.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { usePageData, usePageMutation } from '@/lib/page/usePageData';
|
||||
import { useInject } from '@/lib/di/hooks/useInject';
|
||||
import { LEAGUE_WALLET_SERVICE_TOKEN } from '@/lib/di/tokens';
|
||||
|
||||
export function useLeagueWalletPageData(leagueId: string) {
|
||||
const leagueWalletService = useInject(LEAGUE_WALLET_SERVICE_TOKEN);
|
||||
|
||||
const queryResult = usePageData({
|
||||
queryKey: ['leagueWallet', leagueId],
|
||||
queryFn: () => leagueWalletService.getWalletForLeague(leagueId),
|
||||
enabled: !!leagueId,
|
||||
});
|
||||
|
||||
return queryResult;
|
||||
}
|
||||
|
||||
export function useLeagueWalletWithdrawal(leagueId: string, data: any, refetch: () => void) {
|
||||
const leagueWalletService = useInject(LEAGUE_WALLET_SERVICE_TOKEN);
|
||||
|
||||
const withdrawMutation = usePageMutation(
|
||||
async ({ amount }: { amount: number }) => {
|
||||
if (!data) throw new Error('Wallet data not available');
|
||||
|
||||
const result = await leagueWalletService.withdraw(
|
||||
leagueId,
|
||||
amount,
|
||||
data.currency,
|
||||
'season-2', // Current active season
|
||||
'bank-account-***1234'
|
||||
);
|
||||
|
||||
if (!result.success) {
|
||||
throw new Error(result.message || 'Withdrawal failed');
|
||||
}
|
||||
|
||||
return result;
|
||||
},
|
||||
{
|
||||
onSuccess: () => {
|
||||
// Refetch wallet data after successful withdrawal
|
||||
refetch();
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
return withdrawMutation;
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { useInject } from '@/lib/di/hooks/useInject';
|
||||
import { PROTEST_SERVICE_TOKEN } from '@/lib/di/tokens';
|
||||
import { enhanceQueryResult } from '@/lib/di/hooks/useReactQueryWithApiError';
|
||||
|
||||
export function useLeagueProtests(leagueId: string) {
|
||||
const protestService = useInject(PROTEST_SERVICE_TOKEN);
|
||||
|
||||
const queryResult = useQuery({
|
||||
queryKey: ['leagueProtests', leagueId],
|
||||
queryFn: () => protestService.getLeagueProtests(leagueId),
|
||||
enabled: !!leagueId,
|
||||
});
|
||||
|
||||
return enhanceQueryResult(queryResult);
|
||||
}
|
||||
12
apps/website/hooks/race/useAllRacesPageData.ts
Normal file
12
apps/website/hooks/race/useAllRacesPageData.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { usePageData } from '@/lib/page/usePageData';
|
||||
import { useInject } from '@/lib/di/hooks/useInject';
|
||||
import { RACE_SERVICE_TOKEN } from '@/lib/di/tokens';
|
||||
|
||||
export function useAllRacesPageData() {
|
||||
const raceService = useInject(RACE_SERVICE_TOKEN);
|
||||
|
||||
return usePageData({
|
||||
queryKey: ['races', 'all'],
|
||||
queryFn: () => raceService.getAllRacesPageData(),
|
||||
});
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
import { useMutation, UseMutationOptions } from '@tanstack/react-query';
|
||||
import { useInject } from '@/lib/di/hooks/useInject';
|
||||
import { RACE_SERVICE_TOKEN } from '@/lib/di/tokens';
|
||||
import { ApiError } from '@/lib/api/base/ApiError';
|
||||
|
||||
export function useCancelRace(
|
||||
options?: Omit<UseMutationOptions<void, ApiError, string>, 'mutationFn'>
|
||||
) {
|
||||
const raceService = useInject(RACE_SERVICE_TOKEN);
|
||||
|
||||
return useMutation<void, ApiError, string>({
|
||||
mutationFn: (raceId) => raceService.cancelRace(raceId),
|
||||
...options,
|
||||
});
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
import { useMutation, UseMutationOptions } from '@tanstack/react-query';
|
||||
import { useInject } from '@/lib/di/hooks/useInject';
|
||||
import { RACE_SERVICE_TOKEN } from '@/lib/di/tokens';
|
||||
import { ApiError } from '@/lib/api/base/ApiError';
|
||||
|
||||
export function useCompleteRace(
|
||||
options?: Omit<UseMutationOptions<void, ApiError, string>, 'mutationFn'>
|
||||
) {
|
||||
const raceService = useInject(RACE_SERVICE_TOKEN);
|
||||
|
||||
return useMutation<void, ApiError, string>({
|
||||
mutationFn: (raceId) => raceService.completeRace(raceId),
|
||||
...options,
|
||||
});
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { useInject } from '@/lib/di/hooks/useInject';
|
||||
import { RACE_SERVICE_TOKEN } from '@/lib/di/tokens';
|
||||
import { enhanceQueryResult } from '@/lib/di/hooks/useReactQueryWithApiError';
|
||||
|
||||
export function useRaceDetail(raceId: string, driverId: string) {
|
||||
const raceService = useInject(RACE_SERVICE_TOKEN);
|
||||
|
||||
const queryResult = useQuery({
|
||||
queryKey: ['raceDetail', raceId, driverId],
|
||||
queryFn: () => raceService.getRaceDetail(raceId, driverId),
|
||||
enabled: !!raceId && !!driverId,
|
||||
});
|
||||
|
||||
return enhanceQueryResult(queryResult);
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { useInject } from '@/lib/di/hooks/useInject';
|
||||
import { RACE_RESULTS_SERVICE_TOKEN } from '@/lib/di/tokens';
|
||||
import { enhanceQueryResult } from '@/lib/di/hooks/useReactQueryWithApiError';
|
||||
|
||||
export function useRaceResultsDetail(raceId: string, currentUserId?: string) {
|
||||
const raceResultsService = useInject(RACE_RESULTS_SERVICE_TOKEN);
|
||||
|
||||
const queryResult = useQuery({
|
||||
queryKey: ['raceResultsDetail', raceId, currentUserId],
|
||||
queryFn: () => raceResultsService.getResultsDetail(raceId, currentUserId),
|
||||
enabled: !!raceId,
|
||||
});
|
||||
|
||||
return enhanceQueryResult(queryResult);
|
||||
}
|
||||
20
apps/website/hooks/race/useRaceResultsPageData.ts
Normal file
20
apps/website/hooks/race/useRaceResultsPageData.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { usePageDataMultiple } from '@/lib/page/usePageData';
|
||||
import { useInject } from '@/lib/di/hooks/useInject';
|
||||
import { RACE_RESULTS_SERVICE_TOKEN } from '@/lib/di/tokens';
|
||||
|
||||
export function useRaceResultsPageData(raceId: string, currentDriverId: string) {
|
||||
const raceResultsService = useInject(RACE_RESULTS_SERVICE_TOKEN);
|
||||
|
||||
return usePageDataMultiple({
|
||||
results: {
|
||||
queryKey: ['raceResultsDetail', raceId, currentDriverId],
|
||||
queryFn: () => raceResultsService.getResultsDetail(raceId, currentDriverId),
|
||||
enabled: !!raceId,
|
||||
},
|
||||
sof: {
|
||||
queryKey: ['raceWithSOF', raceId],
|
||||
queryFn: () => raceResultsService.getWithSOF(raceId),
|
||||
enabled: !!raceId,
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { useInject } from '@/lib/di/hooks/useInject';
|
||||
import { RACE_STEWARDING_SERVICE_TOKEN } from '@/lib/di/tokens';
|
||||
import { enhanceQueryResult } from '@/lib/di/hooks/useReactQueryWithApiError';
|
||||
|
||||
export function useRaceStewardingData(raceId: string, currentDriverId?: string) {
|
||||
const raceStewardingService = useInject(RACE_STEWARDING_SERVICE_TOKEN);
|
||||
|
||||
const queryResult = useQuery({
|
||||
queryKey: ['raceStewardingData', raceId, currentDriverId],
|
||||
queryFn: () => raceStewardingService.getRaceStewardingData(raceId, currentDriverId || ''),
|
||||
enabled: !!raceId,
|
||||
});
|
||||
|
||||
return enhanceQueryResult(queryResult);
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { useInject } from '@/lib/di/hooks/useInject';
|
||||
import { RACE_RESULTS_SERVICE_TOKEN } from '@/lib/di/tokens';
|
||||
import { enhanceQueryResult } from '@/lib/di/hooks/useReactQueryWithApiError';
|
||||
|
||||
export function useRaceWithSOF(raceId: string) {
|
||||
const raceResultsService = useInject(RACE_RESULTS_SERVICE_TOKEN);
|
||||
|
||||
const queryResult = useQuery({
|
||||
queryKey: ['raceWithSOF', raceId],
|
||||
queryFn: () => raceResultsService.getWithSOF(raceId),
|
||||
enabled: !!raceId,
|
||||
});
|
||||
|
||||
return enhanceQueryResult(queryResult);
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { useInject } from '@/lib/di/hooks/useInject';
|
||||
import { RACE_SERVICE_TOKEN } from '@/lib/di/tokens';
|
||||
import { enhanceQueryResult } from '@/lib/di/hooks/useReactQueryWithApiError';
|
||||
|
||||
export function useRacesPageData() {
|
||||
const raceService = useInject(RACE_SERVICE_TOKEN);
|
||||
|
||||
const queryResult = useQuery({
|
||||
queryKey: ['racesPageData'],
|
||||
queryFn: () => raceService.getRacesPageData(),
|
||||
});
|
||||
|
||||
return enhanceQueryResult(queryResult);
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
import { useMutation, UseMutationOptions } from '@tanstack/react-query';
|
||||
import { useInject } from '@/lib/di/hooks/useInject';
|
||||
import { RACE_SERVICE_TOKEN } from '@/lib/di/tokens';
|
||||
import { ApiError } from '@/lib/api/base/ApiError';
|
||||
|
||||
export function useReopenRace(
|
||||
options?: Omit<UseMutationOptions<void, ApiError, string>, 'mutationFn'>
|
||||
) {
|
||||
const raceService = useInject(RACE_SERVICE_TOKEN);
|
||||
|
||||
return useMutation<void, ApiError, string>({
|
||||
mutationFn: (raceId) => raceService.reopenRace(raceId),
|
||||
...options,
|
||||
});
|
||||
}
|
||||
120
apps/website/hooks/sponsor/useSponsorshipRequestsPageData.ts
Normal file
120
apps/website/hooks/sponsor/useSponsorshipRequestsPageData.ts
Normal file
@@ -0,0 +1,120 @@
|
||||
import { usePageData, usePageMutation } from '@/lib/page/usePageData';
|
||||
import { useInject } from '@/lib/di/hooks/useInject';
|
||||
import {
|
||||
SPONSORSHIP_SERVICE_TOKEN,
|
||||
DRIVER_SERVICE_TOKEN,
|
||||
LEAGUE_SERVICE_TOKEN,
|
||||
TEAM_SERVICE_TOKEN,
|
||||
LEAGUE_MEMBERSHIP_SERVICE_TOKEN
|
||||
} from '@/lib/di/tokens';
|
||||
import { LeagueRoleUtility } from '@/lib/utilities/LeagueRoleUtility';
|
||||
|
||||
export function useSponsorshipRequestsPageData(currentDriverId: string | null | undefined) {
|
||||
const sponsorshipService = useInject(SPONSORSHIP_SERVICE_TOKEN);
|
||||
const driverService = useInject(DRIVER_SERVICE_TOKEN);
|
||||
const leagueService = useInject(LEAGUE_SERVICE_TOKEN);
|
||||
const teamService = useInject(TEAM_SERVICE_TOKEN);
|
||||
const leagueMembershipService = useInject(LEAGUE_MEMBERSHIP_SERVICE_TOKEN);
|
||||
|
||||
const queryResult = usePageData({
|
||||
queryKey: ['sponsorshipRequests', 'all', currentDriverId || ''],
|
||||
queryFn: async () => {
|
||||
if (!currentDriverId) return [];
|
||||
|
||||
const allSections: any[] = [];
|
||||
|
||||
// 1. Driver's own sponsorship requests
|
||||
const driverRequests = await sponsorshipService.getPendingSponsorshipRequests({
|
||||
entityType: 'driver',
|
||||
entityId: currentDriverId,
|
||||
});
|
||||
|
||||
if (driverRequests.length > 0) {
|
||||
const driverProfile = await driverService.getDriverProfile(currentDriverId);
|
||||
allSections.push({
|
||||
entityType: 'driver',
|
||||
entityId: currentDriverId,
|
||||
entityName: driverProfile?.currentDriver?.name ?? 'Your Profile',
|
||||
requests: driverRequests,
|
||||
});
|
||||
}
|
||||
|
||||
// 2. Leagues where the user is admin/owner
|
||||
const allLeagues = await leagueService.getAllLeagues();
|
||||
for (const league of allLeagues) {
|
||||
const membership = await leagueMembershipService.getMembership(league.id, currentDriverId);
|
||||
if (membership && LeagueRoleUtility.isLeagueAdminOrHigherRole(membership.role)) {
|
||||
try {
|
||||
const leagueRequests = await sponsorshipService.getPendingSponsorshipRequests({
|
||||
entityType: 'season',
|
||||
entityId: league.id,
|
||||
});
|
||||
|
||||
if (leagueRequests.length > 0) {
|
||||
allSections.push({
|
||||
entityType: 'season',
|
||||
entityId: league.id,
|
||||
entityName: league.name,
|
||||
requests: leagueRequests,
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
// Silently skip if no requests found
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Teams where the user is owner/manager
|
||||
const allTeams = await teamService.getAllTeams();
|
||||
for (const team of allTeams) {
|
||||
const membership = await teamService.getMembership(team.id, currentDriverId);
|
||||
if (membership && (membership.role === 'owner' || membership.role === 'manager')) {
|
||||
const teamRequests = await sponsorshipService.getPendingSponsorshipRequests({
|
||||
entityType: 'team',
|
||||
entityId: team.id,
|
||||
});
|
||||
|
||||
if (teamRequests.length > 0) {
|
||||
allSections.push({
|
||||
entityType: 'team',
|
||||
entityId: team.id,
|
||||
entityName: team.name,
|
||||
requests: teamRequests,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return allSections;
|
||||
},
|
||||
enabled: !!currentDriverId,
|
||||
});
|
||||
|
||||
return queryResult;
|
||||
}
|
||||
|
||||
export function useSponsorshipRequestMutations(currentDriverId: string | null | undefined, refetch: () => void) {
|
||||
const sponsorshipService = useInject(SPONSORSHIP_SERVICE_TOKEN);
|
||||
|
||||
const acceptMutation = usePageMutation(
|
||||
async ({ requestId }: { requestId: string }) => {
|
||||
if (!currentDriverId) throw new Error('No driver ID');
|
||||
await sponsorshipService.acceptSponsorshipRequest(requestId, currentDriverId);
|
||||
},
|
||||
{
|
||||
onSuccess: () => refetch(),
|
||||
}
|
||||
);
|
||||
|
||||
const rejectMutation = usePageMutation(
|
||||
async ({ requestId, reason }: { requestId: string; reason?: string }) => {
|
||||
if (!currentDriverId) throw new Error('No driver ID');
|
||||
await sponsorshipService.rejectSponsorshipRequest(requestId, currentDriverId, reason);
|
||||
},
|
||||
{
|
||||
onSuccess: () => refetch(),
|
||||
}
|
||||
);
|
||||
|
||||
return { acceptMutation, rejectMutation };
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { useInject } from '@/lib/di/hooks/useInject';
|
||||
import { LEAGUE_MEMBERSHIP_SERVICE_TOKEN } from '@/lib/di/tokens';
|
||||
import { enhanceQueryResult } from '@/lib/di/hooks/useReactQueryWithApiError';
|
||||
|
||||
export function useLeagueMembership(leagueId: string, driverId: string) {
|
||||
const leagueMembershipService = useInject(LEAGUE_MEMBERSHIP_SERVICE_TOKEN);
|
||||
|
||||
const queryResult = useQuery({
|
||||
queryKey: ['leagueMembership', leagueId, driverId],
|
||||
queryFn: () => leagueMembershipService.getMembership(leagueId, driverId),
|
||||
enabled: !!leagueId && !!driverId,
|
||||
});
|
||||
|
||||
return enhanceQueryResult(queryResult);
|
||||
}
|
||||
@@ -1,15 +1,19 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { useServices } from '@/lib/services/ServiceProvider';
|
||||
import { useInject } from '@/lib/di/hooks/useInject';
|
||||
import { LEAGUE_SERVICE_TOKEN } from '@/lib/di/tokens';
|
||||
import { enhanceQueryResult } from '@/lib/di/hooks/useReactQueryWithApiError';
|
||||
import type { LeagueScoringPresetDTO } from '@/lib/types/generated/LeagueScoringPresetDTO';
|
||||
|
||||
export function useLeagueScoringPresets() {
|
||||
const { leagueService } = useServices();
|
||||
const leagueService = useInject(LEAGUE_SERVICE_TOKEN);
|
||||
|
||||
return useQuery({
|
||||
const queryResult = useQuery({
|
||||
queryKey: ['leagueScoringPresets'],
|
||||
queryFn: async () => {
|
||||
const result = await leagueService.getScoringPresets();
|
||||
return result as LeagueScoringPresetDTO[];
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return enhanceQueryResult(queryResult);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { useMutation } from '@tanstack/react-query';
|
||||
import { useServices } from '@/lib/services/ServiceProvider';
|
||||
import { useInject } from '@/lib/di/hooks/useInject';
|
||||
import { LEAGUE_SERVICE_TOKEN } from '@/lib/di/tokens';
|
||||
import { CreateLeagueInputDTO } from '@/lib/types/generated/CreateLeagueInputDTO';
|
||||
import { CreateLeagueOutputDTO } from '@/lib/types/generated/CreateLeagueOutputDTO';
|
||||
|
||||
@@ -61,7 +62,7 @@ export interface LeagueWizardFormModel {
|
||||
}
|
||||
|
||||
export function useCreateLeagueWizard() {
|
||||
const { leagueService } = useServices();
|
||||
const leagueService = useInject(LEAGUE_SERVICE_TOKEN);
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (params: { form: LeagueWizardFormModel; ownerId: string }): Promise<CreateLeagueOutputDTO> => {
|
||||
@@ -79,4 +80,4 @@ export function useCreateLeagueWizard() {
|
||||
return result;
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,13 +3,12 @@ import { useInject } from '@/lib/di/hooks/useInject';
|
||||
import { PENALTY_SERVICE_TOKEN } from '@/lib/di/tokens';
|
||||
import { enhanceQueryResult } from '@/lib/di/hooks/useReactQueryWithApiError';
|
||||
|
||||
export function useRacePenalties(raceId: string) {
|
||||
export function usePenaltyTypesReference() {
|
||||
const penaltyService = useInject(PENALTY_SERVICE_TOKEN);
|
||||
|
||||
const queryResult = useQuery({
|
||||
queryKey: ['racePenalties', raceId],
|
||||
queryFn: () => penaltyService.findByRaceId(raceId),
|
||||
enabled: !!raceId,
|
||||
queryKey: ['penaltyTypesReference'],
|
||||
queryFn: () => penaltyService.getPenaltyTypesReference(),
|
||||
});
|
||||
|
||||
return enhanceQueryResult(queryResult);
|
||||
Reference in New Issue
Block a user