diff --git a/src/components/ContentCard/ContentCard.tsx b/src/components/ContentCard/ContentCard.tsx index f297d08..3a6c9d5 100644 --- a/src/components/ContentCard/ContentCard.tsx +++ b/src/components/ContentCard/ContentCard.tsx @@ -18,6 +18,7 @@ import { useSession } from '../../utils/AuthContext'; import Emoji from 'react-native-emoji'; import { useCallback, useEffect, useMemo, useState } from 'react'; import { usePubSub } from '../../utils/PubSubContext'; +import SaveStoryButton from '../SaveStoryButton/SaveStoryButton'; type ContentCardProps = { title: string; @@ -28,9 +29,6 @@ type ContentCardProps = { pressFunction: (event: GestureResponderEvent) => void; }; -const saveStoryImage = require('../../../assets/save_story.png'); -const savedStoryImage = require('../../../assets/saved_story.png'); - function ContentCard({ title, author, @@ -39,34 +37,6 @@ function ContentCard({ storyId, pressFunction, }: ContentCardProps) { - const { user } = useSession(); - const [storyIsSaved, setStoryIsSaved] = useState(false); - const { channels, initializeChannel, publish } = usePubSub(); - - useEffect(() => { - isStoryInReadingList(storyId, user?.id).then(storyInReadingList => { - setStoryIsSaved(storyInReadingList); - initializeChannel(storyId); - }); - }, [storyId]); - - useEffect(() => { - // if another card updates this story, update it here also - if (typeof channels[storyId] !== 'undefined') { - setStoryIsSaved(channels[storyId]); - } - }, [channels[storyId]]); - - const saveStory = async (saved: boolean) => { - setStoryIsSaved(saved); - publish(storyId, saved); - if (saved) { - await addUserStoryToReadingList(user?.id, storyId); - } else { - await deleteUserStoryToReadingList(user?.id, storyId); - } - }; - return ( @@ -113,18 +83,8 @@ function ContentCard({ - saveStory(!storyIsSaved)}> - {storyIsSaved ? ( - - ) : ( - - )} + + diff --git a/src/components/PreviewCard/PreviewCard.tsx b/src/components/PreviewCard/PreviewCard.tsx index 708ed80..8ba1a21 100644 --- a/src/components/PreviewCard/PreviewCard.tsx +++ b/src/components/PreviewCard/PreviewCard.tsx @@ -11,7 +11,7 @@ import { Image } from 'expo-image'; import styles from './styles'; import globalStyles from '../../styles/globalStyles'; -import { useCallback, useEffect, useMemo, useState } from 'react'; +import { useEffect, useState } from 'react'; import { addUserStoryToReadingList, deleteUserStoryToReadingList, @@ -20,6 +20,7 @@ import { import { useSession } from '../../utils/AuthContext'; import { useIsFocused } from '@react-navigation/native'; import { usePubSub } from '../../utils/PubSubContext'; +import SaveStoryButton from '../SaveStoryButton/SaveStoryButton'; const placeholderImage = 'https://gwn-uploads.s3.amazonaws.com/wp-content/uploads/2021/10/10120952/Girls-Write-Now-logo-avatar.png'; @@ -47,42 +48,6 @@ function PreviewCard({ tags, pressFunction, }: PreviewCardProps) { - const { user } = useSession(); - const isFocused = useIsFocused(); - const [storyIsSaved, setStoryIsSaved] = useState(false); - const { channels, initializeChannel, publish } = usePubSub(); - - useEffect(() => { - isStoryInReadingList(storyId, user?.id).then(storyInReadingList => { - setStoryIsSaved(storyInReadingList); - initializeChannel(storyId); - }); - }, [storyId]); - - useEffect(() => { - // if another card updates this story, update it here also - if (typeof channels[storyId] !== 'undefined') { - setStoryIsSaved(channels[storyId]); - } - }, [channels[storyId]]); - - useEffect(() => { - isStoryInReadingList(storyId, user?.id).then(storyInReadingList => - setStoryIsSaved(storyInReadingList), - ); - }, [storyId, isFocused]); - - const saveStory = async (saved: boolean) => { - setStoryIsSaved(saved); - publish(storyId, saved); // update other cards with this story - - if (saved) { - await addUserStoryToReadingList(user?.id, storyId); - } else { - await deleteUserStoryToReadingList(user?.id, storyId); - } - }; - return ( @@ -90,18 +55,8 @@ function PreviewCard({ {title} - saveStory(!storyIsSaved)}> - {storyIsSaved ? ( - - ) : ( - - )} + + diff --git a/src/components/SaveStoryButton/SaveStoryButton.tsx b/src/components/SaveStoryButton/SaveStoryButton.tsx new file mode 100644 index 0000000..9aded23 --- /dev/null +++ b/src/components/SaveStoryButton/SaveStoryButton.tsx @@ -0,0 +1,64 @@ +import { useEffect, useState } from 'react'; +import { + addUserStoryToReadingList, + deleteUserStoryToReadingList, + isStoryInReadingList, +} from '../../queries/savedStories'; +import { usePubSub } from '../../utils/PubSubContext'; +import { useSession } from '../../utils/AuthContext'; +import { Image } from 'expo-image'; +import { TouchableOpacity } from 'react-native-gesture-handler'; + +type SaveStoryButtonProps = { + storyId: number; +}; + +const saveStoryImage = require('../../../assets/save_story.png'); +const savedStoryImage = require('../../../assets/saved_story.png'); + +export default function SaveStoryButton({ storyId }: SaveStoryButtonProps) { + const { user } = useSession(); + const [storyIsSaved, setStoryIsSaved] = useState(false); + const { channels, initializeChannel, publish } = usePubSub(); + + useEffect(() => { + isStoryInReadingList(storyId, user?.id).then(storyInReadingList => { + setStoryIsSaved(storyInReadingList); + initializeChannel(storyId); + }); + }, [storyId]); + + useEffect(() => { + // if another card updates this story, update it here also + if (typeof channels[storyId] !== 'undefined') { + setStoryIsSaved(channels[storyId]); + } + }, [channels[storyId]]); + + useEffect(() => { + isStoryInReadingList(storyId, user?.id).then(storyInReadingList => + setStoryIsSaved(storyInReadingList), + ); + }, [storyId]); + + const saveStory = async (saved: boolean) => { + setStoryIsSaved(saved); + publish(storyId, saved); // update other cards with this story + + if (saved) { + await addUserStoryToReadingList(user?.id, storyId); + } else { + await deleteUserStoryToReadingList(user?.id, storyId); + } + }; + + return ( + saveStory(!storyIsSaved)}> + {storyIsSaved ? ( + + ) : ( + + )} + + ); +}