website refactor
This commit is contained in:
@@ -10,9 +10,11 @@ import { EnhancedErrorReporter } from '@/lib/infrastructure/EnhancedErrorReporte
|
||||
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
|
||||
import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
|
||||
import { notFound } from 'next/navigation';
|
||||
import type { LeagueStandingsViewModel } from '@/lib/view-models/LeagueStandingsViewModel';
|
||||
import { StandingEntryViewModel } from '@/lib/view-models/StandingEntryViewModel';
|
||||
import { DriverViewModel } from '@/lib/view-models/DriverViewModel';
|
||||
import type { LeagueMembership } from '@/lib/types/LeagueMembership';
|
||||
import type { LeagueStandingDTO } from '@/lib/types/generated/LeagueStandingDTO';
|
||||
import type { LeagueMemberDTO } from '@/lib/types/generated/LeagueMemberDTO';
|
||||
|
||||
interface Props {
|
||||
params: { id: string };
|
||||
@@ -49,45 +51,72 @@ export default async function Page({ params }: Props) {
|
||||
racesApiClient
|
||||
);
|
||||
|
||||
// Fetch data - using empty string for currentDriverId since this is SSR without session
|
||||
const result = await service.getLeagueStandings(params.id, '');
|
||||
if (!result) {
|
||||
// Fetch data
|
||||
const standingsDto = await service.getLeagueStandings(params.id);
|
||||
if (!standingsDto) {
|
||||
throw new Error('League standings not found');
|
||||
}
|
||||
return result;
|
||||
|
||||
// Get memberships for transformation
|
||||
const membershipsDto = await service.getLeagueMemberships(params.id);
|
||||
|
||||
// Transform standings to StandingEntryViewModel[]
|
||||
const standings: LeagueStandingDTO[] = standingsDto.standings || [];
|
||||
const leaderPoints = standings[0]?.points || 0;
|
||||
const standingViewModels = standings.map((entry, index) => {
|
||||
const nextPoints = standings[index + 1]?.points || entry.points;
|
||||
return new StandingEntryViewModel(entry, leaderPoints, nextPoints, '', undefined);
|
||||
});
|
||||
|
||||
// Extract unique drivers from standings and convert to DriverViewModel[]
|
||||
const driverMap = new Map<string, DriverViewModel>();
|
||||
standings.forEach(standing => {
|
||||
if (standing.driver && !driverMap.has(standing.driver.id)) {
|
||||
const driver = standing.driver;
|
||||
driverMap.set(driver.id, new DriverViewModel({
|
||||
id: driver.id,
|
||||
name: driver.name,
|
||||
avatarUrl: null, // DriverDTO doesn't have avatarUrl
|
||||
iracingId: driver.iracingId,
|
||||
rating: undefined, // DriverDTO doesn't have rating
|
||||
country: driver.country,
|
||||
}));
|
||||
}
|
||||
});
|
||||
const drivers = Array.from(driverMap.values());
|
||||
|
||||
// Transform memberships
|
||||
const memberships: LeagueMembership[] = (membershipsDto.members || []).map((m: LeagueMemberDTO) => ({
|
||||
driverId: m.driverId,
|
||||
leagueId: params.id,
|
||||
role: (m.role as LeagueMembership['role']) ?? 'member',
|
||||
joinedAt: m.joinedAt,
|
||||
status: 'active' as const,
|
||||
}));
|
||||
|
||||
return {
|
||||
standings: standingViewModels,
|
||||
drivers,
|
||||
memberships,
|
||||
};
|
||||
});
|
||||
|
||||
if (!data) {
|
||||
notFound();
|
||||
}
|
||||
|
||||
// Transform data for template
|
||||
const standings = data.standings ?? [];
|
||||
const drivers: DriverViewModel[] = data.drivers?.map((d) =>
|
||||
new DriverViewModel({
|
||||
id: d.id,
|
||||
name: d.name,
|
||||
avatarUrl: d.avatarUrl || null,
|
||||
iracingId: d.iracingId,
|
||||
rating: d.rating,
|
||||
country: d.country,
|
||||
})
|
||||
) ?? [];
|
||||
const memberships: LeagueMembership[] = data.memberships ?? [];
|
||||
|
||||
// Create a wrapper component that passes data to the template
|
||||
const TemplateWrapper = () => {
|
||||
return (
|
||||
<LeagueStandingsTemplate
|
||||
standings={standings}
|
||||
drivers={drivers}
|
||||
memberships={memberships}
|
||||
standings={data.standings}
|
||||
drivers={data.drivers}
|
||||
memberships={data.memberships}
|
||||
leagueId={params.id}
|
||||
currentDriverId={null}
|
||||
isAdmin={false}
|
||||
onRemoveMember={() => {}}
|
||||
onUpdateRole={() => {}}
|
||||
loading={false}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user