error boundary
This commit is contained in:
parent
bae7e17444
commit
c70b0e7236
21
src/components/error-boundary.tsx
Normal file
21
src/components/error-boundary.tsx
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import React from 'react'
|
||||||
|
|
||||||
|
export class ErrorBoundary extends React.Component {
|
||||||
|
state = {
|
||||||
|
error: false
|
||||||
|
}
|
||||||
|
|
||||||
|
static getDerivedStateFromError() {
|
||||||
|
return {
|
||||||
|
error: true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
if (this.state.error) {
|
||||||
|
return <h1>Something went wrong.</h1>
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.props.children
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
|
|
||||||
import data from '../__stubs__/landing-data.json'
|
import data from "../__stubs__/landing-data.json";
|
||||||
import { NavPanel } from "../components/nav-panel";
|
import { NavPanel } from "../components/nav-panel";
|
||||||
import logo from "../assets/logo_1x.png";
|
import logo from "../assets/logo_1x.png";
|
||||||
import cucumber from "../assets/cucumber.png";
|
import cucumber from "../assets/cucumber.png";
|
||||||
@ -21,10 +21,12 @@ import {
|
|||||||
PageFooter,
|
PageFooter,
|
||||||
PageHeader,
|
PageHeader,
|
||||||
} from "./style";
|
} from "./style";
|
||||||
|
import { ErrorBoundary } from "../components/error-boundary";
|
||||||
|
|
||||||
export const LandingPage = () => {
|
export const LandingPage = () => {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
<ErrorBoundary>
|
||||||
<PageHeader>
|
<PageHeader>
|
||||||
<Logo
|
<Logo
|
||||||
height="44px"
|
height="44px"
|
||||||
@ -42,9 +44,9 @@ export const LandingPage = () => {
|
|||||||
(min-width: 520px) 880px
|
(min-width: 520px) 880px
|
||||||
"
|
"
|
||||||
/>
|
/>
|
||||||
<NavPanel currentNavElement={'Home'} />
|
<NavPanel currentNavElement={"Home"} />
|
||||||
</PageHeader>
|
</PageHeader>
|
||||||
|
</ErrorBoundary>
|
||||||
<Main>
|
<Main>
|
||||||
<MainCardWrapper as="header">
|
<MainCardWrapper as="header">
|
||||||
<MainCardText>
|
<MainCardText>
|
||||||
@ -64,7 +66,16 @@ export const LandingPage = () => {
|
|||||||
</MainCardText>
|
</MainCardText>
|
||||||
<MainCardImg src={cucumber} alt="" />
|
<MainCardImg src={cucumber} alt="" />
|
||||||
</MainCardWrapper>
|
</MainCardWrapper>
|
||||||
{data.map((item, index) => (
|
<ErrorBoundary><Cards /></ErrorBoundary>
|
||||||
|
</Main>
|
||||||
|
|
||||||
|
<PageFooter></PageFooter>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const Cards = () =>
|
||||||
|
data.map((item, index) => (
|
||||||
<Card
|
<Card
|
||||||
directionReverse={index % 2 === 0}
|
directionReverse={index % 2 === 0}
|
||||||
key={item.id}
|
key={item.id}
|
||||||
@ -75,10 +86,4 @@ export const LandingPage = () => {
|
|||||||
>
|
>
|
||||||
{item.body}
|
{item.body}
|
||||||
</Card>
|
</Card>
|
||||||
))}
|
));
|
||||||
</Main>
|
|
||||||
|
|
||||||
<PageFooter></PageFooter>
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
@ -6,6 +6,9 @@ import logo from "../assets/logo_1x.png";
|
|||||||
import logo2x from "../assets/logo_2x.png";
|
import logo2x from "../assets/logo_2x.png";
|
||||||
import logo4x from "../assets/logo_4x.png";
|
import logo4x from "../assets/logo_4x.png";
|
||||||
import { NavPanel } from "../components/nav-panel";
|
import { NavPanel } from "../components/nav-panel";
|
||||||
|
import { Header1 } from "../components/common";
|
||||||
|
import { InputField } from "../components/field/field";
|
||||||
|
import { URLs } from "../__data__/urls";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
BrandText,
|
BrandText,
|
||||||
@ -19,9 +22,6 @@ import {
|
|||||||
SearchButton,
|
SearchButton,
|
||||||
SearchForm,
|
SearchForm,
|
||||||
} from "./style";
|
} from "./style";
|
||||||
import { Header1 } from "../components/common";
|
|
||||||
import { InputField } from "../components/field/field";
|
|
||||||
import { URLs } from "../__data__/urls";
|
|
||||||
|
|
||||||
export const SearchCharacterPage = () => {
|
export const SearchCharacterPage = () => {
|
||||||
return (
|
return (
|
||||||
|
@ -1,16 +1,21 @@
|
|||||||
import React from 'react';
|
import React from "react";
|
||||||
import { Routes, Route } from'react-router-dom';
|
import { Routes, Route } from "react-router-dom";
|
||||||
|
|
||||||
import { URLs } from './__data__/urls';
|
import { URLs } from "./__data__/urls";
|
||||||
import { LandingPage } from './pages/landing';
|
import { LandingPage } from "./pages/landing";
|
||||||
import { SearchCharacterPage } from './pages/search-character';
|
import { SearchCharacterPage } from "./pages/search-character";
|
||||||
|
import { ErrorBoundary } from "./components/error-boundary";
|
||||||
|
|
||||||
export const PageRoutes = () => (
|
export const PageRoutes = () => (
|
||||||
|
<ErrorBoundary>
|
||||||
<Routes>
|
<Routes>
|
||||||
<Route path={URLs.ui.charDetail.url} element={<LandingPage />} />
|
<Route path={URLs.ui.charDetail.url} element={<LandingPage />} />
|
||||||
<Route path={URLs.baseUrl} element={<LandingPage />} />
|
<Route path={URLs.baseUrl} element={<LandingPage />} />
|
||||||
{URLs.ui.search && <Route path={URLs.ui.search} element={<SearchCharacterPage />} />}
|
{URLs.ui.search && (
|
||||||
|
<Route path={URLs.ui.search} element={<SearchCharacterPage />} />
|
||||||
|
)}
|
||||||
|
|
||||||
<Route path="*" element={<h1>Page not found</h1>} />
|
<Route path="*" element={<h1>Page not found</h1>} />
|
||||||
</Routes>
|
</Routes>
|
||||||
)
|
</ErrorBoundary>
|
||||||
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user