From 56abbfa8a8e379abed8c7c10b570bcbac2bdd159 Mon Sep 17 00:00:00 2001 From: cris lombardo Date: Wed, 10 Jan 2024 13:30:10 -0500 Subject: [PATCH 1/4] refactor: utilize axios for fetch calls and include status checks to see if server is active --- package.json | 3 ++- pnpm-lock.yaml | 53 +++++++++++++++++++++++++++++++++++++---- src/app/MainFilters.tsx | 4 ++-- src/app/layout.tsx | 29 +++++++--------------- src/app/lib/utils.tsx | 41 +++++++++++++++++++------------ src/atoms/results.tsx | 30 ++++++++++++++++++----- 6 files changed, 111 insertions(+), 49 deletions(-) diff --git a/package.json b/package.json index 52e8d51..5d92989 100644 --- a/package.json +++ b/package.json @@ -21,9 +21,10 @@ "typecheck": "tsc --pretty --noEmit --incremental false" }, "dependencies": { + "axios": "^1.6.5", "clsx": "latest", "jotai": "latest", - "jotai-effect": "^0.2.3", + "jotai-effect": "latest", "next": "latest", "react": "latest", "react-dom": "latest", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2fca6aa..f9a746d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5,6 +5,9 @@ settings: excludeLinksFromLockfile: false dependencies: + axios: + specifier: ^1.6.5 + version: 1.6.5 clsx: specifier: latest version: 2.0.0 @@ -12,7 +15,7 @@ dependencies: specifier: latest version: 2.6.1(@types/react@18.2.46)(react@18.2.0) jotai-effect: - specifier: ^0.2.3 + specifier: latest version: 0.2.3(jotai@2.6.1) next: specifier: latest @@ -1152,6 +1155,10 @@ packages: has-symbols: 1.0.3 dev: true + /asynckit@0.4.0: + resolution: {integrity: sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==} + dev: false + /autoprefixer@10.4.16(postcss@8.4.32): resolution: {integrity: sha512-7vd3UC6xKp0HLfua5IjZlcXvGAGy7cBAXTg2lyQ/8WpNhd6SiZ8Be+xm3FyBSYJx5GKcpRCzBh7RH4/0dnY+uQ==} engines: {node: ^10 || ^12 || >=14} @@ -1178,6 +1185,16 @@ packages: engines: {node: '>=4'} dev: true + /axios@1.6.5: + resolution: {integrity: sha512-Ii012v05KEVuUoFWmMW/UQv9aRIc3ZwkWDcM+h5Il8izZCtRVpDUfwpoFf7eOtajT3QiGR4yDUx7lPqHJULgbg==} + dependencies: + follow-redirects: 1.15.4 + form-data: 4.0.0 + proxy-from-env: 1.1.0 + transitivePeerDependencies: + - debug + dev: false + /axobject-query@3.2.1: resolution: {integrity: sha512-jsyHu61e6N4Vbz/v18DHwWYKK0bSWLqn47eeDSKPB7m8tqMHF9YJ+mhIk2lVteyZrY8tnSj/jHOv4YiTCuCJgg==} dependencies: @@ -1498,6 +1515,13 @@ packages: resolution: {integrity: sha512-IfEDxwoWIjkeXL1eXcDiow4UbKjhLdq6/EuSVR9GMN7KVH3r9gQ83e73hsz1Nd1T3ijd5xv1wcWRYO+D6kCI2w==} dev: true + /combined-stream@1.0.8: + resolution: {integrity: sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==} + engines: {node: '>= 0.8'} + dependencies: + delayed-stream: 1.0.0 + dev: false + /commander@11.1.0: resolution: {integrity: sha512-yPVavfyCcRhmorC7rWlkHn15b4wDVgVmBA7kV4QVBsF7kv/9TKJAbAXVTxvTnwP8HHKjRCJDClKbciiYS7p0DQ==} engines: {node: '>=16'} @@ -1894,6 +1918,11 @@ packages: esprima: 4.0.1 dev: true + /delayed-stream@1.0.0: + resolution: {integrity: sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==} + engines: {node: '>=0.4.0'} + dev: false + /deprecation@2.3.1: resolution: {integrity: sha512-xmHIy4F3scKVwMsQ4WnVaS8bHOx0DmVwRywosKhaILI0ywMDWPtBSku2HNxRvF7jtwDRsoEwYQSfbxj8b7RlJQ==} dev: true @@ -2599,6 +2628,16 @@ packages: resolution: {integrity: sha512-36yxDn5H7OFZQla0/jFJmbIKTdZAQHngCedGxiMmpNfEZM0sdEeT+WczLQrjK6D7o2aiyLYDnkw0R3JK0Qv1RQ==} dev: true + /follow-redirects@1.15.4: + resolution: {integrity: sha512-Cr4D/5wlrb0z9dgERpUL3LrmPKVDsETIJhaCMeDfuFYcqa5bldGV6wBsAN6X/vxlXQtFBMrXdXxdL8CbDTGniw==} + engines: {node: '>=4.0'} + peerDependencies: + debug: '*' + peerDependenciesMeta: + debug: + optional: true + dev: false + /for-each@0.3.3: resolution: {integrity: sha512-jqYfLp7mo9vIyQf8ykW2v7A+2N4QjeCeI5+Dz9XraiO1ign81wjiH7Fb9vSOWvQfNtmSa4H2RoQTrrXivdUZmw==} dependencies: @@ -2618,6 +2657,15 @@ packages: engines: {node: '>= 14.17'} dev: true + /form-data@4.0.0: + resolution: {integrity: sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==} + engines: {node: '>= 6'} + dependencies: + asynckit: 0.4.0 + combined-stream: 1.0.8 + mime-types: 2.1.35 + dev: false + /formdata-polyfill@4.0.10: resolution: {integrity: sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==} engines: {node: '>=12.20.0'} @@ -3898,14 +3946,12 @@ packages: /mime-db@1.52.0: resolution: {integrity: sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==} engines: {node: '>= 0.6'} - dev: true /mime-types@2.1.35: resolution: {integrity: sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==} engines: {node: '>= 0.6'} dependencies: mime-db: 1.52.0 - dev: true /mimic-fn@2.1.0: resolution: {integrity: sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==} @@ -4680,7 +4726,6 @@ packages: /proxy-from-env@1.1.0: resolution: {integrity: sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==} - dev: true /punycode@2.3.1: resolution: {integrity: sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==} diff --git a/src/app/MainFilters.tsx b/src/app/MainFilters.tsx index 4c43cb6..9209624 100644 --- a/src/app/MainFilters.tsx +++ b/src/app/MainFilters.tsx @@ -7,6 +7,7 @@ import React from 'react'; import { Dropdown } from './ui/Dropdown'; import { + allSeasonsAtom, driverAtom, driversAtom, fetchDriver, @@ -22,7 +23,6 @@ import { raceNamesDropdownAtom, resultUrlAtom, seasonAtom, - seasonsAtom, sessionAtom, sessionsAtom, telemetryDisableAtom, @@ -36,7 +36,7 @@ type actionT = { const SeasonDropdown = ({ action }: actionT) => { const [season] = useAtom(seasonAtom); const [, handleSeasonChange] = useAtom(handleSeasonChangeAtom); - const [seasons] = useAtom(seasonsAtom); + const [seasons] = useAtom(allSeasonsAtom); const handleAction = (val: string) => { handleSeasonChange(val).then((url: string) => { diff --git a/src/app/layout.tsx b/src/app/layout.tsx index 943ce6a..39a22db 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -1,9 +1,11 @@ import clsx from 'clsx'; +import { Provider } from 'jotai'; import type { Metadata } from 'next'; import { Inter } from 'next/font/google'; import './globals.css'; +import { fetchAPI } from './lib/utils'; import { Nav } from './ui/Nav'; const inter = Inter({ subsets: ['latin'] }); @@ -13,40 +15,25 @@ export const metadata: Metadata = { description: 'Formula 1 Data Analysis', }; -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(); + const server = await fetchAPI('', true); return ( -