refactor: clean code and remove unnecessary dependencies
This commit is contained in:
1 parent
eceae6fec7
commit
ebf599eed6
92 files changed
+1152
-2842
No files matched your search
@@ -1,19 +1,22 @@
|
||||
'use client';
|
||||
|
||||
import useFavorites from '@/app/(site)/favorites/useFavorites';
|
||||
import List from '@/components/Movie/List';
|
||||
import List from '@/components/List';
|
||||
import useMounted from '@/hooks/useMounted';
|
||||
|
||||
import useFavorites from './useFavorites';
|
||||
|
||||
export default function Container() {
|
||||
const { pages } = useFavorites();
|
||||
const { shows } = useFavorites();
|
||||
const isMounted = useMounted();
|
||||
|
||||
if (isMounted && !pages[0]?.results?.length)
|
||||
if (!isMounted) return null;
|
||||
|
||||
if (!shows.length)
|
||||
return (
|
||||
<div className='h-40 w-full flex items-center text-center justify-center text-xl text-muted-foreground'>
|
||||
There are no items in your favorites list
|
||||
</div>
|
||||
);
|
||||
|
||||
return <List pages={pages} />;
|
||||
return <List shows={shows} />;
|
||||
}
|
||||
@@ -8,8 +8,8 @@ export const metadata: Metadata = {
|
||||
'View your favorite shows on Movie Advisor. Keep track of films you’ve added and revisit your top picks anytime.'
|
||||
};
|
||||
|
||||
export const revalidate = 3600;
|
||||
|
||||
export default function Favorites() {
|
||||
const Favorites = () => {
|
||||
return <Container />;
|
||||
}
|
||||
};
|
||||
|
||||
export default Favorites;
|
||||
@@ -1,41 +1,30 @@
|
||||
import { atom, useAtomValue, useSetAtom } from 'jotai';
|
||||
import { atomWithStorage } from 'jotai/utils';
|
||||
|
||||
const favoritesAtom = atomWithStorage<Array<MovieDBResponse>>('favorite', [
|
||||
{ page: 1, results: [], total_pages: 1, total_results: 0 }
|
||||
]);
|
||||
const favoritesAtom = atomWithStorage<Show[]>('favorite', [], undefined, {
|
||||
unstable_getOnInit: true
|
||||
});
|
||||
const favoritesMapAtom = atom((get) => {
|
||||
const map = new Map();
|
||||
const favorites = get(favoritesAtom);
|
||||
|
||||
favorites[0].results.forEach((movie) => map.set(movie.id, movie));
|
||||
favorites.forEach((show) => map.set(show.id, show));
|
||||
|
||||
return map as Map<number, Movie>;
|
||||
return map as Map<Show['id'], Show>;
|
||||
});
|
||||
|
||||
const useFavorites = () => {
|
||||
return {
|
||||
pages: useAtomValue(favoritesAtom),
|
||||
shows: useAtomValue(favoritesAtom),
|
||||
map: useAtomValue(favoritesMapAtom)
|
||||
};
|
||||
};
|
||||
|
||||
export const useFavoritesMutation = () => {
|
||||
const setFavorites = useSetAtom(favoritesAtom);
|
||||
const add = (movie: Movie) => {
|
||||
setFavorites((favorites) => {
|
||||
favorites[0].results = [movie, ...favorites[0].results];
|
||||
favorites[0].total_results = favorites[0].results.length;
|
||||
return [...favorites];
|
||||
});
|
||||
};
|
||||
|
||||
const remove = (movieId: number) => {
|
||||
setFavorites((favorites) => {
|
||||
favorites[0].results = favorites[0].results.filter((movie) => movie.id !== movieId);
|
||||
favorites[0].total_results = favorites[0].results.length;
|
||||
return [...favorites];
|
||||
});
|
||||
};
|
||||
const add = (show: Show) => setFavorites((favorites) => [show, ...favorites]);
|
||||
const remove = (showId: number) => setFavorites((favorites) => favorites.filter(({ id }) => id !== showId));
|
||||
|
||||
return {
|
||||
add,
|
||||
@@ -43,13 +32,13 @@ export const useFavoritesMutation = () => {
|
||||
};
|
||||
};
|
||||
|
||||
export const useFavoriteToggle = (movie: Movie) => {
|
||||
export const useFavoriteToggle = (show: Show) => {
|
||||
const { add, remove } = useFavoritesMutation();
|
||||
const { map } = useFavorites();
|
||||
|
||||
const isFavorite = map.has(movie.id);
|
||||
const isFavorite = map.has(show.id);
|
||||
|
||||
const toggle = () => (isFavorite ? remove(movie.id) : add(movie));
|
||||
const toggle = () => (isFavorite ? remove(show.id) : add(show));
|
||||
|
||||
return { isFavorite, toggle };
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user