Skip to content

Commit

Permalink
add unfollowed items removal
Browse files Browse the repository at this point in the history
  • Loading branch information
andreymikhadyuk committed Nov 7, 2023
1 parent 0d249b1 commit 1161230
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import { DeletePrompt, GlobalOverlay, ReportModal } from "@/shared/components";
import { EntityTypes } from "@/shared/constants";
import { useModal, useNotification } from "@/shared/hooks";
import {
FeedItemFollowState,
useCommon,
useDiscussionById,
useFeedItemFollow,
useFeedItemUserMetadata,
useUserById,
} from "@/shared/hooks/useCases";
Expand Down Expand Up @@ -63,6 +63,7 @@ interface DiscussionFeedCardProps {
getNonAllowedItems?: GetNonAllowedItemsOptions;
onActiveItemDataChange?: (data: FeedLayoutItemChangeData) => void;
directParent?: DirectParent | null;
feedItemFollow: FeedItemFollowState;
onUserSelect?: (userId: string, commonId?: string) => void;
}

Expand All @@ -89,6 +90,7 @@ const DiscussionFeedCard = forwardRef<FeedItemRef, DiscussionFeedCardProps>(
getNonAllowedItems,
onActiveItemDataChange,
directParent,
feedItemFollow,
onUserSelect,
} = props;
const {
Expand Down Expand Up @@ -124,10 +126,6 @@ const DiscussionFeedCard = forwardRef<FeedItemRef, DiscussionFeedCardProps>(
fetchFeedItemUserMetadata,
} = useFeedItemUserMetadata();
const { data: common } = useCommon(isHome ? commonId : "");
const feedItemFollow = useFeedItemFollow(
{ feedItemId: item.id, commonId },
{ withSubscription: true },
);
const menuItems = useMenuItems(
{
commonId,
Expand Down
29 changes: 26 additions & 3 deletions src/pages/common/components/FeedItem/FeedItem.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { forwardRef, memo } from "react";
import React, { forwardRef, memo, useEffect } from "react";
import { useFeedItemFollow } from "@/shared/hooks/useCases";
import { FeedLayoutItemChangeData } from "@/shared/interfaces";
import {
Circles,
Expand Down Expand Up @@ -64,8 +65,17 @@ const FeedItem = forwardRef<FeedItemRef, FeedItemProps>((props, ref) => {
onActiveItemDataChange,
directParent,
} = props;
const { onFeedItemUpdate, getLastMessage, getNonAllowedItems, onUserSelect } =
useFeedItemContext();
const {
onFeedItemUpdate,
onFeedItemUnfollowed,
getLastMessage,
getNonAllowedItems,
onUserSelect,
} = useFeedItemContext();
const feedItemFollow = useFeedItemFollow(
{ feedItemId: item.id, commonId },
{ withSubscription: true },
);
useFeedItemSubscription(item.id, commonId, onFeedItemUpdate);

if (
Expand Down Expand Up @@ -103,9 +113,22 @@ const FeedItem = forwardRef<FeedItemRef, FeedItemProps>((props, ref) => {
isMobileVersion,
onActiveItemDataChange: handleActiveItemDataChange,
directParent,
feedItemFollow,
onUserSelect,
};

useEffect(() => {
if (
feedItemFollow.isUserFeedItemFollowDataFetched &&
!feedItemFollow.userFeedItemFollowData
) {
onFeedItemUnfollowed?.(item.id);
}
}, [
feedItemFollow.isUserFeedItemFollowDataFetched,
feedItemFollow.userFeedItemFollowData,
]);

if (item.data.type === CommonFeedType.Discussion) {
return <DiscussionFeedCard {...generalProps} />;
}
Expand Down
1 change: 1 addition & 0 deletions src/pages/common/components/FeedItem/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export interface FeedItemContextValue {
setExpandedFeedItemId?: (feedItemId: string | null) => void;
renderFeedItemBaseContent?: (props: FeedItemBaseContentProps) => ReactNode;
onFeedItemUpdate?: (item: CommonFeed, isRemoved: boolean) => void;
onFeedItemUnfollowed?: (itemId: string) => void;
feedCardSettings?: FeedCardSettings;
getLastMessage: (options: GetLastMessageOptions) => TextEditorValue;
getNonAllowedItems?: GetNonAllowedItemsOptions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import { DeletePrompt, GlobalOverlay } from "@/shared/components";
import { useRoutesContext } from "@/shared/contexts";
import { useForceUpdate, useModal, useNotification } from "@/shared/hooks";
import {
FeedItemFollowState,
useDiscussionById,
useFeedItemFollow,
useFeedItemUserMetadata,
useProposalById,
useUserById,
Expand Down Expand Up @@ -80,6 +80,7 @@ interface ProposalFeedCardProps {
getLastMessage: (options: GetLastMessageOptions) => TextEditorValue;
getNonAllowedItems?: GetNonAllowedItemsOptions;
isMobileVersion?: boolean;
feedItemFollow: FeedItemFollowState;
onActiveItemDataChange?: (data: FeedLayoutItemChangeData) => void;
onUserSelect?: (userId: string, commonId?: string) => void;
}
Expand All @@ -101,6 +102,7 @@ const ProposalFeedCard = forwardRef<FeedItemRef, ProposalFeedCardProps>(
getLastMessage,
getNonAllowedItems,
isMobileVersion,
feedItemFollow,
onActiveItemDataChange,
onUserSelect,
} = props;
Expand Down Expand Up @@ -175,10 +177,6 @@ const ProposalFeedCard = forwardRef<FeedItemRef, ProposalFeedCardProps>(
onOpen: onShareModalOpen,
onClose: onShareModalClose,
} = useModal(false);
const feedItemFollow = useFeedItemFollow(
{ feedItemId: item.id, commonId },
{ withSubscription: true },
);
const menuItems = useMenuItems(
{
commonId,
Expand Down
4 changes: 4 additions & 0 deletions src/pages/commonFeed/components/FeedLayout/FeedLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ interface FeedLayoutProps {
renderFeedItemBaseContent: (props: FeedItemBaseContentProps) => ReactNode;
renderChatChannelItem?: (props: ChatChannelFeedLayoutItemProps) => ReactNode;
onFeedItemUpdate?: (item: CommonFeed, isRemoved: boolean) => void;
onFeedItemUnfollowed?: (itemId: string) => void;
getLastMessage: (options: GetLastMessageOptions) => TextEditorValue;
sharedFeedItemId?: string | null;
emptyText?: string;
Expand Down Expand Up @@ -160,6 +161,7 @@ const FeedLayout: ForwardRefRenderFunction<FeedLayoutRef, FeedLayoutProps> = (
renderFeedItemBaseContent,
renderChatChannelItem,
onFeedItemUpdate,
onFeedItemUnfollowed,
getLastMessage,
sharedFeedItemId,
emptyText,
Expand Down Expand Up @@ -327,13 +329,15 @@ const FeedLayout: ForwardRefRenderFunction<FeedLayoutRef, FeedLayoutProps> = (
setExpandedFeedItemId,
renderFeedItemBaseContent,
onFeedItemUpdate,
onFeedItemUnfollowed,
getLastMessage,
getNonAllowedItems,
onUserSelect: handleUserWithCommonClick,
}),
[
renderFeedItemBaseContent,
onFeedItemUpdate,
onFeedItemUnfollowed,
getLastMessage,
getNonAllowedItems,
handleUserWithCommonClick,
Expand Down
13 changes: 13 additions & 0 deletions src/pages/inbox/BaseInbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ const InboxPage: FC<InboxPageProps> = (props) => {
[dispatch],
);

const handleFeedItemUnfollowed = useCallback(
(itemId: string) => {
dispatch(
inboxActions.updateFeedItem({
item: { id: itemId },
isRemoved: true,
}),
);
},
[dispatch],
);

const handleActiveItemChange = useCallback(
(activeItemId?: string) => {
dispatch(inboxActions.removeEmptyChatChannelItems(activeItemId));
Expand Down Expand Up @@ -267,6 +279,7 @@ const InboxPage: FC<InboxPageProps> = (props) => {
renderFeedItemBaseContent={renderFeedItemBaseContent}
renderChatChannelItem={renderChatChannelItem}
onFeedItemUpdate={handleFeedItemUpdate}
onFeedItemUnfollowed={handleFeedItemUnfollowed}
getLastMessage={getLastMessage}
emptyText={
isActiveUnreadInboxItemsQueryParam
Expand Down
5 changes: 5 additions & 0 deletions src/shared/hooks/useCases/useFeedItemFollow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import { selectUser } from "@/pages/Auth/store/selectors";
import { FollowFeedItemAction } from "@/shared/constants";
import { FeedItemFollow } from "@/shared/models";
import {
selectCommonFeedFollows,
selectFollowFeedItemMutationState,
Expand All @@ -15,6 +16,8 @@ export interface FeedItemFollowState {
isFollowing: boolean;
isDisabled: boolean;
onFollowToggle: (action?: FollowFeedItemAction) => void;
isUserFeedItemFollowDataFetched: boolean;
userFeedItemFollowData: FeedItemFollow | null;
}

interface Data {
Expand Down Expand Up @@ -105,5 +108,7 @@ export function useFeedItemFollow(
isFollowing,
isDisabled,
onFollowToggle,
isUserFeedItemFollowDataFetched,
userFeedItemFollowData,
};
}

0 comments on commit 1161230

Please sign in to comment.