view data fixes
This commit is contained in:
@@ -1,56 +1,19 @@
|
||||
import { RaceListItemViewModel } from './RaceListItemViewModel';
|
||||
import type { RacesPageDataRaceDTO } from '@/lib/types/generated/RacesPageDataRaceDTO';
|
||||
|
||||
// DTO matching the backend RacesPageDataDTO
|
||||
interface RacesPageDTO {
|
||||
races: RacesPageDataRaceDTO[];
|
||||
}
|
||||
import { ViewModel } from "../contracts/view-models/ViewModel";
|
||||
import { RacesPageViewData } from '../view-data/RacesPageViewData';
|
||||
|
||||
/**
|
||||
* Races page view model
|
||||
* Represents the races page data with all races in a single list
|
||||
*/
|
||||
import { ViewModel } from "../contracts/view-models/ViewModel";
|
||||
|
||||
export class RacesPageViewModel extends ViewModel {
|
||||
races: RaceListItemViewModel[];
|
||||
private readonly data: RacesPageViewData;
|
||||
readonly races: RaceListItemViewModel[];
|
||||
|
||||
constructor(dto: RacesPageDTO) {
|
||||
this.races = dto.races.map((r) => {
|
||||
const status = 'status' in r ? r.status : 'unknown';
|
||||
|
||||
const isUpcoming =
|
||||
'isUpcoming' in r ? r.isUpcoming :
|
||||
(status === 'upcoming' || status === 'scheduled');
|
||||
|
||||
const isLive =
|
||||
'isLive' in r ? r.isLive :
|
||||
(status === 'live' || status === 'running');
|
||||
|
||||
const isPast =
|
||||
'isPast' in r ? r.isPast :
|
||||
(status === 'completed' || status === 'finished' || status === 'cancelled');
|
||||
|
||||
// Build the RaceListItemDTO from the input with proper type checking
|
||||
const scheduledAt = 'scheduledAt' in r ? r.scheduledAt :
|
||||
('date' in r ? (r as { date?: string }).date : '');
|
||||
|
||||
const normalized = {
|
||||
id: r.id,
|
||||
track: 'track' in r ? r.track : '',
|
||||
car: 'car' in r ? r.car : '',
|
||||
scheduledAt: scheduledAt || '',
|
||||
status: status,
|
||||
leagueId: 'leagueId' in r ? r.leagueId : '',
|
||||
leagueName: 'leagueName' in r ? r.leagueName : '',
|
||||
strengthOfField: 'strengthOfField' in r ? (r as { strengthOfField?: number }).strengthOfField ?? null : null,
|
||||
isUpcoming: Boolean(isUpcoming),
|
||||
isLive: Boolean(isLive),
|
||||
isPast: Boolean(isPast),
|
||||
};
|
||||
|
||||
return new RaceListItemViewModel(normalized);
|
||||
});
|
||||
constructor(data: RacesPageViewData) {
|
||||
super();
|
||||
this.data = data;
|
||||
this.races = data.races.map((r) => new RaceListItemViewModel(r));
|
||||
}
|
||||
|
||||
/** UI-specific: Total races */
|
||||
@@ -87,4 +50,4 @@ export class RacesPageViewModel extends ViewModel {
|
||||
get completedRaces(): RaceListItemViewModel[] {
|
||||
return this.races.filter(r => r.status === 'completed');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user