-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: FRON-43 Fetching lap data from backend (#42)
- Loading branch information
1 parent
faad5f1
commit 4027a82
Showing
4 changed files
with
105 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters