+
diff --git a/src/atoms/results.tsx b/src/atoms/results.tsx
index f05ecc8..b7677fc 100644
--- a/src/atoms/results.tsx
+++ b/src/atoms/results.tsx
@@ -1,66 +1,76 @@
import { atom } from 'jotai';
import { atomEffect } from 'jotai-effect';
-import {
- fetchAPI,
- IConstructorStandings,
- IConstructorStandingsFetch,
- ISchedule,
-} from '../app/lib/utils';
-
-//
-export const raceAtom = atom('All Races');
-export const seasonRacesAtom = atom
([]);
-export const seasonAtom = atom('2023');
-export const seasonsAtom = atom([]);
+import { fetchAPI } from '../app/lib/utils';
+
+export const raceAtom = atom('All Races');
+export const seasonRacesAtom = atom([]);
+export const seasonAtom = atom('2023');
+export const allSeasonsAtom = atom([]);
export const driverAtom = atom('All Drivers');
export const driversAtom = atom([]);
export const sessionAtom = atom('Race');
export const sessionsAtom = atom([]);
export const telemetryDisableAtom = atom(true);
export const resultUrlAtom = atom('/results');
-export const constructorStandingsAtom = atom([]);
-
-// Derived Atoms
-export const raceNamesDropdownAtom = atom((get) => {
- const data = get(seasonRacesAtom).map((race) => race.EventName);
- // Add All Races option
- data.unshift('All Races');
- return data;
-});
+export const constructorStandingsAtom = atom([]);
+export const driverStandingsAtom = atom([]);
// Effect Atoms
+// Get Seasons values, this is done clientside
+// Dependencies: seasonsAtom
export const fetchSeasons = atomEffect((get, set) => {
- if (get(seasonsAtom).length <= 0) {
- fetchAPI('seasons').then((data) => set(seasonsAtom, data));
+ // Seasons do not change, only fetch if empty array
+ if (get(allSeasonsAtom).length <= 0) {
+ fetchAPI('seasons').then((data) => set(allSeasonsAtom, data));
}
});
+// Get Races per year
+// Dependencies: seasonAtom
export const fetchRaces = atomEffect((get, set) => {
- fetchAPI('schedule/' + get(seasonAtom)).then((data) => {
+ const params = get(seasonAtom) && `?year=${get(seasonAtom)}`;
+ fetchAPI('schedule' + params).then((data) => {
set(seasonRacesAtom, data);
});
});
-export const fetchDriver = atomEffect((get, set) => {
+// Get Driver per ...season & race
+export const fetchDriver = atomEffect((_get, set) => {
fetchAPI('drivers').then((data) => set(driversAtom, data));
});
-export const fetchSessions = atomEffect((get, set) => {
+// Get sessions per ...season & race
+export const fetchSessions = atomEffect((_get, set) => {
fetchAPI('sessions').then((data) => set(sessionsAtom, data));
});
+// Get Driver & Constructor Standings
export const fetchStandings = atomEffect((get, set) => {
- fetchAPI('standings').then((data) => {
- const constructors = data.constructors.map(
- (con: IConstructorStandingsFetch) => ({
- pos: con.position,
- name: con.Constructor.name,
- points: con.points,
- wins: con.wins,
- }),
- );
- set(constructorStandingsAtom, constructors);
- });
+ const year = get(seasonAtom) && `?year=${get(seasonAtom)}`;
+ const round =
+ typeof get(raceAtom) !== 'string'
+ ? `&round=${(get(raceAtom) as ScheduleSchema).RoundNumber}`
+ : '';
+ fetchAPI('standings' + year + round).then(
+ ({
+ DriverStandings,
+ ConstructorStandings,
+ }: DataConfigSchema['standings']) => {
+ // Include Drivers in Constructors Info
+ const constructors = ConstructorStandings.map((cs) => {
+ const { name } = cs.Constructor;
+ return {
+ ...cs,
+ Drivers: DriverStandings.filter((driver) =>
+ driver.Constructors.find((c) => c.name === name),
+ ),
+ };
+ });
+
+ set(constructorStandingsAtom, constructors);
+ set(driverStandingsAtom, DriverStandings);
+ },
+ );
});
// Telemetry is disabled if no race and driver are selected
@@ -72,6 +82,9 @@ export const toggleTelemetryDisableAtom = atomEffect((get, set) => {
});
// Write only Atoms
+// aka Update other atoms
+
+// Update Values when a season changes
export const handleSeasonChangeAtom = atom(
null,
async (get, set, update: string) => {
@@ -85,22 +98,27 @@ export const handleSeasonChangeAtom = atom(
},
);
+// Update Values when a Race changes
export const handleRaceChangeAtom = atom(
null,
- async (get, set, update: string) => {
+ async (get, set, update: ScheduleSchema) => {
set(raceAtom, update);
set(driverAtom, 'All Drivers');
- set(resultUrlAtom, '/results/' + get(seasonAtom) + '/' + update);
+ set(
+ resultUrlAtom,
+ '/results/' + get(seasonAtom) + '/' + update.RoundNumber,
+ );
return get(resultUrlAtom);
},
);
+// Update Values when a Driver changes
export const handleDriverChangeAtom = atom(
null,
async (get, set, update: string) => {
set(driverAtom, update);
- set(sessionAtom, 'Race');
+ // set(sessionAtom, 'Race');
set(
resultUrlAtom,
'/results/' + get(seasonAtom) + '/' + get(raceAtom) + '/' + update,
@@ -110,6 +128,7 @@ export const handleDriverChangeAtom = atom(
},
);
+// Update Values when a session changes
export const handleSessionChangeAtom = atom(
null,
async (get, set, update: string) => {
@@ -130,6 +149,7 @@ export const handleSessionChangeAtom = atom(
},
);
-export const handleResultsAtom = atom(null, (get, set) => {
- set(seasonRacesAtom, get(seasonRacesAtom));
+// Handle click of results button in
+export const handleResultsAtom = atom(null, (_get, _set) => {
+ // set(seasonRacesAtom, get(seasonRacesAtom));
});
diff --git a/src/constants.ts b/src/constants.ts
new file mode 100644
index 0000000..69f76d3
--- /dev/null
+++ b/src/constants.ts
@@ -0,0 +1,3 @@
+const serverUrl = 'http://127.0.0.1:8081';
+
+export { serverUrl };
diff --git a/src/results.d.ts b/src/results.d.ts
new file mode 100644
index 0000000..6520355
--- /dev/null
+++ b/src/results.d.ts
@@ -0,0 +1,75 @@
+interface ScheduleSchema {
+ RoundNumber: number;
+ Country: string;
+ Location: string;
+ OfficialEventName: string;
+ EventDate: string;
+ EventName: string;
+ EventFormat: string;
+ Session1: string;
+ Session1Date: string;
+ Session1DateUtc: string;
+ Session2: string;
+ Session2Date: string;
+ Session2DateUtc: string;
+ Session3: string;
+ Session3Date: string;
+ Session3DateUtc: string;
+ Session4: string;
+ Session4Date: string;
+ Session4DateUtc: string;
+ Session5: string;
+ Session5Date: string;
+ Session5DateUtc: string;
+ F1ApiSupport: boolean;
+}
+
+// Raw Fetch Format
+interface DriverSchema {
+ driverId: string;
+ permanentNumber: string;
+ code: string;
+ url: string;
+ givenName: string;
+ familyName: string;
+ dateOfBirth: string;
+ nationality: string;
+}
+
+interface ConstructorSchema {
+ constructorId: string;
+ url: string;
+ name: string;
+ nationality: string;
+}
+
+interface StandingsSchema {
+ position: string;
+ positionText: string;
+ points: string;
+ wins: string;
+}
+
+interface DriverStandingSchema extends StandingsSchema {
+ Constructors: ConstructorSchema[];
+ Driver: DriverSchema;
+}
+
+interface ConstructorStandingSchema extends StandingsSchema {
+ Constructor: ConstructorSchema;
+ Drivers?: DriverStandingSchema[];
+}
+
+// UI format
+interface DataConfigSchema {
+ seasons: string[];
+ schedule: ScheduleSchema[];
+ drivers: string[];
+ sessions: string[];
+ standings: {
+ season: number;
+ round: number;
+ DriverStandings: DriverStandingSchema[];
+ ConstructorStandings: ConstructorStandingSchema[];
+ };
+}