Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2c72ffc613 | ||
|
|
ace3d0bd7e | ||
|
|
85025970ba | ||
|
|
2685e66d17 |
@@ -3,7 +3,7 @@ import Card from 'react-bootstrap/Card';
|
|||||||
import Table from 'react-bootstrap/Table';
|
import Table from 'react-bootstrap/Table';
|
||||||
|
|
||||||
export default function CharacterCard(props) {
|
export default function CharacterCard(props) {
|
||||||
|
|
||||||
const image = new URL(props.character.image).pathname;
|
const image = new URL(props.character.image).pathname;
|
||||||
const characterName = props.character.fullName;
|
const characterName = props.character.fullName;
|
||||||
const firstEpisode = props.character.firstEpisode;
|
const firstEpisode = props.character.firstEpisode;
|
||||||
|
|||||||
@@ -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>
|
||||||
|
);
|
||||||
|
}
|
||||||
+89
-20
@@ -5,44 +5,111 @@ import CardHolder from '../components/cardHolder';
|
|||||||
import CharacterCard from '../components/characterCard';
|
import CharacterCard from '../components/characterCard';
|
||||||
import Loading from '../components/loading';
|
import Loading from '../components/loading';
|
||||||
import LoadingError from '../components/loadingError';
|
import LoadingError from '../components/loadingError';
|
||||||
|
import DataButtonHolder from '../components/dataButtonHolder';
|
||||||
|
import DataButton from '../components/dataButton';
|
||||||
|
|
||||||
import CharacterService from '../services/characterService';
|
import CharacterService from '../services/characterService';
|
||||||
|
import QuoteService from '../services/quoteService';
|
||||||
|
import QuoteCard from '../components/quoteCard';
|
||||||
const characterService = new CharacterService();
|
const characterService = new CharacterService();
|
||||||
|
const quoteService = new QuoteService();
|
||||||
|
|
||||||
export default function Home() {
|
export default function Home() {
|
||||||
|
|
||||||
const [characters, setCharacters] = useState([]);
|
const [currentData, setCurrentData] = useState('characters');
|
||||||
|
const [data, setData] = useState([]);
|
||||||
const [errorMessage, setErrorMessage] = useState(null);
|
const [errorMessage, setErrorMessage] = useState(null);
|
||||||
|
|
||||||
const getCharacters = async () => {
|
const getCharacters = async () => {
|
||||||
|
|
||||||
|
setData([]);
|
||||||
|
setCurrentData('characters');
|
||||||
|
|
||||||
const res = await characterService.getCharacters();
|
const res = await characterService.getCharacters();
|
||||||
|
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
|
|
||||||
return setErrorMessage('Failed to load characters.');
|
return setErrorMessage(`Failed to load characters`);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const characters = await res.json();
|
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(() => {
|
useEffect(() => {
|
||||||
|
|
||||||
getCharacters();
|
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 (
|
return (
|
||||||
|
|
||||||
<CharacterCard
|
<CharacterCard
|
||||||
character={character}
|
character={character}
|
||||||
key={character.id}
|
key={character.id}
|
||||||
/>
|
/>
|
||||||
@@ -57,30 +124,32 @@ export default function Home() {
|
|||||||
|
|
||||||
setErrorMessage(null);
|
setErrorMessage(null);
|
||||||
|
|
||||||
if (errorMessage === 'Failed to load characters.') return await getCharacters();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Greeting />
|
<Greeting />
|
||||||
|
|
||||||
{characters.length > 0 ?
|
<DataButtonHolder>
|
||||||
|
{getDataButtons()}
|
||||||
|
</DataButtonHolder>
|
||||||
|
|
||||||
|
{data.length > 0 ?
|
||||||
|
|
||||||
<CardHolder>
|
<CardHolder>
|
||||||
|
|
||||||
{getCharacterCards()}
|
{getCards()}
|
||||||
|
|
||||||
</CardHolder>
|
</CardHolder>
|
||||||
|
|
||||||
: errorMessage ?
|
|
||||||
|
|
||||||
<LoadingError
|
: errorMessage ?
|
||||||
message={errorMessage}
|
|
||||||
handleClick={handleTryAgain}
|
<LoadingError
|
||||||
/>
|
message={errorMessage}
|
||||||
|
handleClick={handleTryAgain}
|
||||||
: <Loading />}
|
/>
|
||||||
|
|
||||||
|
: <Loading />}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -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