From 69b9193edb9e93c07294d3e133fbc3844b007860 Mon Sep 17 00:00:00 2001 From: Pavel Meyer Date: Mon, 14 Oct 2024 11:48:52 +0300 Subject: [PATCH] CW-fix-mark-as-read-unread Added cancellation mechanism for mark as read/unread Added instant change of flags --- .../utils/getAllowedItems.ts | 12 +----- .../ChatChannelItem/utils/getAllowedItems.ts | 10 +---- .../useCases/useUpdateFeedItemSeenState.ts | 37 +++++++++++++++++-- 3 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/pages/common/components/DiscussionFeedCard/utils/getAllowedItems.ts b/src/pages/common/components/DiscussionFeedCard/utils/getAllowedItems.ts index 27f3e77cb..ac8573a88 100644 --- a/src/pages/common/components/DiscussionFeedCard/utils/getAllowedItems.ts +++ b/src/pages/common/components/DiscussionFeedCard/utils/getAllowedItems.ts @@ -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) || diff --git a/src/pages/inbox/components/ChatChannelItem/utils/getAllowedItems.ts b/src/pages/inbox/components/ChatChannelItem/utils/getAllowedItems.ts index b2ed935f2..e3dbbb81f 100644 --- a/src/pages/inbox/components/ChatChannelItem/utils/getAllowedItems.ts +++ b/src/pages/inbox/components/ChatChannelItem/utils/getAllowedItems.ts @@ -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) || diff --git a/src/shared/hooks/useCases/useUpdateFeedItemSeenState.ts b/src/shared/hooks/useCases/useUpdateFeedItemSeenState.ts index d63b56d3a..09cd01621 100644 --- a/src/shared/hooks/useCases/useUpdateFeedItemSeenState.ts +++ b/src/shared/hooks/useCases/useUpdateFeedItemSeenState.ts @@ -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, @@ -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(null); const updateSeenState = async ( payload: @@ -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, @@ -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,