-
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
Showing
10 changed files
with
169 additions
and
17 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,42 @@ | ||
const KEY = 'saved_pokemon'; | ||
|
||
function savePokemonToFavourites(name, data) { | ||
const savedItems = JSON.parse(localStorage.getItem(KEY)); | ||
if(savedItems === null){ | ||
const newObject = { | ||
[name]: data | ||
}; | ||
localStorage.setItem(KEY, JSON.stringify(newObject)); | ||
} else { | ||
const newObject = { | ||
...savedItems, | ||
[name]: data | ||
}; | ||
localStorage.setItem(KEY, JSON.stringify(newObject)); | ||
} | ||
} | ||
|
||
function removePokemonFromFavourites(name) { | ||
const savedItems = JSON.parse(localStorage.getItem(KEY)); | ||
if(savedItems === null) return false; | ||
else { | ||
if(Object.keys(savedItems).includes(name)){ | ||
const newObject = {...savedItems}; | ||
delete newObject[name]; | ||
console.log("DELETED FAVOURITE, New object:", newObject); | ||
localStorage.setItem(KEY, JSON.stringify(newObject)); | ||
} | ||
} | ||
} | ||
|
||
function getFavourites() { | ||
const savedItems = localStorage.getItem(KEY); | ||
if(savedItems === null) return {}; | ||
return JSON.parse(savedItems); | ||
} | ||
|
||
export { | ||
getFavourites, | ||
removePokemonFromFavourites, | ||
savePokemonToFavourites | ||
} |
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,53 @@ | ||
import React, { useEffect, useState } from "react"; | ||
import { getFavourites, removePokemonFromFavourites } from "../../services/favourites"; | ||
import Layout from "../../components/Layout"; | ||
import { Col, Container, Row } from "react-bootstrap"; | ||
import "./favourites.scss"; | ||
|
||
function Favourites() { | ||
const [favourites, setFavourites] = useState({}); | ||
|
||
useEffect(() => { | ||
setFavourites(getFavourites()); | ||
}, []); | ||
|
||
const removeFavourite = (name) => { | ||
removePokemonFromFavourites(name); | ||
setFavourites(prevState => { | ||
const newFavourites = {...prevState}; | ||
delete newFavourites[name]; | ||
return newFavourites; | ||
}) | ||
} | ||
|
||
return ( | ||
<Layout> | ||
<Container className="p-3"> | ||
<h3 className="fs-3 fw-bold text-center mb-4 bg-dark text-light py-2">FAVOURITES</h3> | ||
<Row> | ||
{Object.keys(favourites).length > 0 ? ( | ||
Object.keys(favourites).map((favourite) => ( | ||
<Col className="col-3"> | ||
<div className="favourites-item"> | ||
<span>{favourite}</span> | ||
<img | ||
width={16} | ||
height={16} | ||
className="align-self-center ms-auto remove-favourite" | ||
src="/images/cross.png" | ||
alt="remove favourite" | ||
onClick={() => removeFavourite(favourite)} | ||
/> | ||
</div> | ||
</Col> | ||
)) | ||
) : ( | ||
<span className="fw-bold text-center fs-5">No Favourites</span> | ||
)} | ||
</Row> | ||
</Container> | ||
</Layout> | ||
); | ||
} | ||
|
||
export default Favourites; |
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,11 @@ | ||
.favourites-item{ | ||
border: 3px solid black; | ||
background-color: rgb(243,244,245); | ||
border-radius: .5rem; | ||
padding: 10px 14px; | ||
display: flex; | ||
flex-direction: row; | ||
} | ||
.remove-favourite{ | ||
cursor: pointer; | ||
} |
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,2 @@ | ||
import Favourites from "./favourites"; | ||
export default Favourites; |
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