-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6db32d3
commit 77c5940
Showing
11 changed files
with
188 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { useState } from "react"; | ||
|
||
export const useForm = (initialState = {}) => { | ||
const [values, setValues] = useState(initialState); | ||
|
||
const reset = () => { | ||
setValues(initialState); | ||
}; | ||
|
||
const handleInputChange = ({ target }) => { | ||
setValues({ | ||
...values, | ||
[target.name]: target.value, | ||
}); | ||
}; | ||
|
||
return [values, handleInputChange, reset]; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
import React from "react"; | ||
import ReactDOM from "react-dom"; | ||
import "./index.css"; | ||
import HeroesApp from "./HeroesApp"; | ||
|
||
ReactDOM.render(<HeroesApp />, document.getElementById("root")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,63 @@ | ||
import React from "react"; | ||
import React, { useMemo } from "react"; | ||
import { useParams, Redirect } from "react-router-dom"; | ||
import getHeroById from "../../selectors/getHeroById"; | ||
|
||
const HeroScreen = ({ history }) => { | ||
const { heroId: heroId } = useParams(); | ||
|
||
const hero = useMemo(() => getHeroById(heroId), [heroId]); | ||
|
||
if (!hero) { | ||
return <Redirect to="/" />; | ||
} | ||
|
||
const handleReturn = () => { | ||
if (history.length <= 2) { | ||
history.push("/"); | ||
} else { | ||
history.goBack(); | ||
} | ||
}; | ||
|
||
const { superhero, publisher, alter_ego, first_appearance, characters } = | ||
hero; | ||
|
||
const HeroeScreen = () => { | ||
return ( | ||
<div> | ||
<h1>Hero Screen</h1> | ||
<div className="row mt-5"> | ||
<div className="col-4"> | ||
<img | ||
src={`../assets/heroes/${heroId}.jpg`} | ||
alt={superhero} | ||
className="img-thumbnail animate__animated animate__fadeInLeft" | ||
/> | ||
</div> | ||
|
||
<div className="col-8 animate__animated animate__fadeIn"> | ||
<h3> {superhero} </h3> | ||
<ul className="list-group list-group-flush"> | ||
<li className="list-group-item"> | ||
{" "} | ||
<b> Alter ego: </b> {alter_ego}{" "} | ||
</li> | ||
<li className="list-group-item"> | ||
{" "} | ||
<b> Publisher: </b> {publisher}{" "} | ||
</li> | ||
<li className="list-group-item"> | ||
{" "} | ||
<b> First appearance: </b> {first_appearance}{" "} | ||
</li> | ||
</ul> | ||
|
||
<h5> Characters </h5> | ||
<p> {characters} </p> | ||
|
||
<button className="btn btn-outline-info" onClick={handleReturn}> | ||
Return | ||
</button> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default HeroeScreen; | ||
export default HeroScreen; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import React, { useMemo } from "react"; | ||
import queryString from "query-string"; | ||
import HeroCard from "../heroes/HeroCard"; | ||
import { useForm } from "../../hooks/useForm.js"; | ||
import { useLocation } from "react-router-dom"; | ||
import { getHeroesByName } from "../../selectors/getHeroesByName"; | ||
|
||
const SearchScreen = ({ history }) => { | ||
const location = useLocation(); | ||
const { q = "" } = queryString.parse(location.search); | ||
|
||
const [formValues, handleInputChange] = useForm({ | ||
searchText: q, | ||
}); | ||
const { searchText } = formValues; | ||
|
||
const heroesFiltered = useMemo(() => getHeroesByName(q), [q]); | ||
|
||
const handleSearch = (e) => { | ||
e.preventDefault(); | ||
history.push(`?q=${searchText}`); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<h1>Search Screen</h1> | ||
<hr /> | ||
|
||
<div className="row"> | ||
<div className="col-5"> | ||
<h4> Search Form </h4> | ||
<hr /> | ||
|
||
<form onSubmit={handleSearch}> | ||
<input | ||
type="text" | ||
placeholder="Find your hero" | ||
className="form-control" | ||
name="searchText" | ||
autoComplete="off" | ||
value={searchText} | ||
onChange={handleInputChange} | ||
/> | ||
|
||
<button | ||
type="submit" | ||
className="btn m-1 btn-block btn-outline-primary" | ||
> | ||
Search... | ||
</button> | ||
</form> | ||
</div> | ||
|
||
<div className="col-7"> | ||
<h4> Results </h4> | ||
<hr /> | ||
|
||
{q === "" && <div className="alert alert-info">Search a hero</div>} | ||
|
||
{q !== "" && heroesFiltered.length === 0 && ( | ||
<div className="alert alert-danger"> | ||
There is no a hero with {q} | ||
</div> | ||
)} | ||
|
||
{heroesFiltered.map((hero) => ( | ||
<HeroCard key={hero.id} {...hero} /> | ||
))} | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default SearchScreen; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import heroes from "../data/heroes"; | ||
|
||
export default getHeroeById = (id) => { | ||
const getHeroById = (id) => { | ||
return heroes.find((heroe) => heroe.id === id); | ||
}; | ||
|
||
export default getHeroById; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import heroes from "../data/heroes"; | ||
|
||
export const getHeroesByName = (name = "") => { | ||
if (name === "") { | ||
return []; | ||
} | ||
|
||
name = name.toLocaleLowerCase(); | ||
return heroes.filter((hero) => | ||
hero.superhero.toLocaleLowerCase().includes(name) | ||
); | ||
}; |