From ace3d0bd7ed75e882c8fd7e6e06788caef758f18 Mon Sep 17 00:00:00 2001 From: StevanFreeborn Date: Tue, 19 Jul 2022 12:38:58 -0500 Subject: [PATCH] more work to add multiple datasets to home page --- client/src/components/characterCard.js | 2 +- client/src/components/dataButton.js | 7 +- client/src/components/quoteCard.js | 21 ++++++ client/src/pages/home.js | 99 +++++++++++++++++++------- client/src/services/quoteService.js | 19 +++++ 5 files changed, 120 insertions(+), 28 deletions(-) create mode 100644 client/src/components/quoteCard.js create mode 100644 client/src/services/quoteService.js diff --git a/client/src/components/characterCard.js b/client/src/components/characterCard.js index b9db9a8..9f49881 100644 --- a/client/src/components/characterCard.js +++ b/client/src/components/characterCard.js @@ -3,7 +3,7 @@ import Card from 'react-bootstrap/Card'; import Table from 'react-bootstrap/Table'; export default function CharacterCard(props) { - + const image = new URL(props.character.image).pathname; const characterName = props.character.fullName; const firstEpisode = props.character.firstEpisode; diff --git a/client/src/components/dataButton.js b/client/src/components/dataButton.js index 0653a2a..135ef8c 100644 --- a/client/src/components/dataButton.js +++ b/client/src/components/dataButton.js @@ -2,8 +2,13 @@ import React from 'react'; export default function DataButton(props) { + const label = props.label; + return ( - ); diff --git a/client/src/components/quoteCard.js b/client/src/components/quoteCard.js new file mode 100644 index 0000000..d6c2dd0 --- /dev/null +++ b/client/src/components/quoteCard.js @@ -0,0 +1,21 @@ +import React from 'react'; +import Card from 'react-bootstrap/Card'; + +export default function QuoteCard(props) { + + const text = props.quote.text; + const source = props.quote.source + + return ( + + + + + {text} + {source} + + + + + ); +} \ No newline at end of file diff --git a/client/src/pages/home.js b/client/src/pages/home.js index 9a2b8e3..4a51475 100644 --- a/client/src/pages/home.js +++ b/client/src/pages/home.js @@ -5,48 +5,81 @@ 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'; import DataButtonHolder from '../components/dataButtonHolder'; import DataButton from '../components/dataButton'; + +import CharacterService from '../services/characterService'; +import QuoteService from '../services/quoteService'; +import QuoteCard from '../components/quoteCard'; const characterService = new CharacterService(); +const quoteService = new QuoteService(); export default function Home() { - const models = [{name: 'Characters'}, {name: 'Quotes'}, {name: 'Episodes'}, {name: 'Seasons'}]; - - const [characters, setCharacters] = useState([]); + const [currentData, setCurrentData] = useState('characters'); + const [data, setData] = useState([]); const [errorMessage, setErrorMessage] = useState(null); const getCharacters = async () => { + setData([]); + setCurrentData('characters'); + const res = await characterService.getCharacters(); - + if (!res.ok) { - return setErrorMessage('Failed to load characters.'); + return setErrorMessage(`Failed to load characters`); } const characters = await res.json(); - return setCharacters(characters); - + return setData(characters); } + const getQuotes = async () => { + + setData([]); + setCurrentData('quotes'); + + const res = await quoteService.getQuotes(); + + if (!res.ok) { + + return setErrorMessage(`Failed to load quotes`); + + } + + const quotes = await res.json(); + + return setData(quotes.slice(0,20)); + } + + const getEpisodes = () => {console.log('episodes')} + + const getSeasons = () => {console.log('seasons')} + useEffect(() => { getCharacters(); }, []); + const models = [{ id: 1, name: 'Characters', handleClick: getCharacters}, + { id: 2, name: 'Quotes', handleClick: getQuotes }, + { id: 3, name: 'Episodes', handleClick: getEpisodes }, + { id: 4, name: 'Seasons', handleClick: getSeasons }]; + const getDataButtons = () => { return models.map(model => { - + return ( - ); @@ -54,13 +87,29 @@ export default function Home() { } - const getCharacterCards = () => { + const getCards = () => { - return characters.map(character => { + if (currentData === 'quotes') { + + return data.map(quote => { + + return ( + + + ); + + }); + + } + + return data.map(character => { return ( - @@ -75,8 +124,6 @@ export default function Home() { setErrorMessage(null); - if (errorMessage === 'Failed to load characters.') return await getCharacters(); - } return ( @@ -87,22 +134,22 @@ export default function Home() { {getDataButtons()} - {characters.length > 0 ? + {data.length > 0 ? - {getCharacterCards()} + {getCards()} - - : errorMessage ? - - - : } + : errorMessage ? + + + + : } ); } \ No newline at end of file diff --git a/client/src/services/quoteService.js b/client/src/services/quoteService.js new file mode 100644 index 0000000..0af882c --- /dev/null +++ b/client/src/services/quoteService.js @@ -0,0 +1,19 @@ +import { baseUrl, versionHeader, versionHeaderValue } from '../constants'; + +class QuoteService { + + getQuotes = async () => { + + const res = await fetch(`${baseUrl}/api/quotes`, { + headers: { + [versionHeader]: versionHeaderValue + } + }); + + return res; + + } + +} + +export default QuoteService; \ No newline at end of file