From 460245841ba28ca280ff0be2700b80e19ea3eacf Mon Sep 17 00:00:00 2001 From: StevanFreeborn Date: Tue, 28 Jun 2022 18:49:06 -0500 Subject: [PATCH] handle a fail to load characters situation --- client/src/components/characterCard.js | 46 ++++++++++++-------------- client/src/components/loadingError.js | 30 +++++++++++++++++ client/src/pages/home.js | 25 ++++++++++++-- client/src/styles/app.css | 10 ++++++ 4 files changed, 84 insertions(+), 27 deletions(-) create mode 100644 client/src/components/loadingError.js diff --git a/client/src/components/characterCard.js b/client/src/components/characterCard.js index f78d866..b9db9a8 100644 --- a/client/src/components/characterCard.js +++ b/client/src/components/characterCard.js @@ -17,34 +17,30 @@ export default function CharacterCard(props) { {characterName} - - - - - - - - - - - - - - - - - - - - - - - +
First Episode:{firstEpisode}
Last Episode:{lastEpisode}
-
+ + + + + First Episode: + + {firstEpisode} + + + + + + Last Episode: + + {lastEpisode} + + + + -
+
diff --git a/client/src/components/loadingError.js b/client/src/components/loadingError.js new file mode 100644 index 0000000..8269537 --- /dev/null +++ b/client/src/components/loadingError.js @@ -0,0 +1,30 @@ +import React from 'react'; + +import Container from 'react-bootstrap/Container'; +import Row from 'react-bootstrap/Row'; +import Col from 'react-bootstrap/Col'; + +export default function LoadingError(props) { + return ( + + + + + + +

{props.message}

+ + + + + +
+ +
+ ); +} \ No newline at end of file diff --git a/client/src/pages/home.js b/client/src/pages/home.js index 1c46759..6b18887 100644 --- a/client/src/pages/home.js +++ b/client/src/pages/home.js @@ -4,6 +4,7 @@ import Greeting from '../components/greeting'; import CardHolder from '../components/cardHolder'; import CharacterCard from '../components/characterCard'; import Loading from '../components/loading'; +import LoadingError from '../components/loadingError'; import CharacterService from '../services/characterService'; const characterService = new CharacterService(); @@ -11,13 +12,17 @@ const characterService = new CharacterService(); export default function Home() { const [characters, setCharacters] = useState([]); + const [errorMessage, setErrorMessage] = useState(null); const getCharacters = async () => { const res = await characterService.getCharacters(); - // TODO: Handle a failed request. - if (!res.ok) return; + if (!res.ok) { + + return setErrorMessage('Failed to load characters.'); + + } const characters = await res.json(); @@ -26,7 +31,9 @@ export default function Home() { } useEffect(() => { + getCharacters(); + }, []); const getCharacterCards = () => { @@ -37,6 +44,7 @@ export default function Home() { ); @@ -45,6 +53,12 @@ export default function Home() { } + const handleTryAgain = async () => { + + if (errorMessage === 'Failed to load characters.') return await getCharacters(); + + } + return ( <> @@ -57,6 +71,13 @@ export default function Home() { + : errorMessage ? + + + : } ); diff --git a/client/src/styles/app.css b/client/src/styles/app.css index 37b08c9..eb24a20 100644 --- a/client/src/styles/app.css +++ b/client/src/styles/app.css @@ -14,4 +14,14 @@ .highlight { color: #912718; +} + +.btn-try-again, +.btn-try-again:hover, +.btn-try-again:focus, +.btn-try-again:active:focus { + color: white; + background-color: #912718; + border-color: #912718; + box-shadow: none; } \ No newline at end of file