From eb9854fb444f5e16857153d1d833dd1b2ff39449 Mon Sep 17 00:00:00 2001 From: Aditya Pawar Date: Tue, 23 Apr 2024 21:41:51 -0700 Subject: [PATCH] Add pubsub to favorites --- assets/icons.tsx | 1 - src/app/(tabs)/library/index.tsx | 21 ++++++-- .../FavoriteStoryButton.tsx | 51 ++++++++++++------- .../SaveStoryButton/SaveStoryButton.tsx | 43 +++++++++------- 4 files changed, 74 insertions(+), 42 deletions(-) diff --git a/assets/icons.tsx b/assets/icons.tsx index 44728bb1..df93d8cc 100644 --- a/assets/icons.tsx +++ b/assets/icons.tsx @@ -110,7 +110,6 @@ const IconSvgs: Record = { `} /> ), - home_inactive: ( { setFavoritesSelected(true); @@ -60,6 +61,20 @@ function LibraryScreen() { ); }; + useEffect(() => { + if (updateFavoritesListTimeout) { + clearTimeout(updateFavoritesListTimeout); + } + + updateFavoritesListTimeout = setTimeout( + () => + fetchUserStoriesFavorites(user?.id).then(favoriteStories => { + setFavoriteStories(favoriteStories); + }), + 4000, + ); + }, [channels[Channel.FAVORITES]]); + useEffect(() => { if (updateReadingListTimeout) { clearTimeout(updateReadingListTimeout); @@ -70,9 +85,9 @@ function LibraryScreen() { fetchUserStoriesReadingList(user?.id).then(readingList => { setReadingListStories(readingList); }), - 5000, + 4000, ); - }, [channels]); + }, [channels[Channel.SAVED_STORIES]]); useEffect(() => { (async () => { diff --git a/src/components/FavoriteStoryButton/FavoriteStoryButton.tsx b/src/components/FavoriteStoryButton/FavoriteStoryButton.tsx index 97c62bb2..cea45ffa 100644 --- a/src/components/FavoriteStoryButton/FavoriteStoryButton.tsx +++ b/src/components/FavoriteStoryButton/FavoriteStoryButton.tsx @@ -8,6 +8,7 @@ import { isStoryInFavorites, } from '../../queries/savedStories'; import { useSession } from '../../utils/AuthContext'; +import { Channel, usePubSub } from '../../utils/PubSubContext'; type FavoriteStoryButtonProps = { storyId: number; @@ -17,6 +18,7 @@ export default function FavoriteStoryButton({ storyId, }: FavoriteStoryButtonProps) { const { user } = useSession(); + const { publish } = usePubSub() const [storyIsFavorited, setStoryIsFavorited] = useState(false); useEffect(() => { @@ -26,38 +28,49 @@ export default function FavoriteStoryButton({ }, [storyId]); useEffect(() => { - isStoryInFavorites(storyId, user?.id).then(storyInReadingList => - setStoryIsFavorited(storyInReadingList), - ); + isStoryInFavorites(storyId, user?.id).then(storyInFavorites => { + setStoryIsFavorited(storyInFavorites) + publish(Channel.FAVORITES, storyId, storyInFavorites); + }); }, [storyId]); const favoriteStory = async (favorited: boolean) => { setStoryIsFavorited(favorited); if (favorited) { + publish(Channel.FAVORITES, storyId, true); await addUserStoryToFavorites(user?.id, storyId); } else { + publish(Channel.FAVORITES, storyId, false); await deleteUserStoryToFavorites(user?.id, storyId); } }; + const renderFavoritedIcon = () => { + return ( + + + + ) + } + + const renderNotFavoritedIcon = () => { + return ( + + + + ) + } + return ( favoriteStory(!storyIsFavorited)}> - {storyIsFavorited ? ( - - - - ) : ( - - - - )} - + {storyIsFavorited ? renderFavoritedIcon() : renderNotFavoritedIcon()} + ); } diff --git a/src/components/SaveStoryButton/SaveStoryButton.tsx b/src/components/SaveStoryButton/SaveStoryButton.tsx index 0da856f9..97370b32 100644 --- a/src/components/SaveStoryButton/SaveStoryButton.tsx +++ b/src/components/SaveStoryButton/SaveStoryButton.tsx @@ -9,15 +9,12 @@ import { } from '../../queries/savedStories'; import { Channel, usePubSub } from '../../utils/PubSubContext'; import { useSession } from '../../utils/AuthContext'; -import { usePubSub } from '../../utils/PubSubContext'; type SaveStoryButtonProps = { storyId: number; defaultState?: boolean | null; }; -const saveStoryImage = require('../../../assets/save_story.png'); -const savedStoryImage = require('../../../assets/saved_story.png'); export default function SaveStoryButton({ storyId, @@ -56,23 +53,31 @@ export default function SaveStoryButton({ } }; + const renderSavedStoryImage = () => { + return ( + + + + ) + } + + const renderSaveStoryImage = () => { + return ( + + + + ) + } + return ( saveStory(!storyIsSaved)}> - {storyIsSaved ? ( - - - - ) : ( - - - - )} - + {storyIsSaved ? renderSavedStoryImage() : renderSaveStoryImage()} + ); }