Skip to content

Commit

Permalink
chore: FRON-43 Fetching lap data from backend (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lombardoc4 committed Jun 10, 2024
1 parent faad5f1 commit 4027a82
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 1 deletion.
68 changes: 68 additions & 0 deletions src/app/api/fetchLaps.ts
Original file line number Diff line number Diff line change
@@ -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
});
2 changes: 2 additions & 0 deletions src/components/QueryNav/DropdownGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -52,6 +53,7 @@ export const DropdownGroup = () => {
useAtom(fetchSeasonList);
useAtom(fetchEventList);
useAtom(fetchDriverList);
useAtom(fetchLapData);

const [season, setSeason] = useAtom(SeasonState);
const [seasonList] = useAtom(SeasonListState);
Expand Down
34 changes: 34 additions & 0 deletions src/results.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/state-mgmt/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const ConstructorListState = atom<ConstructorResult[]>((get) => {
const drivers = get(DriverListState);
return formatConstructorResults(drivers) || [];
});
const LapListState = atom<string[]>([]);
const LapListState = atom<LapData[]>([]);

// Customize to be derived, can have multiple driver states and multiple lap states
// Possible AtomFamily https://jotai.org/docs/utilities/family
Expand Down

0 comments on commit 4027a82

Please sign in to comment.