-
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.
[wip] pass favorites into user favorites
- Loading branch information
1 parent
768c181
commit 0bd742d
Showing
4 changed files
with
38 additions
and
88 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 |
---|---|---|
@@ -1,32 +1,32 @@ | ||
"use client"; | ||
|
||
import {useCallback} from "react"; | ||
import {useLocalStorage} from "usehooks-ts"; | ||
|
||
type Favorite = { | ||
uuid: string | ||
title: string | ||
} | ||
import { useCallback } from "react"; | ||
import { useLocalStorage, useIsClient } from "usehooks-ts"; | ||
|
||
const useFavorites = (): { | ||
favs: Favorite[], | ||
addFav: (_uuid: string, _title: string) => void, | ||
removeFav: (_uuid: string) => void | ||
favs: string[]; | ||
addFav: (_uuid: string) => void; | ||
removeFav: (_uuid: string) => void; | ||
} => { | ||
const [favs, setFavs] = useLocalStorage<Favorite[]>("favorites", [], {initializeWithValue: false}) | ||
const isClient = useIsClient(); | ||
const [favs, setFavs] = useLocalStorage<string[]>("favorites", [], { initializeWithValue: false }); | ||
|
||
const addFav = useCallback((uuid: string, title: string) => { | ||
setFavs([...favs, {uuid, title}]) | ||
}, [favs, setFavs]) | ||
const addFav = useCallback( | ||
(uuid: string) => { | ||
setFavs([...favs, uuid ]); | ||
}, | ||
[favs, setFavs] | ||
); | ||
|
||
const removeFav = useCallback((uuid: string) => { | ||
const itemIndex = favs.findIndex(fav => fav.uuid === uuid); | ||
const oldFavs = [...favs]; | ||
oldFavs.splice(itemIndex, 1); | ||
setFavs(oldFavs); | ||
}, [favs, setFavs]) | ||
const removeFav = useCallback( | ||
(uuid: string) => { | ||
const updatedFavs = favs.filter((fav) => fav !== uuid); | ||
setFavs(updatedFavs); | ||
}, | ||
[favs, setFavs] | ||
); | ||
|
||
return {favs, addFav, removeFav} | ||
} | ||
return { favs: isClient ? favs : [], addFav, removeFav }; | ||
}; | ||
|
||
export default useFavorites; | ||
export default useFavorites; |