website refactor
This commit is contained in:
@@ -1,63 +1,36 @@
|
||||
import { LeagueRulebookTemplate } from '@/templates/LeagueRulebookTemplate';
|
||||
import { PageWrapper } from '@/components/shared/state/PageWrapper';
|
||||
import { PageDataFetcher } from '@/lib/page/PageDataFetcher';
|
||||
import { LeagueService } from '@/lib/services/leagues/LeagueService';
|
||||
import { LeaguesApiClient } from '@/lib/api/leagues/LeaguesApiClient';
|
||||
import { DriversApiClient } from '@/lib/api/drivers/DriversApiClient';
|
||||
import { SponsorsApiClient } from '@/lib/api/sponsors/SponsorsApiClient';
|
||||
import { RacesApiClient } from '@/lib/api/races/RacesApiClient';
|
||||
import { EnhancedErrorReporter } from '@/lib/infrastructure/EnhancedErrorReporter';
|
||||
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
|
||||
import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
|
||||
import { LeagueRulebookPageQuery } from '@/lib/page-queries/page-queries/LeagueRulebookPageQuery';
|
||||
import { RulebookTemplate } from '@/templates/RulebookTemplate';
|
||||
import { notFound } from 'next/navigation';
|
||||
import type { LeagueDetailPageViewModel } from '@/lib/view-models/LeagueDetailPageViewModel';
|
||||
|
||||
interface Props {
|
||||
params: { id: string };
|
||||
}
|
||||
|
||||
export default async function Page({ params }: Props) {
|
||||
// Validate params
|
||||
if (!params.id) {
|
||||
const leagueId = params.id;
|
||||
|
||||
if (!leagueId) {
|
||||
notFound();
|
||||
}
|
||||
|
||||
// Fetch data using PageDataFetcher.fetchManual
|
||||
const data = await PageDataFetcher.fetchManual(async () => {
|
||||
// Create dependencies for API clients
|
||||
const baseUrl = getWebsiteApiBaseUrl();
|
||||
const logger = new ConsoleLogger();
|
||||
const errorReporter = new EnhancedErrorReporter(logger, {
|
||||
showUserNotifications: true,
|
||||
logToConsole: true,
|
||||
reportToExternal: process.env.NODE_ENV === 'production',
|
||||
});
|
||||
const result = await LeagueRulebookPageQuery.execute(leagueId);
|
||||
|
||||
// Create API clients
|
||||
const leaguesApiClient = new LeaguesApiClient(baseUrl, errorReporter, logger);
|
||||
const driversApiClient = new DriversApiClient(baseUrl, errorReporter, logger);
|
||||
const sponsorsApiClient = new SponsorsApiClient(baseUrl, errorReporter, logger);
|
||||
const racesApiClient = new RacesApiClient(baseUrl, errorReporter, logger);
|
||||
|
||||
// Create service
|
||||
const service = new LeagueService(
|
||||
leaguesApiClient,
|
||||
driversApiClient,
|
||||
sponsorsApiClient,
|
||||
racesApiClient
|
||||
);
|
||||
|
||||
return await service.getLeagueDetailPageData(params.id);
|
||||
});
|
||||
|
||||
if (!data) {
|
||||
notFound();
|
||||
if (result.isErr()) {
|
||||
const error = result.getError();
|
||||
if (error.type === 'notFound') {
|
||||
notFound();
|
||||
}
|
||||
// For serverError, show the template with empty data
|
||||
return <RulebookTemplate viewData={{
|
||||
leagueId,
|
||||
scoringConfig: {
|
||||
gameName: 'Unknown',
|
||||
scoringPresetName: 'Unknown',
|
||||
championships: [],
|
||||
dropPolicySummary: 'Unknown',
|
||||
},
|
||||
}} />;
|
||||
}
|
||||
|
||||
// Create a Template wrapper that matches PageWrapper's expected interface
|
||||
const Template = ({ data }: { data: LeagueDetailPageViewModel }) => {
|
||||
return <LeagueRulebookTemplate viewModel={data} loading={false} />;
|
||||
};
|
||||
|
||||
return <PageWrapper data={data} Template={Template} />;
|
||||
return <RulebookTemplate viewData={result.unwrap()} />;
|
||||
}
|
||||
@@ -1,142 +1,43 @@
|
||||
import { PageWrapper } from '@/components/shared/state/PageWrapper';
|
||||
import { LeagueStandingsPageQuery } from '@/lib/page-queries/page-queries/LeagueStandingsPageQuery';
|
||||
import { LeagueStandingsTemplate } from '@/templates/LeagueStandingsTemplate';
|
||||
import { PageDataFetcher } from '@/lib/page/PageDataFetcher';
|
||||
import { LeagueService } from '@/lib/services/leagues/LeagueService';
|
||||
import { LeaguesApiClient } from '@/lib/api/leagues/LeaguesApiClient';
|
||||
import { DriversApiClient } from '@/lib/api/drivers/DriversApiClient';
|
||||
import { SponsorsApiClient } from '@/lib/api/sponsors/SponsorsApiClient';
|
||||
import { RacesApiClient } from '@/lib/api/races/RacesApiClient';
|
||||
import { EnhancedErrorReporter } from '@/lib/infrastructure/EnhancedErrorReporter';
|
||||
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
|
||||
import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
|
||||
import { notFound } from 'next/navigation';
|
||||
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';
|
||||
import { LeagueStandingsPresenter } from '@/lib/presenters/LeagueStandingsPresenter';
|
||||
|
||||
interface Props {
|
||||
params: { id: string };
|
||||
}
|
||||
|
||||
export default async function Page({ params }: Props) {
|
||||
// Validate params
|
||||
if (!params.id) {
|
||||
const leagueId = params.id;
|
||||
|
||||
if (!leagueId) {
|
||||
notFound();
|
||||
}
|
||||
|
||||
// Fetch data using PageDataFetcher.fetchManual for multiple dependencies
|
||||
const data = await PageDataFetcher.fetchManual(async () => {
|
||||
// Create dependencies
|
||||
const baseUrl = getWebsiteApiBaseUrl();
|
||||
const logger = new ConsoleLogger();
|
||||
const errorReporter = new EnhancedErrorReporter(logger, {
|
||||
showUserNotifications: true,
|
||||
logToConsole: true,
|
||||
reportToExternal: process.env.NODE_ENV === 'production',
|
||||
});
|
||||
const result = await LeagueStandingsPageQuery.execute(leagueId);
|
||||
|
||||
// Create API clients
|
||||
const leaguesApiClient = new LeaguesApiClient(baseUrl, errorReporter, logger);
|
||||
const driversApiClient = new DriversApiClient(baseUrl, errorReporter, logger);
|
||||
const sponsorsApiClient = new SponsorsApiClient(baseUrl, errorReporter, logger);
|
||||
const racesApiClient = new RacesApiClient(baseUrl, errorReporter, logger);
|
||||
|
||||
// Create service
|
||||
const service = new LeagueService(
|
||||
leaguesApiClient,
|
||||
driversApiClient,
|
||||
sponsorsApiClient,
|
||||
racesApiClient
|
||||
);
|
||||
|
||||
// Fetch data
|
||||
const standingsDto = await service.getLeagueStandings(params.id);
|
||||
if (!standingsDto) {
|
||||
throw new Error('League standings not found');
|
||||
if (result.isErr()) {
|
||||
const error = result.getError();
|
||||
if (error.type === 'notFound') {
|
||||
notFound();
|
||||
}
|
||||
|
||||
// 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();
|
||||
// For serverError, show the template with empty data
|
||||
return <LeagueStandingsTemplate
|
||||
viewData={{
|
||||
standings: [],
|
||||
drivers: [],
|
||||
memberships: [],
|
||||
leagueId,
|
||||
currentDriverId: null,
|
||||
isAdmin: false,
|
||||
}}
|
||||
onRemoveMember={() => {}}
|
||||
onUpdateRole={() => {}}
|
||||
/>;
|
||||
}
|
||||
|
||||
// Create a wrapper component that passes ViewData to the template
|
||||
const TemplateWrapper = () => {
|
||||
// Convert ViewModels to ViewData using Presenter
|
||||
const viewData = LeagueStandingsPresenter.createViewData(
|
||||
data.standings,
|
||||
data.drivers,
|
||||
data.memberships,
|
||||
params.id,
|
||||
null, // currentDriverId
|
||||
false // isAdmin
|
||||
);
|
||||
|
||||
return (
|
||||
<LeagueStandingsTemplate
|
||||
viewData={viewData}
|
||||
onRemoveMember={() => {}}
|
||||
onUpdateRole={() => {}}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<PageWrapper
|
||||
data={data}
|
||||
Template={TemplateWrapper}
|
||||
loading={{ variant: 'skeleton', message: 'Loading standings...' }}
|
||||
errorConfig={{ variant: 'full-screen' }}
|
||||
empty={{
|
||||
title: 'Standings not found',
|
||||
description: 'The standings for this league are not available.',
|
||||
}}
|
||||
/>
|
||||
);
|
||||
return <LeagueStandingsTemplate
|
||||
viewData={result.unwrap()}
|
||||
onRemoveMember={() => {}}
|
||||
onUpdateRole={() => {}}
|
||||
/>;
|
||||
}
|
||||
@@ -1,71 +1,35 @@
|
||||
'use client';
|
||||
import { LeagueStewardingPageQuery } from '@/lib/page-queries/page-queries/LeagueStewardingPageQuery';
|
||||
import { StewardingTemplate } from '@/templates/StewardingTemplate';
|
||||
import { notFound } from 'next/navigation';
|
||||
|
||||
import { useCurrentDriver } from "@/lib/hooks/driver/useCurrentDriver";
|
||||
import { useLeagueAdminStatus } from "@/lib/hooks/league/useLeagueAdminStatus";
|
||||
import { useLeagueStewardingData } from "@/lib/hooks/league/useLeagueStewardingData";
|
||||
import { useLeagueStewardingMutations } from "@/lib/hooks/league/useLeagueStewardingMutations";
|
||||
import { StatefulPageWrapper } from '@/components/shared/state/StatefulPageWrapper';
|
||||
import { StewardingTemplate } from './StewardingTemplate';
|
||||
import { LoadingWrapper } from '@/components/shared/state/LoadingWrapper';
|
||||
import Card from '@/components/ui/Card';
|
||||
import { AlertTriangle } from 'lucide-react';
|
||||
import { useParams } from 'next/navigation';
|
||||
interface Props {
|
||||
params: { id: string };
|
||||
}
|
||||
|
||||
export default function LeagueStewardingPage() {
|
||||
const params = useParams();
|
||||
const leagueId = params.id as string;
|
||||
const { data: currentDriver } = useCurrentDriver();
|
||||
const currentDriverId = currentDriver?.id || '';
|
||||
export default async function LeagueStewardingPage({ params }: Props) {
|
||||
const leagueId = params.id;
|
||||
|
||||
// Check admin status
|
||||
const { data: isAdmin, isLoading: adminLoading } = useLeagueAdminStatus(leagueId, currentDriverId);
|
||||
|
||||
// Show loading for admin check
|
||||
if (adminLoading) {
|
||||
return <LoadingWrapper variant="full-screen" message="Checking permissions..." />;
|
||||
if (!leagueId) {
|
||||
notFound();
|
||||
}
|
||||
|
||||
// Show access denied if not admin
|
||||
if (!isAdmin) {
|
||||
return (
|
||||
<Card>
|
||||
<div className="text-center py-12">
|
||||
<div className="w-16 h-16 mx-auto mb-4 rounded-full bg-iron-gray/50 flex items-center justify-center">
|
||||
<AlertTriangle className="w-8 h-8 text-warning-amber" />
|
||||
</div>
|
||||
<h3 className="text-lg font-medium text-white mb-2">Admin Access Required</h3>
|
||||
<p className="text-sm text-gray-400">
|
||||
Only league admins can access stewarding functions.
|
||||
</p>
|
||||
</div>
|
||||
</Card>
|
||||
);
|
||||
const result = await LeagueStewardingPageQuery.execute(leagueId);
|
||||
|
||||
if (result.isErr()) {
|
||||
const error = result.getError();
|
||||
if (error.type === 'notFound') {
|
||||
notFound();
|
||||
}
|
||||
// For serverError, show the template with empty data
|
||||
return <StewardingTemplate viewData={{
|
||||
leagueId,
|
||||
totalPending: 0,
|
||||
totalResolved: 0,
|
||||
totalPenalties: 0,
|
||||
races: [],
|
||||
drivers: []
|
||||
}} />;
|
||||
}
|
||||
|
||||
// Load stewarding data using domain hook
|
||||
const { data, isLoading, error, refetch } = useLeagueStewardingData(leagueId);
|
||||
|
||||
return (
|
||||
<StatefulPageWrapper
|
||||
data={data}
|
||||
isLoading={isLoading}
|
||||
error={error}
|
||||
retry={refetch}
|
||||
Template={({ data }) => (
|
||||
<StewardingTemplate
|
||||
data={data}
|
||||
leagueId={leagueId}
|
||||
currentDriverId={currentDriverId}
|
||||
onRefetch={refetch}
|
||||
/>
|
||||
)}
|
||||
loading={{ variant: 'skeleton', message: 'Loading stewarding data...' }}
|
||||
errorConfig={{ variant: 'full-screen' }}
|
||||
empty={{
|
||||
icon: require('lucide-react').Flag,
|
||||
title: 'No stewarding data',
|
||||
description: 'There are no protests or penalties to review.',
|
||||
}}
|
||||
/>
|
||||
);
|
||||
return <StewardingTemplate viewData={result.unwrap()} />;
|
||||
}
|
||||
Reference in New Issue
Block a user