diff --git a/src/app/api/fetchLaps.ts b/src/app/api/fetchLaps.ts new file mode 100644 index 0000000..2f2c0e3 --- /dev/null +++ b/src/app/api/fetchLaps.ts @@ -0,0 +1,68 @@ +// Based off race data +// Set session and sessions from race sessions + +import { atomEffect } from 'jotai-effect'; + +import { + EventListState, + EventState, + LapListState, + SeasonState, + serverErrorState, + SessionListState, + SessionState, +} from '@/state-mgmt/atoms'; + +import { fetchAPI } from './fetch'; + +// Fetch race results to get drivers in the session +export const fetchLapData = atomEffect((get, set) => { + // We need to see if there is an event from params + // We need to confirm eventlist loaded + const season = get(SeasonState); + + const event = get(EventState); + const eventList = get(EventListState); + const eventRound = eventList.find( + (evt) => evt.EventName === event, + )?.RoundNumber; + + const sessionName = get(SessionState); + const sessionList = get(SessionListState); + const sessionRound = sessionList.indexOf(sessionName) + 1; + + // TODO: Get driver index from driver list + // const drivers = get(DriverListState); + + let url = `laps/${season}/${eventRound}`; + + if (sessionRound) { + url += `?session=${sessionRound}`; + } + + if (season && eventRound) { + fetchAPI(url).then((res: LapData[] | ServerErrorResponse) => { + const laps = res as LapData[]; + + // Check for errors + const error = res as ServerErrorResponse; + + // *** If errors specific prop, detail, update serverErrorState + if (error.detail) { + set(serverErrorState, 'Laps Error'); + return; + } + + // *** If no errors clear serverErrorState + set(serverErrorState, ''); + + // *** Update Driver List + set(LapListState, laps); + }); + } + + // Dependencies: + // SeasonState + // EventListState + // SessionState +}); diff --git a/src/components/QueryNav/DropdownGroup.tsx b/src/components/QueryNav/DropdownGroup.tsx index 3b704c0..82b606d 100644 --- a/src/components/QueryNav/DropdownGroup.tsx +++ b/src/components/QueryNav/DropdownGroup.tsx @@ -9,6 +9,7 @@ import { driverDefault, eventDefault, sessionDefault } from '@/lib/constants'; import { fetchDriverList } from '@/app/api/fetchDriversAndSessions'; import { fetchEventList } from '@/app/api/fetchEvents'; +import { fetchLapData } from '@/app/api/fetchLaps'; import { fetchSeasonList } from '@/app/api/fetchSeasons'; // State values import { @@ -52,6 +53,7 @@ export const DropdownGroup = () => { useAtom(fetchSeasonList); useAtom(fetchEventList); useAtom(fetchDriverList); + useAtom(fetchLapData); const [season, setSeason] = useAtom(SeasonState); const [seasonList] = useAtom(SeasonListState); diff --git a/src/results.d.ts b/src/results.d.ts index ef5927d..8a1f000 100644 --- a/src/results.d.ts +++ b/src/results.d.ts @@ -84,6 +84,40 @@ interface DriverResult { Points: number; } +interface LapData { + Time: number; + Driver: string; + DriverNumber: string; + LapTime: number; + LapNumber: number; + Stint: number; + PitOutTime: number; + PitInTime: number; + Sector1Time: number; + Sector2Time: number; + Sector3Time: number; + Sector1SessionTime: number; + Sector2SessionTime: number; + Sector3SessionTime: number; + SpeedI1: number; + SpeedI2: number; + SpeedFL: number; + SpeedST: number; + IsPersonalBest: boolean; + Compound: string; + TyreLife: number; + FreshTyre: boolean; + Team: string; + LapStartTime: number; + LapStartDate: string; + TrackStatus: string; + Position: number; + Deleted: boolean; + DeletedReason: string; + FastF1Generated: boolean; + IsAccurate: boolean; +} + interface ConstructorResult { name: string; position: number; diff --git a/src/state-mgmt/atoms.ts b/src/state-mgmt/atoms.ts index 0a05519..a6af17b 100644 --- a/src/state-mgmt/atoms.ts +++ b/src/state-mgmt/atoms.ts @@ -16,7 +16,7 @@ const ConstructorListState = atom((get) => { const drivers = get(DriverListState); return formatConstructorResults(drivers) || []; }); -const LapListState = atom([]); +const LapListState = atom([]); // Customize to be derived, can have multiple driver states and multiple lap states // Possible AtomFamily https://jotai.org/docs/utilities/family