Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
adityapawar1 committed Apr 18, 2024
1 parent ce4d017 commit 5314e98
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
21 changes: 11 additions & 10 deletions src/components/ContentCard/ContentCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ function ContentCard({
const [storyIsSaved, setStoryIsSaved] = useState(false);
const { channels, initializeChannel, publish } = usePubSub();

const savedStoryImageComponent = useMemo(() => {
return <Image style={{ width: 30, height: 30 }} source={savedStoryImage} />;
}, []);
const saveStoryImageComponent = useMemo(() => {
return <Image style={{ width: 30, height: 30 }} source={saveStoryImage} />;
}, []);

useEffect(() => {
isStoryInReadingList(storyId, user?.id).then(storyInReadingList => {
setStoryIsSaved(storyInReadingList);
Expand Down Expand Up @@ -121,9 +114,17 @@ function ContentCard({
</View>
</View>
<TouchableOpacity onPress={() => saveStory(!storyIsSaved)}>
{storyIsSaved
? savedStoryImageComponent
: saveStoryImageComponent}
{storyIsSaved ? (
<Image
style={{ width: 30, height: 30 }}
source={savedStoryImage}
/>
) : (
<Image
style={{ width: 30, height: 30 }}
source={saveStoryImage}
/>
)}
</TouchableOpacity>
</View>
</View>
Expand Down
22 changes: 13 additions & 9 deletions src/components/PreviewCard/PreviewCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,6 @@ function PreviewCard({
const [storyIsSaved, setStoryIsSaved] = useState(false);
const { channels, initializeChannel, publish } = usePubSub();

const savedStoryImageComponent = useMemo(() => {
return <Image style={{ width: 30, height: 30 }} source={savedStoryImage} />;
}, []);
const saveStoryImageComponent = useMemo(() => {
return <Image style={{ width: 30, height: 30 }} source={saveStoryImage} />;
}, []);

useEffect(() => {
isStoryInReadingList(storyId, user?.id).then(storyInReadingList => {
setStoryIsSaved(storyInReadingList);
Expand All @@ -81,7 +74,8 @@ function PreviewCard({

const saveStory = async (saved: boolean) => {
setStoryIsSaved(saved);
publish(storyId, saved);
publish(storyId, saved); // update other cards with this story

if (saved) {
await addUserStoryToReadingList(user?.id, storyId);
} else {
Expand All @@ -97,7 +91,17 @@ function PreviewCard({
{title}
</Text>
<TouchableOpacity onPress={() => saveStory(!storyIsSaved)}>
{storyIsSaved ? savedStoryImageComponent : saveStoryImageComponent}
{storyIsSaved ? (
<Image
style={{ width: 30, height: 30 }}
source={savedStoryImage}
/>
) : (
<Image
style={{ width: 30, height: 30 }}
source={saveStoryImage}
/>
)}
</TouchableOpacity>
</View>
<View style={styles.body}>
Expand Down

0 comments on commit 5314e98

Please sign in to comment.