diff --git a/src/App.jsx b/src/App.jsx index e315ba4..80dd842 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,9 +1,9 @@ import React from 'react'; import ReactDOM from 'react-dom/client'; -import { BrowserRouter, Routes, Route, Link } from "react-router-dom"; +import { Route, createBrowserRouter, createRoutesFromElements, RouterProvider } from "react-router-dom"; import { Home } from './pages/Home'; import { About } from './pages/About'; -import { Vans } from './pages/vans/Vans'; +import { Vans , loader as vanLoader } from './pages/vans/Vans'; import { VanDetail } from './pages/vans/VanDetail'; import { Layout } from './components/Layout'; import { HostLayout } from './components/HostLayout'; @@ -15,33 +15,41 @@ import { HostVanDetail } from './pages/host/HostVanDetail'; import { HostVanInfo } from './pages/host/HostVanInfo'; import { HostVanPrice } from './pages/host/HostVanPrice'; import { HostVanPhoto } from './pages/host/HostVanPhoto'; +import { NotFound } from './pages/NotFound'; +import { Error } from './components/Error'; -export const App =()=>{ - return( - - - }> - } /> - } /> - } /> - } /> - } > - } /> - } /> - } /> - } /> +const router = createBrowserRouter(createRoutesFromElements( + }> + } /> + } /> + } + loader={vanLoader} + errorElement={} + /> + } /> + + } > + } /> + } /> + } /> + } /> - } > - } /> - } /> - } /> - + } > + } /> + } /> + } /> + - + + } /> + +)) - - - +export const App =()=>{ + return( + ) } diff --git a/src/api.js b/src/api.js new file mode 100644 index 0000000..24df5f1 --- /dev/null +++ b/src/api.js @@ -0,0 +1,13 @@ +export async function getVans(){ + const res = await fetch('/api/vans'); + if (!res.ok) { + throw { + message: "Failed to fetch vans", + statusText: res.statusText, + status: res.status + } + } + const data = await res.json(); + return data.vans; +} + diff --git a/src/components/Error.jsx b/src/components/Error.jsx new file mode 100644 index 0000000..f3f3fbc --- /dev/null +++ b/src/components/Error.jsx @@ -0,0 +1,14 @@ +import React from "react"; +import { ReactDOM } from "react-dom/client"; +import { useRouteError } from "react-router-dom"; + +export const Error = ()=>{ + const error = useRouteError(); + console.log(error) + return ( + <> +

Error: {error.message}

+
{error.status} - {error.statusText}
+ + ) +} diff --git a/src/components/Footer.jsx b/src/components/Footer.jsx index 20a918d..3c9dea6 100644 --- a/src/components/Footer.jsx +++ b/src/components/Footer.jsx @@ -4,7 +4,7 @@ import ReactDOM from 'react-dom/client'; export const Footer = ()=>{ return( <> - + ) } diff --git a/src/components/Layout.jsx b/src/components/Layout.jsx index efdbe4c..9c14fcc 100644 --- a/src/components/Layout.jsx +++ b/src/components/Layout.jsx @@ -8,9 +8,7 @@ export const Layout = ()=>{ return ( <>
-
- -
+