Skip to content

Commit

Permalink
Try add favorites
Browse files Browse the repository at this point in the history
  • Loading branch information
isadoragalvaoss committed Sep 13, 2021
1 parent f59486d commit 33112f0
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/components/getCharacters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import notfound from "../images/not-found.png";

const GetCharacter: React.FC = () => {
const [character, setCharacter] = useState<Array<charactersData>>([]);

const [favorite, setFavorite] = useState<Array<charactersData>>([]);
const [searchName, setSearchName] = useState<string>("");

const [search, setSearch] = useState<boolean>(false);
const [loading, setLoading] = useState<boolean>(true);

Expand All @@ -16,6 +15,23 @@ const GetCharacter: React.FC = () => {
setSearchName(searchName);
};

useEffect(() => {
localStorage.setItem("favorite", JSON.stringify(favorite));
}, [favorite]);

const newFavorite = () => {
Service.getAll()
.then((response) => {
const favoritos = response.data.results;
console.log(favoritos);
const salvar = JSON.stringify(favoritos);
localStorage.setItem("favoritos", salvar);
})
.catch((e) => {
console.log(e);
});
};

useEffect(() => {
retrieveCharacter();
}, []);
Expand Down Expand Up @@ -120,6 +136,7 @@ const GetCharacter: React.FC = () => {
marginTop: -15,
marginLeft: 10,
}}
onClick={newFavorite}
>
<svg
xmlns="http://www.w3.org/2000/svg"
Expand Down

0 comments on commit 33112f0

Please sign in to comment.