Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2c72ffc613 | ||
|
|
ace3d0bd7e | ||
|
|
85025970ba | ||
|
|
2685e66d17 |
@@ -0,0 +1,16 @@
|
||||
import React from 'react';
|
||||
|
||||
export default function DataButton(props) {
|
||||
|
||||
const label = props.label;
|
||||
|
||||
return (
|
||||
<button
|
||||
value={label.toLowerCase()}
|
||||
onClick={(e) => props.handleClick(e.target.value)}
|
||||
>
|
||||
{props.label}
|
||||
</button>
|
||||
);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import React from 'react';
|
||||
import Container from 'react-bootstrap/Container';
|
||||
import Row from 'react-bootstrap/Row';
|
||||
|
||||
export default function DataButtonHolder(props) {
|
||||
return(
|
||||
<Container fluid>
|
||||
|
||||
<Row className="d-flex flex-row justify-content-center align-items-center">
|
||||
|
||||
{props.children}
|
||||
|
||||
</Row>
|
||||
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
@@ -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 (
|
||||
<Card style={{ width: '22rem' }} className='p-1 m-3'>
|
||||
|
||||
<Card.Body>
|
||||
|
||||
<Card.Text>{text}</Card.Text>
|
||||
<Card.Text>{source}</Card.Text>
|
||||
|
||||
</Card.Body>
|
||||
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
@@ -5,40 +5,107 @@ import CardHolder from '../components/cardHolder';
|
||||
import CharacterCard from '../components/characterCard';
|
||||
import Loading from '../components/loading';
|
||||
import LoadingError from '../components/loadingError';
|
||||
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 [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 getCharacterCards = () => {
|
||||
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 }];
|
||||
|
||||
return characters.map(character => {
|
||||
const getDataButtons = () => {
|
||||
|
||||
return models.map(model => {
|
||||
|
||||
return (
|
||||
<DataButton
|
||||
label={model.name}
|
||||
handleClick={model.handleClick}
|
||||
key={model.id}
|
||||
/>
|
||||
);
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
const getCards = () => {
|
||||
|
||||
if (currentData === 'quotes') {
|
||||
|
||||
return data.map(quote => {
|
||||
|
||||
return (
|
||||
|
||||
<QuoteCard
|
||||
quote={quote}
|
||||
key={quote.id}
|
||||
/>
|
||||
);
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
return data.map(character => {
|
||||
|
||||
return (
|
||||
|
||||
@@ -57,19 +124,21 @@ export default function Home() {
|
||||
|
||||
setErrorMessage(null);
|
||||
|
||||
if (errorMessage === 'Failed to load characters.') return await getCharacters();
|
||||
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<Greeting />
|
||||
|
||||
{characters.length > 0 ?
|
||||
<DataButtonHolder>
|
||||
{getDataButtons()}
|
||||
</DataButtonHolder>
|
||||
|
||||
{data.length > 0 ?
|
||||
|
||||
<CardHolder>
|
||||
|
||||
{getCharacterCards()}
|
||||
{getCards()}
|
||||
|
||||
</CardHolder>
|
||||
|
||||
|
||||
@@ -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;
|
||||
Reference in New Issue
Block a user