-
-
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.
fix: using next template file structure
added function to fetch server data or dummy data
- Loading branch information
1 parent
551e4ac
commit af4401a
Showing
14 changed files
with
387 additions
and
176 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { faker } from '@faker-js/faker'; | ||
|
||
export const DriverHeadings = [ | ||
'Pos.', | ||
'Driver', | ||
'Points', | ||
// Race Starts | ||
// Race Finishes | ||
// Podiums | ||
]; | ||
export const ConstuctorHeadings = [ | ||
'Pos.', | ||
'Constructor', | ||
'Points', | ||
'Drivers', | ||
// Best Result | ||
// DNFs | ||
]; | ||
|
||
const formatDriver = (key: string, i: number) => { | ||
switch (key) { | ||
case 'Pos.': | ||
return i + 1; | ||
case 'Driver': | ||
return ( | ||
<> | ||
{faker.lorem.word()} | ||
<br /> | ||
<span className='text-xs' suppressHydrationWarning={true}> | ||
{faker.lorem.word()} | ||
</span> | ||
</> | ||
); | ||
case 'Points': | ||
return faker.number.int(26); | ||
} | ||
}; | ||
const formatConstructor = (key: string, i: number) => { | ||
switch (key) { | ||
case 'Pos.': | ||
return i + 1; | ||
case 'Constructor': | ||
return faker.lorem.word(); | ||
case 'Points': | ||
return faker.number.int(51); | ||
case 'Drivers': | ||
return ( | ||
<> | ||
{faker.lorem.word()} - {faker.number.int(26)}, {faker.lorem.word()} -{' '} | ||
{faker.number.int(26)} | ||
</> | ||
); | ||
} | ||
}; | ||
|
||
export const constructorsData = Array.from({ length: 20 }, (_v, index) => | ||
ConstuctorHeadings.reduce( | ||
(obj, value) => ({ | ||
...obj, | ||
[value]: formatConstructor(value, index), | ||
}), | ||
{}, | ||
), | ||
); | ||
|
||
export const driverData = Array.from({ length: 20 }, (_v, index) => | ||
DriverHeadings.reduce( | ||
(obj, value) => ({ ...obj, [value]: formatDriver(value, index) }), | ||
{}, | ||
), | ||
); |
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,69 @@ | ||
/** | ||
* @description | ||
* Get all possible seasons/years with results | ||
* @return {*} {string[]} | ||
*/ | ||
export const f1Seasons = (): string[] => { | ||
// Discuss : Bump to fetch call to get data | ||
// ! Alter to be dynamic | ||
const testingDate = new Date('02/22/2024'); | ||
|
||
const currDate = new Date(); | ||
let currYear = currDate.getFullYear(); | ||
|
||
// Compare curr date to testing date (Feb 22 2024) | ||
// If same year as testing and before testing date | ||
// Get previous year | ||
if ( | ||
testingDate.getFullYear() === currYear && | ||
currDate.getTime() < testingDate.getTime() | ||
) { | ||
currYear -= 1; | ||
} | ||
|
||
// Fill array with values between range | ||
return Array.from({ length: currYear - 1950 + 1 }, (_v, index) => | ||
(currYear - index).toString(), | ||
); | ||
}; | ||
|
||
const dataConfig: { [key: string]: string[] } = { | ||
seasons: f1Seasons(), | ||
races: ['All Races', 'Bahrain', 'Mexico', 'Monaco', 'Imola', 'Spain'], | ||
drivers: [ | ||
'All Drivers', | ||
'Drive 1', | ||
'Drive 2', | ||
'Drive 3', | ||
'Drive 4', | ||
'Drive 5', | ||
], | ||
}; | ||
|
||
export const fetchAPI = (endpoint: string) => { | ||
const server = document.body.classList.contains('server'); | ||
const dummy: string[] | false = dataConfig[endpoint] || false; | ||
|
||
if (!server) { | ||
return dummy; | ||
} else { | ||
// Fetch from server | ||
// TODO : update to axios | ||
const data = fetch(`http://0.0.0.0:80/${endpoint}`).then( | ||
(res) => { | ||
if (!res.ok) { | ||
// console.error(`Backend responded with ${res.status} error`); | ||
return null; | ||
} | ||
return res.json(); | ||
}, | ||
() => { | ||
// console.error('Could not reach backend', error); | ||
return null; | ||
}, | ||
); | ||
// if error use dummy data | ||
|
||
return data || dataConfig; | ||
} | ||
}; |
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
Oops, something went wrong.