Skip to content

Commit

Permalink
CW-fix-mark-as-read-unread
Browse files Browse the repository at this point in the history
Added cancellation mechanism for mark as read/unread
Added instant change of flags
  • Loading branch information
MeyerPV committed Oct 14, 2024
1 parent 4a18934 commit 69b9193
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,18 @@ const MENU_ITEM_TO_CHECK_FUNCTION_MAP: Record<
);
},
[FeedItemMenuItem.MarkUnread]: ({ feedItemUserMetadata }) => {
const { count, seen, isSeenUpdating } = feedItemUserMetadata || {};
const { count, seen } = feedItemUserMetadata || {};

if (!feedItemUserMetadata) {
return true;
}

if (isSeenUpdating) {
return false;
}

return notEmpty(count) && notEmpty(seen) && count === 0 && seen;
},
[FeedItemMenuItem.MarkRead]: ({ feedItemUserMetadata }) => {
const { count, seenOnce, seen, isSeenUpdating } =
const { count, seenOnce, seen } =
feedItemUserMetadata || {};

if (isSeenUpdating) {
return false;
}

return (
Boolean(count) ||
(notEmpty(seen) && !seen) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,17 @@ const MENU_ITEM_TO_CHECK_FUNCTION_MAP: Record<
(options: Options) => boolean
> = {
[ChatChannelMenuItem.MarkUnread]: ({ chatChannelUserStatus }) => {
const { notSeenCount, seen, isSeenUpdating } = chatChannelUserStatus || {};
const { notSeenCount, seen } = chatChannelUserStatus || {};

if (isSeenUpdating) {
return false;
}

return (
notEmpty(notSeenCount) && notEmpty(seen) && notSeenCount === 0 && seen
);
},
[ChatChannelMenuItem.MarkRead]: ({ chatChannelUserStatus }) => {
const { notSeenCount, seenOnce, seen, isSeenUpdating } =
const { notSeenCount, seenOnce, seen } =
chatChannelUserStatus || {};

if (isSeenUpdating) {
return false;
}

return (
Boolean(notSeenCount) ||
Expand Down
37 changes: 33 additions & 4 deletions src/shared/hooks/useCases/useUpdateFeedItemSeenState.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { useCallback } from "react";
import { useCallback, useRef } from "react";
import { useDispatch, useSelector } from "react-redux";
import { selectUser } from "@/pages/Auth/store/selectors";
import { CommonFeedService } from "@/services";
import { getFeedItemUserMetadataKey } from "@/shared/constants";
import axios, { CancelTokenSource } from "axios";
import {
MarkCommonFeedItemAsSeenPayload,
MarkCommonFeedItemAsUnseenPayload,
Expand All @@ -21,6 +22,9 @@ export const useUpdateFeedItemSeenState = (): Return => {
const dispatch = useDispatch();
const user = useSelector(selectUser());
const userId = user?.uid;

// Ref to store the current CancelTokenSource
const cancelTokenRef = useRef<CancelTokenSource | null>(null);

const updateSeenState = async (
payload:
Expand All @@ -32,6 +36,15 @@ export const useUpdateFeedItemSeenState = (): Return => {
return;
}

// Cancel the previous request if it exists
if (cancelTokenRef.current) {
cancelTokenRef.current.cancel("Operation canceled due to a new request.");
}

// Create a new CancelToken for the current request
const cancelTokenSource = axios.CancelToken.source();
cancelTokenRef.current = cancelTokenSource;

const { commonId, feedObjectId } = payload;
const key = getFeedItemUserMetadataKey({
commonId,
Expand All @@ -49,11 +62,27 @@ export const useUpdateFeedItemSeenState = (): Return => {
);

if (newSeenValue) {
await CommonFeedService.markCommonFeedItemAsSeen(payload);
await CommonFeedService.markCommonFeedItemAsSeen(payload, {
cancelToken: cancelTokenSource.token,
});
} else {
await CommonFeedService.markCommonFeedItemAsUnseen(payload);
await CommonFeedService.markCommonFeedItemAsUnseen(payload, {
cancelToken: cancelTokenSource.token,
});
}
} catch (error) {
} catch (error: unknown) {
if (error instanceof Error) {
if (error.name === 'AbortError') {
console.log('Request was aborted');
return;
}

// Handle other types of errors here, like logging or displaying a message
console.error("An error occurred:", error.message);
} else {
console.error("An unknown error occurred");
}

dispatch(
cacheActions.updateFeedItemUserSeenState({
key,
Expand Down

0 comments on commit 69b9193

Please sign in to comment.