-
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] create favorites list; adjust favorite button to include path a…
…nd favorites list
- Loading branch information
1 parent
16f8faf
commit e4d3ccf
Showing
5 changed files
with
108 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
"use client"; | ||
import useFavorites from "@lib/hooks/useFavorites"; | ||
import { HTMLAttributes } from "react"; | ||
import { HeartIcon } from "@heroicons/react/24/outline"; | ||
import { clsx } from "clsx"; | ||
import { useIsClient } from "usehooks-ts"; | ||
|
||
type Props = HTMLAttributes<HTMLButtonElement> & { | ||
uuid: string; | ||
title: string; | ||
path: string; | ||
units?: number; | ||
}; | ||
|
||
const FavoriteButton = ({ uuid, title, path, units, ...props }: Props) => { | ||
const { favs, addFav, removeFav } = useFavorites(); | ||
const isFavorite = favs.some((fav) => fav.uuid === uuid); | ||
|
||
const onClick = () => { | ||
isFavorite ? removeFav(uuid) : addFav(uuid, title, path, units); | ||
}; | ||
|
||
// No need to add the button on the server, but also it doesn't show initial state correctly for some reason. | ||
if (!useIsClient()) return null; | ||
|
||
return ( | ||
<button onClick={onClick} role="switch" aria-checked={isFavorite} {...props}> | ||
<HeartIcon | ||
width={30} | ||
className={clsx("text-spirited-dark", { "fill-spirited-dark": isFavorite })} | ||
/> | ||
<span className="sr-only">Add/Remove "{title}" from favorites</span> | ||
</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,51 @@ | ||
"use client"; | ||
|
||
import useFavorites from "@lib/hooks/useFavorites"; | ||
import {HeartIcon} from "@heroicons/react/24/outline"; | ||
import {useIsClient} from "usehooks-ts"; | ||
import { XMarkIcon } from "@heroicons/react/20/solid"; | ||
|
||
|
||
const FavoritesList = () => { | ||
const { favs, removeFav } = useFavorites(); | ||
|
||
const removeFavorite = (uuid: string) => removeFav(uuid); | ||
|
||
// No need to add the button on the server, but also it doesn't show initial state correctly for some reason. | ||
if (!useIsClient()) return; | ||
|
||
return ( | ||
<div> | ||
{favs.length > 0 ? | ||
<div> | ||
Your favorites list is empty. Tap the{" "} | ||
<HeartIcon width={30} className="text-spirited-dark" />{" "} | ||
icon on courses you’re interested in to see them here. And share them with family and friends. | ||
</div> | ||
: | ||
<> | ||
<ul> | ||
{favs.map(fav => | ||
<li key={fav.uuid}> | ||
<button type="button" onClick={() => removeFavorite(fav.uuid)}> | ||
<XMarkIcon width={24} /> | ||
<span className="sr-only">Remove "{fav.title}" from favorites</span> | ||
</button> | ||
<p>{fav.title}</p> | ||
<p>Units {fav.units}</p> | ||
</li> | ||
)} | ||
</ul> | ||
<div> | ||
Total units | ||
</div> | ||
</> | ||
} | ||
<div> | ||
{/* Shareable options: Text, Email, Copy Link */} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default FavoritesList |
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,38 @@ | ||
"use client"; | ||
|
||
import { useCallback } from "react"; | ||
import { useLocalStorage, useIsClient } from "usehooks-ts"; | ||
import { useLocalStorage } from "usehooks-ts"; | ||
|
||
type Favorite = { | ||
uuid: string; | ||
title: string; | ||
path: string; | ||
units: number; | ||
}; | ||
|
||
const useFavorites = (): { | ||
favs: string[]; | ||
addFav: (_uuid: string) => void; | ||
favs: Favorite[]; | ||
addFav: (_uuid: string, _title: string, _path: string, _units?: number) => void; | ||
removeFav: (_uuid: string) => void; | ||
} => { | ||
const isClient = useIsClient(); | ||
const [favs, setFavs] = useLocalStorage<string[]>("favorites", [], { initializeWithValue: false }); | ||
const [favs, setFavs] = useLocalStorage<Favorite[]>("favorites", [], {initializeWithValue: false}); | ||
|
||
const addFav = useCallback( | ||
(uuid: string) => { | ||
setFavs([...favs, uuid ]); | ||
(uuid: string, title: string, path: string, units: number) => { | ||
setFavs([...favs, { uuid, title, path, units }]); | ||
}, | ||
[favs, setFavs] | ||
); | ||
|
||
const removeFav = useCallback( | ||
(uuid: string) => { | ||
const updatedFavs = favs.filter((fav) => fav !== uuid); | ||
const updatedFavs = favs.filter((fav) => fav.uuid !== uuid); | ||
setFavs(updatedFavs); | ||
}, | ||
[favs, setFavs] | ||
); | ||
|
||
return { favs: isClient ? favs : [], addFav, removeFav }; | ||
return { favs, addFav, removeFav }; | ||
}; | ||
|
||
export default useFavorites; |