Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Get data to single character page #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {Card, Container, FormControl, InputGroup, Row} from 'react-bootstrap';
import './App.css';
import Pagination from "./components/Pagination";
import {withCharacters} from "./context/CharacterContext";
import CharacterProfile from "./components/characterProfile/CharacterProfile";

const App = ({isLoading, characters, characterPages, fetchCharactersByNamePaginated, fetchCharactersPaginated}) => {
const [searchQuery, setSearchQuery] = useState('');
Expand Down Expand Up @@ -39,6 +40,14 @@ const App = ({isLoading, characters, characterPages, fetchCharactersByNamePagina
}
};

// const searchSelectedCharacter = async () => {
// this.toggleLoader(true);
// const marvelCharacter = await fetchCharacterById(1009664);
// console.log('marvelCharacter', marvelCharacter);
// this.toggleLoader(false);
// this.setState({ marvelCharacter: marvelCharacter.results[0] });
// };

return (
<div className="main">
<Container>
Expand All @@ -56,26 +65,27 @@ const App = ({isLoading, characters, characterPages, fetchCharactersByNamePagina
</div>
</Row>
<Pagination pages={characterPages} onPageChanged={onPageChanged}/>
<Row className="Scrollable">
{(!isLoading && characters.length > 0)&& <CharacterProfile character={characters[0]} />}
{/* <Row className="Scrollable">
{isLoading ? (
<div className="Loader">
<div className="loader"></div>
</div>
) : characters.map(character => (
<Card key={character.id} style={{width: '18rem', margin: '20px 40px '}}>
<Card.Img variant="top"
src={character.thumbnail.path + "/standard_fantastic." + character.thumbnail.extension}
alt="Character"
<Card.Img
variant="top"
src={character.thumbnail.path + "/standard_fantastic." + character.thumbnail.extension}
alt="Character"
/>
<Card.Body>
<Card.Title>{character.name}</Card.Title>
<Card.Text>

</Card.Text>
</Card.Body>
</Card>
))}
</Row>
</Row> */}
<div className="footer">
<p>Made with &hearts;</p>
</div>
Expand Down
16 changes: 15 additions & 1 deletion src/api/character.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,21 @@ const fetchCharactersByNamePaginated = (name, page) => {
})
};

const fetchCharacterById = (id) => {
return new Promise((resolve, reject) => {
fetch(`https://gateway.marvel.com/v1/public/characters/${id}?ts=${apiConfig.timestamp}&apikey=${apiConfig.marvelapikey}&hash=${apiConfig.marvelapihash}`)
.then(res => res.json())
.then(res => {
resolve(res.data);
})
.catch(error => {
reject(error);
});
})
};

export {
fetchCharactersPaginated,
fetchCharactersByNamePaginated
fetchCharactersByNamePaginated,
fetchCharacterById
};
66 changes: 66 additions & 0 deletions src/components/characterProfile/CharacterProfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import React, { Component, Fragment } from 'react';
import { Row, Card } from 'react-bootstrap';

import { fetchCharacterById } from '../../api/character';
import DisplayCard from '../DisplayCard';

class CharacterProfile extends Component {
state = {
marvelCharacter: null,
loading: true,
};

async componentDidMount() {
await this.fetchInit();
}

toggleLoader = active => {
this.setState({ loading: active });
};

fetchInit = async () => {
this.toggleLoader(true);
const marvelCharacter = await fetchCharacterById(1009664);
console.log('marvelCharacter', marvelCharacter);
this.toggleLoader(false);
this.setState({ marvelCharacter: marvelCharacter.results[0] });
};

renderLoader() {
const { loading } = this.state;

if (!loading) return null;

return (
<div className="Loader">
<div className="loader"></div>
</div>
);
}

renderCard() {
const { marvelCharacter } = this.state;

return (
<DisplayCard thumbnail={marvelCharacter.thumbnail}
title={marvelCharacter.name} />
);
}

render() {
const { character } = this.props;
console.log('marvelCharacter', character);
return (
<Fragment>
<Row className="Scrollable">
<DisplayCard
thumbnail={character.thumbnail}
title={character.name}
/>
</Row>
</Fragment>
);
}
}

export default CharacterProfile;
16 changes: 16 additions & 0 deletions src/context/CharacterContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ const CharacterProvider = ({children}) => {
});
};

const fetchCharactersById = (id) => {
setIsLoading(true);

API.fetchCharacterById(id)
.then(apiData => {
console.log('apiData', apiData);
setCharacters(apiData.results);
setCharacterPages(Math.ceil(apiData.total / apiConfig.perPage));
setIsLoading(false);
})
.catch(error => {
setCharacters([]);
setIsLoading(false);
});
};

return (
<Provider value={{
fetchCharactersPaginated,
Expand Down