Skip to content

Commit

Permalink
fix membership modals showing based on global data fetched flag
Browse files Browse the repository at this point in the history
  • Loading branch information
andreymikhadyuk committed Sep 19, 2023
1 parent e663da4 commit 6d9df4c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/pages/commonFeed/CommonFeed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ const CommonFeedComponent: FC<CommonFeedProps> = (props) => {
rootCommonId: commonData?.common.rootCommonId,
parentCommonId,
governanceCircles: commonData?.governance.circles,
rootCommonGovernanceCircles: commonData?.rootCommonGovernance?.circles,
});
const {
data: commonPinnedFeedItems,
Expand Down Expand Up @@ -479,14 +480,14 @@ const CommonFeedComponent: FC<CommonFeedProps> = (props) => {
{commonData.common && commonData.governance && (
<>
<MembershipRequestModal
isShowing={isCommonJoinModalOpen}
isShowing={isGlobalDataFetched && isCommonJoinModalOpen}
onClose={onCommonJoinModalClose}
common={commonData.common}
governance={commonData.governance}
showLoadingAfterSuccessfulCreation
/>
<JoinProjectModal
isShowing={isProjectJoinModalOpen}
isShowing={isGlobalDataFetched && isProjectJoinModalOpen}
onClose={onProjectJoinModalClose}
common={commonData.common}
governance={commonData.governance}
Expand All @@ -496,7 +497,7 @@ const CommonFeedComponent: FC<CommonFeedProps> = (props) => {
)}
{commonData.rootCommon && commonData.rootCommonGovernance && (
<MembershipRequestModal
isShowing={isRootCommonJoinModalOpen}
isShowing={isGlobalDataFetched && isRootCommonJoinModalOpen}
onClose={onRootCommonJoinModalClose}
common={commonData.rootCommon}
governance={commonData.rootCommonGovernance}
Expand Down
30 changes: 27 additions & 3 deletions src/pages/commonFeed/hooks/useGlobalCommonData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback } from "react";
import { useCallback, useEffect } from "react";
import { useCommonMember } from "@/pages/OldCommon/hooks";
import { LoadingState } from "@/shared/interfaces";
import { Circles, CirclesPermissions, CommonMember } from "@/shared/models";
Expand All @@ -8,6 +8,7 @@ interface UseGlobalCommonDataArguments {
rootCommonId?: string;
parentCommonId?: string;
governanceCircles?: Circles;
rootCommonGovernanceCircles?: Circles;
}

type State = LoadingState<{
Expand All @@ -23,7 +24,13 @@ interface Return extends State {
export const useGlobalCommonData = (
data: UseGlobalCommonDataArguments,
): Return => {
const { commonId, rootCommonId, parentCommonId, governanceCircles } = data;
const {
commonId,
rootCommonId,
parentCommonId,
governanceCircles,
rootCommonGovernanceCircles,
} = data;
const {
loading: isCommonMemberLoading,
fetched: isCommonMemberFetched,
Expand All @@ -43,6 +50,9 @@ export const useGlobalCommonData = (
setCommonMember: setRootCommonMember,
} = useCommonMember({
shouldAutoReset: false,
withSubscription: true,
commonId: rootCommonId,
governanceCircles: rootCommonGovernanceCircles,
});
const {
loading: isParentCommonMemberLoading,
Expand Down Expand Up @@ -71,12 +81,20 @@ export const useGlobalCommonData = (
}, [rootCommonId, fetchRootCommonMember, setRootCommonMember]);

const fetchParentCommonMemberData = useCallback(() => {
if (parentCommonId && rootCommonId === parentCommonId) {
return;
}
if (parentCommonId) {
fetchParentCommonMember(parentCommonId, {}, true);
} else {
setParentCommonMember(null);
}
}, [parentCommonId, fetchParentCommonMember, setParentCommonMember]);
}, [
rootCommonId,
parentCommonId,
fetchParentCommonMember,
setParentCommonMember,
]);

const fetchUserRelatedData = useCallback(() => {
fetchCommonMember(commonId, {}, true);
Expand All @@ -89,6 +107,12 @@ export const useGlobalCommonData = (
fetchParentCommonMemberData,
]);

useEffect(() => {
if (rootCommonId === parentCommonId) {
setParentCommonMember(rootCommonMember);
}
}, [rootCommonMember]);

return {
loading: isGlobalDataLoading,
fetched: isGlobalDataFetched,
Expand Down

0 comments on commit 6d9df4c

Please sign in to comment.