Skip to content

Commit ed69bdd

Browse files
committed
improved routing
1 parent 5b7fff3 commit ed69bdd

File tree

5 files changed

+44
-18
lines changed

5 files changed

+44
-18
lines changed

Diff for: src/components/search-results/SearchPage.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export const SearchPage = ({
9797
/**
9898
* breaking up into two components so that the url only gets evaluated once
9999
*/
100-
export const SearchRoute = () => {
100+
export default function SearchRoute() {
101101
const urlValues = usePetZipQueryParams();
102102

103103
const initialValues = {
@@ -106,4 +106,4 @@ export const SearchRoute = () => {
106106
};
107107

108108
return <SearchPage initialValues={initialValues} />;
109-
};
109+
}

Diff for: src/components/single-pet/PetProfile.tsx

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import "./pet-profile.less";
22
import React, { useEffect } from "react";
33
import { Col, Row } from "antd";
44
import { ArrowLeftOutlined } from "@ant-design/icons";
5-
import { Link } from "react-router-dom";
5+
import { Link, useParams } from "react-router-dom";
66
import {
77
useDispatch,
88
useEntitiesSelector,
@@ -127,3 +127,11 @@ export const PetProfile = ({ id }: Props) => {
127127
</div>
128128
);
129129
};
130+
131+
/**
132+
* Page gets the id from the URL
133+
*/
134+
export default function ProfilePage() {
135+
const { id } = useParams<{ id: string }>();
136+
return <PetProfile id={id} />;
137+
}

Diff for: src/services/routing/AppRouter.tsx

+30-15
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
11
import { Redirect, Route, Switch } from "react-router-dom";
2-
import React from "react";
3-
import { PetProfile } from "../../components/single-pet/PetProfile";
4-
import { SearchRoute } from "../../components/search-results/SearchPage";
5-
import Dalmatian from "../../components/breed-page/BreedPage";
2+
import React, { Suspense } from "react";
3+
import { Spin } from "antd";
64
import { Home } from "../../components/home/Home";
75
import { PATHS } from "./paths";
86

7+
const PageLoading = () => (
8+
<div className="full-size center-contents">
9+
<Spin size="large" />
10+
</div>
11+
);
12+
13+
const Dalmatian = React.lazy(
14+
() => import("../../components/breed-page/BreedPage")
15+
);
16+
17+
const Search = React.lazy(
18+
() => import("../../components/search-results/SearchPage")
19+
);
20+
21+
const PetProfile = React.lazy(
22+
() => import("../../components/single-pet/PetProfile")
23+
);
24+
925
export const AppRouter = () => (
10-
<Switch>
11-
<Route
12-
path={PATHS.petProfile(":id")}
13-
render={({ match }) => <PetProfile id={match.params.id} />}
14-
/>
15-
<Route path="/breed/dalmatian/:tab" component={Dalmatian} />
16-
<Route path="/breed/dalmatian" component={Dalmatian} />
17-
<Route path="/breed" render={() => <Redirect to="/breed/dalmatian" />} />
18-
<Route path={PATHS.search()} component={SearchRoute} />
19-
<Route component={Home} />
20-
</Switch>
26+
<Suspense fallback={PageLoading}>
27+
<Switch>
28+
<Route path={PATHS.petProfile(":id")} component={PetProfile} />
29+
<Route path="/breed/dalmatian/:tab" component={Dalmatian} />
30+
<Route path="/breed/dalmatian" component={Dalmatian} />
31+
<Route path="/breed" render={() => <Redirect to="/breed/dalmatian" />} />
32+
<Route path={PATHS.search()} component={Search} />
33+
<Route component={Home} />
34+
</Switch>
35+
</Suspense>
2136
);

Diff for: vercel.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"rewrites": [{ "source": "/(.*)", "destination": "/index.html" }]
3+
}

Diff for: webpack.config.js

Whitespace-only changes.

0 commit comments

Comments
 (0)