Skip to content

Commit

Permalink
perf: rewrite home page to server side component (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
pure-js authored Jul 14, 2023
1 parent f6989c3 commit e2c5498
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 36 deletions.
2 changes: 1 addition & 1 deletion app/Loading.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import './Loading.css';
import './loading.css';

const Loading = () => (
<>
Expand Down
10 changes: 7 additions & 3 deletions app/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
export const fetchManufacturers = () => {
export async function getManufacturers(page = 1) {
const domain = 'https://vpic.nhtsa.dot.gov';
const endpoint = 'getallmanufacturers';
const args = 'format=json&page=1';
const args = `format=json&page=${page}`;
const url = `${domain}/api/vehicles/${endpoint}?${args}`;
return fetch(url).then(data => data.json());
const res = await fetch(url);
if (!res.ok) {
throw new Error('Failed to fetch data')
}
return res.json();
};
13 changes: 1 addition & 12 deletions app/components/Table.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Link from 'next/link';
import { ArrowTopRightOnSquareIcon } from '@heroicons/react/24/outline';

import Loading from '../Loading';
import { getFlagByCountryName } from '../helpers/getEmojiFlagByISO'
// import { toLamaCase } from './toLamaCase';

Expand All @@ -10,11 +9,9 @@ import './Helpers.css';

interface TableProps {
manufacturers: Array<any> | null;
isLoading: boolean;
isError: boolean;
}

const Table = ({ manufacturers, isLoading, isError }: TableProps) => (
const Table = ({ manufacturers }: TableProps) => (
<table className="table">
<thead>
<tr>
Expand All @@ -37,14 +34,6 @@ const Table = ({ manufacturers, isLoading, isError }: TableProps) => (
</td>
</tr>
)) }
{ isLoading || isError && (
<tr>
<td colSpan={3}>
{ isLoading && (<Loading />) }
{ isError && (<h3>Error!</h3>) }
</td>
</tr>
) }
</tbody>
</table>
);
Expand Down
16 changes: 16 additions & 0 deletions app/error.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use client'

export default function Error({
error,
reset,
}: {
error: Error
reset: () => void
}) {
return (
<div>
<h2>Something went wrong!</h2>
<button onClick={() => reset()}>Try again</button>
</div>
)
}
13 changes: 13 additions & 0 deletions app/not-found.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Link from 'next/link'

export default function NotFound() {
return (
<div>
<h2>Not Found</h2>
<p>Could not find requested resource</p>
<p>
View <Link href="/">main page</Link>
</p>
</div>
)
}
25 changes: 6 additions & 19 deletions app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,18 @@
"use client";
import { useEffect, useState } from 'react';
// import { useEffect, useState } from 'react';

import Table from './components/Table';
import { fetchManufacturers } from './api';
import { getManufacturers } from './api';

import './components/Grid.css';

const Home = () => {
const [isLoading, setIsLoading]= useState(true);
const [isError, setIsError]= useState(false);
const [manufacturers, setManufacturers]= useState(null);
useEffect(() => {
// setIsLoading(true);
fetchManufacturers().then(data => {
console.log(data);
setManufacturers(data.Results);
setIsLoading(false);
}).catch((error) => {
setIsError(true);
})
}, []);
const HomePage = async () => {
const manufacturers = await getManufacturers().then(data => data.Results);

return (
<main className="container">
<Table manufacturers={manufacturers} isLoading={isLoading} isError={isError} />
<Table manufacturers={manufacturers} />
</main>
);
}

export default Home;
export default HomePage;
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
]
},
"include": [
"src",
"app/page.tsx",
"app/manufacturers/[id]/page.tsx",
"app/components",
"app/helpers",
"app/api.ts",
"app/**/*.tsx",
".next/types/**/*.ts"
],
"references": [
Expand Down

0 comments on commit e2c5498

Please sign in to comment.