From f5bf240f77f5b4ff1790e1200eefb0fd32287c66 Mon Sep 17 00:00:00 2001 From: djanilson alves Date: Wed, 23 Oct 2019 11:10:23 -0300 Subject: [PATCH] get data to single character page --- src/App.js | 22 +++++-- src/api/character.js | 16 ++++- .../characterProfile/CharacterProfile.js | 66 +++++++++++++++++++ src/context/CharacterContext.js | 16 +++++ 4 files changed, 113 insertions(+), 7 deletions(-) create mode 100644 src/components/characterProfile/CharacterProfile.js diff --git a/src/App.js b/src/App.js index 1836c0f..77933df 100644 --- a/src/App.js +++ b/src/App.js @@ -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(''); @@ -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 (
@@ -56,26 +65,27 @@ const App = ({isLoading, characters, characterPages, fetchCharactersByNamePagina
- + {(!isLoading && characters.length > 0)&& } + {/* {isLoading ? (
) : characters.map(character => ( - {character.name} - ))} -
+
*/}

Made with ♥

diff --git a/src/api/character.js b/src/api/character.js index a9bbcca..8ed69d9 100644 --- a/src/api/character.js +++ b/src/api/character.js @@ -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 }; diff --git a/src/components/characterProfile/CharacterProfile.js b/src/components/characterProfile/CharacterProfile.js new file mode 100644 index 0000000..7658148 --- /dev/null +++ b/src/components/characterProfile/CharacterProfile.js @@ -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 ( +
+
+
+ ); + } + + renderCard() { + const { marvelCharacter } = this.state; + + return ( + + ); + } + + render() { + const { character } = this.props; + console.log('marvelCharacter', character); + return ( + + + + + + ); + } +} + +export default CharacterProfile; diff --git a/src/context/CharacterContext.js b/src/context/CharacterContext.js index f25e9c0..ca3d0e5 100644 --- a/src/context/CharacterContext.js +++ b/src/context/CharacterContext.js @@ -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 (