Skip to content

Commit

Permalink
Fix reaction bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
adityapawar1 committed Apr 24, 2024
1 parent 8214d4d commit e3b8139
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 14 deletions.
1 change: 0 additions & 1 deletion src/components/PreviewCard/PreviewCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ function PreviewCard({
(async () => {
const temp = await fetchAllReactionsToStory(storyId);
if (temp != null) {
console.log(temp);
setReactions(temp.filter(r => r != null));
return;
}
Expand Down
6 changes: 3 additions & 3 deletions src/components/ReactionDisplay/ReactionDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const reactionColors: Record<string, string> = {
};

function ReactionDisplay({ reactions, storyId }: ReactionDisplayProps) {
const { subscribe, getPubSubValue } = usePubSub();
const { channels, getPubSubValue } = usePubSub();
const [reactionCount, setReactionCount] = useState(0);

const cleanedReactions = reactions.filter(reaction => reaction != null);
Expand All @@ -34,7 +34,7 @@ function ReactionDisplay({ reactions, storyId }: ReactionDisplayProps) {

useEffect(() => {
setReactionCount(serverReactionCount);
}, []);
}, [reactions]);

useEffect(() => {
const value = getPubSubValue(Channel.REACTIONS, storyId);
Expand All @@ -47,7 +47,7 @@ function ReactionDisplay({ reactions, storyId }: ReactionDisplayProps) {
} else {
setReactionCount(serverReactionCount);
}
}, [subscribe(Channel.REACTIONS, storyId)]);
}, [channels[Channel.REACTIONS][storyId]]);

return (
<View
Expand Down
5 changes: 3 additions & 2 deletions src/components/SaveStoryButton/SaveStoryButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function SaveStoryButton({
const [storyIsSaved, setStoryIsSaved] = useState<boolean | null>(
defaultState,
);
const { publish, subscribe, getPubSubValue } = usePubSub();
const { publish, channels, getPubSubValue } = usePubSub();

useEffect(() => {
if (defaultState != null) {
Expand All @@ -41,7 +41,8 @@ export default function SaveStoryButton({
if (getPubSubValue(Channel.SAVED_STORIES, storyId) != null) {
setStoryIsSaved(getPubSubValue(Channel.SAVED_STORIES, storyId) ?? false);
}
}, [subscribe(Channel.SAVED_STORIES, storyId)]);

}, [channels[Channel.SAVED_STORIES][storyId]]);

const saveStory = async (saved: boolean) => {
setStoryIsSaved(saved);
Expand Down
2 changes: 1 addition & 1 deletion src/queries/reactions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export async function fetchAllReactionsToStory(
`An error occured when trying to fetch reactions to a story', ${error}`,
);
} else {
return data.reactions;
return data[0].reactions;
}
}

Expand Down
8 changes: 1 addition & 7 deletions src/utils/PubSubContext.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { createContext, useContext, useMemo, useState } from 'react';
import React, { createContext, useContext, useState } from 'react';

export enum Channel {
REACTIONS = 'reactions',
Expand All @@ -11,7 +11,6 @@ type channel = Record<number, boolean | undefined>;
export interface PubSubState {
channels: Record<Channel, channel>;
publish: (channel: Channel, id: number, message: boolean) => void;
subscribe: (channel: Channel, id: number) => ReturnType<typeof useMemo>;
getPubSubValue: (channel: Channel, id: number) => boolean | undefined;
}

Expand Down Expand Up @@ -46,18 +45,13 @@ export function BooleanPubSubProvider({
setChannels({ ...channels, [channel]: thisChannel });
};

const subscribe = (channel: Channel, id: number) => {
return useMemo(() => channels[channel][id], [channels[channel][id]]);
};

const getPubSubValue = (channel: Channel, id: number) => {
return channels[channel][id];
};

const authContextValue = {
channels,
publish,
subscribe,
getPubSubValue,
};

Expand Down

0 comments on commit e3b8139

Please sign in to comment.