Skip to content

Commit

Permalink
fix: using next template file structure
Browse files Browse the repository at this point in the history
added function to fetch server data or dummy data
  • Loading branch information
Lombardoc4 committed Jan 5, 2024
1 parent 551e4ac commit af4401a
Show file tree
Hide file tree
Showing 14 changed files with 387 additions and 176 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"dependencies": {
"clsx": "latest",
"jotai": "latest",
"jotai-effect": "^0.2.3",
"next": "latest",
"react": "latest",
"react-dom": "latest",
Expand Down
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 26 additions & 7 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,47 @@
import clsx from 'clsx';
import type { Metadata } from 'next';
import { Inter } from 'next/font/google';

import './globals.css';

import { Nav } from './ui/Nav';

const inter = Inter({ subsets: ['latin'] });

export const metadata: Metadata = {
title: 'Slick Telemetry',
description: 'Formula 1 Data Analysis',
};

export default function RootLayout({
const checkServer = async () => {
// Check if server exists
// Cannot use jotai on server component, aka RootLayout
const data = fetch('http://0.0.0.0:80', { cache: 'no-store' }).then(
(res) => {
if (!res.ok) {
return null;
}
return true;
},
() => {
return null;
},
);

return data;
};

export default async function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
const server = await checkServer();

return (
<html lang='en'>
<body className={inter.className}>
<div className='navbar bg-base-100'>
<div className='container mx-auto'>
<a className='btn btn-ghost text-xl'>Slick Telemetry</a>
</div>
</div>
<body className={clsx(inter.className, { server: server })}>
<Nav />
{children}
</body>
</html>
Expand Down
71 changes: 71 additions & 0 deletions src/app/lib/placerholder-results.tsx
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) }),
{},
),
);
69 changes: 69 additions & 0 deletions src/app/lib/utils.tsx
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;
}
};
65 changes: 54 additions & 11 deletions src/app/results/RaceResults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const ResultCard = () => (
);

const WinterTesting = () => (
<div className='card relative min-h-32 justify-center overflow-hidden rounded-2xl p-4'>
<div className='card relative min-h-32 justify-center overflow-hidden rounded-2xl p-4'>
<figure className='absolute inset-0 z-0 flex items-center justify-end bg-gradient-to-r from-base-100'>
<Image
width={928}
Expand All @@ -85,7 +85,7 @@ const WinterTesting = () => (
alt='Shoes'
/>
</figure>
<div className='relative z-0 flex items-center gap-4'>
<div className='relative z-0 flex flex-col gap-4 lg:flex-row lg:items-center'>
<div>
<h3>Winter Testing</h3>
<p>Sakhir, Bahrain</p>
Expand All @@ -97,14 +97,57 @@ const WinterTesting = () => (
</div>
);

export const RaceResults = () => (
<>
<WinterTesting />
<div className='mt-4 grid gap-4 md:grid-cols-2 2xl:grid-cols-3'>
{/* 10 Placeholder Cards */}
{Array.from(Array(10).keys()).map((item) => (
<ResultCard key={item} />
))}
// Loading animation
const shimmer =
'before:absolute before:inset-0 before:-translate-x-full before:animate-[shimmer_2s_infinite] before:bg-gradient-to-r before:from-transparent before:via-white/60 before:to-transparent';

// <div className={clsx('card relative min-h-32 justify-center overflow-hidden rounded-2xl p-4', shimmer)}>
// <figure className='absolute inset-0 z-0 flex items-center justify-end bg-gradient-to-r from-base-100'>
// <Image
// width={928}
// height={548}
// className='w-full mix-blend-overlay'
// loader={() =>
// 'https://daisyui.com/images/stock/photo-1606107557195-0e29a4b5b4aa.jpg'
// }
// src='/shoe.jpg'
// alt='Shoes'
// />
// </figure>
// <div className='relative z-0 flex flex-col gap-4 lg:flex-row lg:items-center'>
// <div>
// <h3>Winter Testing</h3>
// <p>Sakhir, Bahrain</p>
// </div>
// <a role='button' className='btn btn-sm'>
// Testing Results
// </a>
// </div>
// </div>
const WinterTestingSkeleton = () => (
<div className={`${shimmer} relative w-full overflow-hidden md:col-span-4`}>
<div className='mb-4 h-8 w-36 rounded-md bg-gray-100' />
<div className='rounded-xl bg-gray-100 p-4'>
<div className='sm:grid-cols-13 mt-0 grid h-[410px] grid-cols-12 items-end gap-2 rounded-md bg-white p-4 md:gap-4' />
<div className='flex items-center pb-2 pt-6'>
<div className='h-5 w-5 rounded-full bg-gray-200' />
<div className='ml-2 h-4 w-20 rounded-md bg-gray-200' />
</div>
</div>
</>
</div>
);

export const RaceResults = () => {
return (
<div className='px-4 lg:px-0'>
<WinterTestingSkeleton />
<WinterTesting />
<div className='mt-4 grid gap-4 md:grid-cols-2 2xl:grid-cols-3'>
{/* 10 Placeholder Cards */}
{Array.from(Array(10).keys()).map((item) => (
<ResultCard key={item} />
))}
</div>
</div>
);
};
42 changes: 21 additions & 21 deletions src/app/results/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';

import { f1Seasons } from './utils';
import { Dropdown } from '../components/Dropdown';
import { f1Seasons } from '../lib/utils';
import { Dropdown } from '../ui/Dropdown';

// Default Next Layout
export default function ResultsLayout({
Expand All @@ -17,27 +17,27 @@ export default function ResultsLayout({
);
}

const ResultsNav = () => {
const seasons = f1Seasons();
const dummyRaces = [
'All Races',
'Bahrain',
'Mexico',
'Monaco',
'Imola',
'Spain',
];
const dummyDrivers = [
'All Drivers',
'Drive 1',
'Drive 2',
'Drive 3',
'Drive 4',
'Drive 5',
];
const seasons = f1Seasons();
const dummyRaces = [
'All Races',
'Bahrain',
'Mexico',
'Monaco',
'Imola',
'Spain',
];
const dummyDrivers = [
'All Drivers',
'Drive 1',
'Drive 2',
'Drive 3',
'Drive 4',
'Drive 5',
];

const ResultsNav = () => {
return (
<div className='container navbar mx-auto my-4 rounded-box bg-base-300'>
<div className='container navbar mx-auto my-4 flex-col items-start rounded-box bg-base-300 lg:flex-row lg:items-center'>
<h2 className='p-2 text-lg font-bold'>Results:</h2>
<Dropdown value={seasons[0]} items={seasons} action={() => {}} />
<Dropdown value={dummyRaces[0]} items={dummyRaces} action={() => {}} />
Expand Down
Loading

0 comments on commit af4401a

Please sign in to comment.