Skip to content

Commit

Permalink
chore: FRON-59 Update state structure to handle overlapping functiona…
Browse files Browse the repository at this point in the history
…lity
  • Loading branch information
Lombardoc4 committed May 7, 2024
1 parent 53ccc69 commit 654cff4
Show file tree
Hide file tree
Showing 11 changed files with 477 additions and 176 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Install pnpm
uses: pnpm/action-setup@a3252b78c470c02df07e9d59298aecedc3ccdd6d
with:
version: 9
version: 8
run_install: true

- name: 🔬 Lint
Expand Down
15 changes: 8 additions & 7 deletions src/app/api/fetchDriversAndSessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import { lastSession, sessionTitles } from '@/lib/helpers';
import {
DriverListState,
EventListState,
EventState,
SeasonState,
// EventState,
QueryAtom,
// SeasonState,
serverErrorState,
SessionListState,
SessionState,
// SessionState,
} from '@/state-mgmt/atoms';

import { fetchAPI } from './fetch';
Expand All @@ -23,7 +24,7 @@ import { fetchAPI } from './fetch';
export const fetchDriverList = atomEffect((get: Getter, set: Setter) => {
// We need to see if there is an event from params
// We need to confirm eventlist loaded
const eventName = get(EventState);
const eventName = get(QueryAtom).event;
const eventList = get(EventListState);

// This indicated eventList has not been fetched has not loaded
Expand All @@ -44,13 +45,13 @@ export const fetchDriverList = atomEffect((get: Getter, set: Setter) => {
// Confirm event exists
if (event) {
// *** Base url for fetch
let url = `results/${get(SeasonState)}/${event.RoundNumber}`;
let url = `results/${get(QueryAtom).season}/${event.RoundNumber}`;

// *** Get all session titles of event
const sessions = sessionTitles(event);
set(SessionListState, sessions);

let session = get(SessionState);
let session = get(QueryAtom).session;

// *** If no session update update session variable and session state
// *** Use last session from event as the default value
Expand All @@ -59,7 +60,7 @@ export const fetchDriverList = atomEffect((get: Getter, set: Setter) => {
session = lastSession(event);

// Update Session state
set(SessionState, session);
set(QueryAtom, { ...get(QueryAtom), session: session });
}

// *** If sessions available find session round and add to url
Expand Down
9 changes: 6 additions & 3 deletions src/app/api/fetchEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { atomEffect } from 'jotai-effect';

import {
EventListState,
SeasonState,
QueryAtom,
// SeasonState,
serverErrorState,
} from '@/state-mgmt/atoms';

Expand All @@ -16,9 +17,11 @@ import { fetchAPI } from './fetch';
export const fetchEventList = atomEffect(
(get: Getter, set: Setter) => {
// *** if SeasonState, set api season param
const season = get(SeasonState);
const season = get(QueryAtom).season;
const params = season && `?year=${season}`;

// console.log('fetch schedule', params);

// TODO: Prevent fetch if not connected
// ! Bug with serverConnectedState not updating
// const connected = get(serverConnectedState);
Expand Down Expand Up @@ -54,7 +57,7 @@ export const fetchEventList = atomEffect(

// *** If no season, sync default year with server provided season
if (!season) {
set(SeasonState, schedule.year);
set(QueryAtom, { ...get(QueryAtom), season: schedule.year });
}
},
);
Expand Down
29 changes: 21 additions & 8 deletions src/app/api/fetchLaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import { Getter, Setter } from 'jotai';
import { atomEffect } from 'jotai-effect';

import {
// DriverAtom,
DriverListState,
EventListState,
EventState,
// EventState,
LapListState,
SeasonState,
QueryAtom,
// SeasonState,
serverErrorState,
SessionListState,
SessionState,
// SessionState,
} from '@/state-mgmt/atoms';

import { fetchAPI } from './fetch';
Expand All @@ -20,26 +23,36 @@ import { fetchAPI } from './fetch';
export const fetchLapData = atomEffect((get: Getter, set: Setter) => {
// We need to see if there is an event from params
// We need to confirm eventlist loaded
const season = get(SeasonState);
const { season, event, session: sessionName, driver } = get(QueryAtom);
// const season = get(SeasonState);

const event = get(EventState);
// const event = get(EventState);
const eventList = get(EventListState);
const eventRound = eventList.find(
(evt) => evt.EventName === event,
)?.RoundNumber;

const sessionName = get(SessionState);
// 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);
// Get driver number from driver list
const driverList = get(DriverListState);
const driverNumber = driverList.find(
(d) => d.DriverId === driver,
)?.DriverNumber;

let url = `laps/${season}/${eventRound}`;

if (sessionRound) {
url += `?session=${sessionRound}`;
}
if (driverNumber) {
url += `${sessionRound ? '&' : '?'}driver_number=${driverNumber}`;
} else {
// To prevent fetching too much data we require driver number
return;
}

if (season && eventRound) {
fetchAPI(url).then((res: LapData[] | ServerErrorResponse) => {
Expand Down
10 changes: 6 additions & 4 deletions src/app/api/fetchStandings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ import {
ConstructorStandingState,
DriverStandingState,
EventListState,
EventState,
SeasonState,
QueryAtom,
// EventState,
// SeasonState,
serverErrorState,
} from '@/state-mgmt/atoms';

import { fetchAPI } from './fetch';

// Get Driver & Constructor Standings
export const fetchStandings = atomEffect((get: Getter, set: Setter) => {
const season = get(SeasonState);
const season = get(QueryAtom).season;
const eventAtom = get(QueryAtom).event;
const race = get(EventListState).find(
(event) => event.EventName === get(EventState),
(event) => event.EventName === eventAtom,
);

// Year
Expand Down
Loading

0 comments on commit 654cff4

Please sign in to comment.