-
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.
feat(favorites): implement favorites functionality with persistent to…
… local storage
- Loading branch information
1 parent
76b20d7
commit a4e2d20
Showing
6 changed files
with
114 additions
and
4 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
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,26 @@ | ||
import { GoHeart, GoHeartFill } from 'react-icons/go'; | ||
import { useFavorites } from '@context/favorites-provider'; | ||
|
||
const FavoriteButton: React.FC<{ name: string }> = ({ name }) => { | ||
const { favorites, addFavorite, removeFavorite } = useFavorites(); | ||
|
||
const isFavorite = favorites.includes(name); | ||
|
||
const handleClick = (e: React.MouseEvent<HTMLButtonElement>) => { | ||
e.stopPropagation(); | ||
|
||
if (isFavorite) { | ||
return removeFavorite(name); | ||
} | ||
|
||
addFavorite(name); | ||
}; | ||
|
||
return ( | ||
<button className='favorite-button' onClick={ handleClick }> | ||
{ isFavorite ? <GoHeartFill /> : <GoHeart /> } | ||
</button> | ||
); | ||
}; | ||
|
||
export default FavoriteButton; |
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,32 @@ | ||
import { useContext, createContext } from 'react'; | ||
import usePersistedFavorites from '@hooks/use-persisted-favorites'; | ||
|
||
type FavoritesContextType = { | ||
favorites: string[]; | ||
addFavorite: (name: string) => void; | ||
removeFavorite: (name: string) => void; | ||
}; | ||
|
||
const favoritesContext = createContext<FavoritesContextType | undefined>(undefined); | ||
|
||
export const useFavorites = () => { | ||
const context = useContext(favoritesContext); | ||
|
||
if(! context) { | ||
throw new Error('useFavorites must be used within a FavoritesProvider'); | ||
} | ||
|
||
return context; | ||
} | ||
|
||
export const FavoritesProvider: React.FC<{ | ||
children: React.ReactNode; | ||
}> = ({ children }) => { | ||
const { favorites, addFavorite, removeFavorite } = usePersistedFavorites(); | ||
|
||
return ( | ||
<favoritesContext.Provider value={{ favorites, addFavorite, removeFavorite }}> | ||
{ children } | ||
</favoritesContext.Provider> | ||
); | ||
}; |
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,31 @@ | ||
import { useState, useEffect } from 'react'; | ||
|
||
const usePersistentFavorites = () => { | ||
const [favorites, setFavorites] = useState<string[]>([]); | ||
|
||
useEffect(() => { | ||
const favorites = localStorage.getItem('favorites'); | ||
|
||
if(favorites) { | ||
setFavorites(JSON.parse(favorites)); | ||
} | ||
}, []); | ||
|
||
return { | ||
favorites, | ||
addFavorite: (name: string) => { | ||
const updatedFavorites = [...favorites, name]; | ||
|
||
setFavorites(updatedFavorites); | ||
localStorage.setItem('favorites', JSON.stringify(updatedFavorites)); | ||
}, | ||
removeFavorite: (name: string) => { | ||
const updatedFavorites = favorites.filter((favorite) => favorite !== name); | ||
|
||
setFavorites(updatedFavorites); | ||
localStorage.setItem('favorites', JSON.stringify(updatedFavorites)); | ||
}, | ||
}; | ||
}; | ||
|
||
export default usePersistentFavorites; |