Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
adityapawar1 committed Apr 18, 2024
1 parent 47b3c11 commit ce4d017
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 58 deletions.
28 changes: 9 additions & 19 deletions src/components/ContentCard/ContentCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,32 +44,22 @@ function ContentCard({
const { channels, initializeChannel, publish } = usePubSub();

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

useEffect(() => {
isStoryInReadingList(storyId, user?.id).then(storyInReadingList => {
setStoryIsSaved(storyInReadingList)
setStoryIsSaved(storyInReadingList);
initializeChannel(storyId);
});
}, [storyId]);

useEffect(() => {
// if another card updates this story, update it here also
if (typeof channels[storyId] !== "undefined") {
if (typeof channels[storyId] !== 'undefined') {
setStoryIsSaved(channels[storyId]);
}
}, [channels[storyId]]);
Expand Down Expand Up @@ -131,9 +121,9 @@ function ContentCard({
</View>
</View>
<TouchableOpacity onPress={() => saveStory(!storyIsSaved)}>
{storyIsSaved ?
savedStoryImageComponent : saveStoryImageComponent
}
{storyIsSaved
? savedStoryImageComponent
: saveStoryImageComponent}
</TouchableOpacity>
</View>
</View>
Expand Down
29 changes: 8 additions & 21 deletions src/components/PreviewCard/PreviewCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,37 +53,26 @@ function PreviewCard({
const { channels, initializeChannel, publish } = usePubSub();

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

useEffect(() => {
isStoryInReadingList(storyId, user?.id).then(storyInReadingList => {
setStoryIsSaved(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])
if (typeof channels[storyId] !== 'undefined') {
setStoryIsSaved(channels[storyId]);
}
}, [channels[storyId]]);


useEffect(() => {
isStoryInReadingList(storyId, user?.id).then(storyInReadingList =>
setStoryIsSaved(storyInReadingList),
Expand All @@ -108,9 +97,7 @@ function PreviewCard({
{title}
</Text>
<TouchableOpacity onPress={() => saveStory(!storyIsSaved)}>
{storyIsSaved ?
savedStoryImageComponent : saveStoryImageComponent
}
{storyIsSaved ? savedStoryImageComponent : saveStoryImageComponent}
</TouchableOpacity>
</View>
<View style={styles.body}>
Expand Down
4 changes: 3 additions & 1 deletion src/queries/savedStories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ async function addUserStory(
if (error) {
if (process.env.NODE_ENV !== 'production') {
throw new Error(
`An error occured when trying to set user saved stories: ${JSON.stringify(error)}`,
`An error occured when trying to set user saved stories: ${JSON.stringify(
error,
)}`,
);
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/utils/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ export interface AuthState {
resendVerification: (email: string) => Promise<AuthResponse>;
resetPassword: (email: string) => Promise<
| {
data: object;
error: null;
}
data: object;
error: null;
}
| {
data: null;
error: AuthError;
}
data: null;
error: AuthError;
}
>;
updateUser: (attributes: UserAttributes) => Promise<UserResponse>;
signOut: () => Promise<void>;
Expand Down
19 changes: 8 additions & 11 deletions src/utils/PubSubContext.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import React, {
createContext,
useContext,
useMemo,
useState,
} from 'react';
import React, { createContext, useContext, useMemo, useState } from 'react';

export interface PubSubState {
channels: Record<number, boolean>;
Expand Down Expand Up @@ -31,17 +26,19 @@ export function BooleanPubSubProvider({
}: {
children: React.ReactNode;
}) {
const [channels, setChannels] = useState<Record<number, boolean | undefined>>({})
const [channels, setChannels] = useState<Record<number, boolean | undefined>>(
{},
);

const initializeChannel = (id: number) => {
if (!(id in channels)) {
setChannels({ ...channels, [id]: undefined })
setChannels({ ...channels, [id]: undefined });
}
}
};

const publish = (id: number, message: boolean) => {
setChannels({ ...channels, [id]: message })
}
setChannels({ ...channels, [id]: message });
};

const authContextValue = useMemo(
() => ({
Expand Down

0 comments on commit ce4d017

Please sign in to comment.