-
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.
perf: rewrite home page to server side component (#12)
- Loading branch information
Showing
7 changed files
with
45 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import './Loading.css'; | ||
import './loading.css'; | ||
|
||
const Loading = () => ( | ||
<> | ||
|
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 |
---|---|---|
@@ -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(); | ||
}; |
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,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> | ||
) | ||
} |
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,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> | ||
) | ||
} |
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 |
---|---|---|
@@ -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; |
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