Skip to content

Commit

Permalink
move fetching parent common member logic to the useCommonData hook
Browse files Browse the repository at this point in the history
  • Loading branch information
budnik9 committed Sep 13, 2023
1 parent 03278bf commit 6f949b0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
21 changes: 4 additions & 17 deletions src/pages/commonFeed/CommonFeed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import {
selectRecentStreamId,
selectSharedFeedItem,
} from "@/store/states";
import { useCommonMember } from "../OldCommon/hooks";
import {
NewDiscussionCreation,
NewProposalCreation,
Expand Down Expand Up @@ -99,6 +98,7 @@ const CommonFeedComponent: FC<CommonFeedProps> = (props) => {
fetchCommonData,
} = useCommonData(userId);
const parentCommonId = commonData?.common.directParent?.commonId;
const parentCommonMember = commonData?.parentCommonMember;
const isRootCommon = !parentCommonId;
const isRootCommonMember = Boolean(commonData?.rootCommonMember);
const anotherCommonId =
Expand All @@ -121,11 +121,6 @@ const CommonFeedComponent: FC<CommonFeedProps> = (props) => {
loading: areCommonPinnedFeedItemsLoading,
fetch: fetchCommonPinnedFeedItems,
} = useCommonPinnedFeedItems(commonId, pinnedItemIds);
const {
data: parentCommonMember,
fetched: isParentCommonMemberFetched,
fetchCommonMember: fetchParentCommonMember,
} = useCommonMember();

const commonFeedItemIdsForNotListening = useMemo(() => {
const items: string[] = [];
Expand Down Expand Up @@ -165,9 +160,7 @@ const CommonFeedComponent: FC<CommonFeedProps> = (props) => {
return items;
}, [sharedFeedItem, sharedFeedItemId, commonPinnedFeedItems]);
const firstItem = commonFeedItems?.[0];
const isDataFetched =
isCommonDataFetched &&
(parentCommonId ? isParentCommonMemberFetched : true);
const isDataFetched = isCommonDataFetched;
const hasPublicItem = true;

const fetchData = () => {
Expand Down Expand Up @@ -202,16 +195,10 @@ const CommonFeedComponent: FC<CommonFeedProps> = (props) => {
);

useEffect(() => {
if (parentCommonId) {
fetchParentCommonMember(parentCommonId, {}, true);
}
}, [parentCommonId]);

useEffect(() => {
if (isParentCommonMemberFetched && !parentCommonMember) {
if (isCommonDataFetched && parentCommonId && !parentCommonMember) {
history.replace(getCommonPageAboutTabPath(commonId));
}
}, [parentCommonMember?.id, isParentCommonMemberFetched, commonId]);
}, [isCommonDataFetched, parentCommonMember?.id, commonId]);

useEffect(() => {
if (!isCommonDataFetched || !isGlobalDataFetched || commonMember) {
Expand Down
27 changes: 19 additions & 8 deletions src/pages/commonFeed/hooks/useCommonData/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,24 @@ export const useCommonData = (userId?: string): Return => {
}

const rootCommonId = common.directParent?.commonId;
const [parentCommons, subCommons, rootCommonMember] =
await Promise.all([
CommonService.getAllParentCommonsForCommon(common),
CommonService.getCommonsByDirectParentIds([common.id]),
rootCommonId && userId
? CommonService.getCommonMemberByUserId(rootCommonId, userId)
: null,
]);
const [
parentCommons,
subCommons,
rootCommonMember,
parentCommonMember,
] = await Promise.all([
CommonService.getAllParentCommonsForCommon(common),
CommonService.getCommonsByDirectParentIds([common.id]),
rootCommonId && userId
? CommonService.getCommonMemberByUserId(rootCommonId, userId)
: null,
common.directParent?.commonId && userId
? CommonService.getCommonMemberByUserId(
common.directParent.commonId,
userId,
)
: null,
]);

setState({
loading: false,
Expand All @@ -85,6 +95,7 @@ export const useCommonData = (userId?: string): Return => {
commonMembersAmount,
sharedFeedItem,
rootCommonMember,
parentCommonMember,
parentCommon: last(parentCommons),
},
});
Expand Down
1 change: 1 addition & 0 deletions src/pages/commonFeed/hooks/useCommonData/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface Data {
commonMembersAmount: number;
sharedFeedItem: CommonFeed | null;
rootCommonMember: CommonMember | null;
parentCommonMember: CommonMember | null;
parentCommon?: Common;
}

Expand Down

0 comments on commit 6f949b0

Please sign in to comment.