-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[saved] Implement saved story button (#79)
* Implemented save story button * Move story image to assets * Add save story to preview card, one home screen viewing bug * Add pubsub to sync preview and content cards * Bug fixes * Refactor * Create SavedStoryButton componenet * Remove unused imports * Fix type errors --------- Co-authored-by: Aditya Pawar <[email protected]> Co-authored-by: Aditya Pawar <[email protected]>
- Loading branch information
1 parent
860d346
commit b9865bc
Showing
18 changed files
with
209 additions
and
40 deletions.
There are no files selected for viewing
File renamed without changes
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
Binary file not shown.
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,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] ?? false); | ||
} | ||
}, [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 ( | ||
<TouchableOpacity onPress={() => saveStory(!storyIsSaved)}> | ||
{storyIsSaved ? ( | ||
<Image style={{ width: 30, height: 30 }} source={savedStoryImage} /> | ||
) : ( | ||
<Image style={{ width: 30, height: 30 }} source={saveStoryImage} /> | ||
)} | ||
</TouchableOpacity> | ||
); | ||
} |
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
Oops, something went wrong.