Skip to content

Commit

Permalink
update logic of projects data change on leave or join
Browse files Browse the repository at this point in the history
  • Loading branch information
andreymikhadyuk committed May 14, 2024
1 parent 4074e03 commit c43cc50
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 11 deletions.
3 changes: 0 additions & 3 deletions src/pages/common/providers/CommonData/CommonData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,6 @@ const CommonData: FC<CommonDataProps> = (props) => {
CommonMemberEventEmitter.emit(CommonMemberEvent.Clear, commonMember.id);
}

dispatch(
commonLayoutActions.removeMembershipFromProjectsByRootCommonId(common.id),
);
dispatch(projectsActions.removeMembershipFromProjectAndChildren(common.id));
notify("You’ve successfully left the common");
handleMenuClose();
Expand Down
4 changes: 0 additions & 4 deletions src/pages/commonFeed/CommonFeed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ import { checkIsAutomaticJoin, checkIsProject } from "@/shared/utils";
import {
cacheActions,
commonActions,
commonLayoutActions,
selectCommonAction,
selectFeedSearchValue,
selectIsSearchingFeedItems,
Expand Down Expand Up @@ -452,9 +451,6 @@ const CommonFeedComponent: FC<CommonFeedProps> = (props) => {

useEffect(() => {
if (commonMember && isCommonJoinModalOpen) {
setTimeout(() => {
dispatch(commonLayoutActions.clearProjects());
}, 500);
onCommonJoinModalClose();
}
}, [commonMember?.id]);
Expand Down
11 changes: 11 additions & 0 deletions src/services/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,17 @@ class UserService {

return snapshot.data() || null;
};

public subscribeToUserMemberships = (
userId: string,
callback: (userMemberships: UserMemberships | null) => void,
): UnsubscribeFunction => {
const query = this.getUserMembershipsCollection().doc(userId);

return query.onSnapshot((snapshot) => {
callback(snapshot.data() || null);
});
};
}

export default new UserService();
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export const useProjectsData = (): Return => {
location.pathname,
);
useProjectsSubscription({
currentCommonId,
activeItemId,
areProjectsFetched,
commons,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { useEffect, useState } from "react";
import { useSelector } from "react-redux";
import { useDispatch, useSelector } from "react-redux";
import { usePreviousDistinct } from "react-use";
import { CommonEvent, CommonEventEmitter } from "@/events";
import { selectUser } from "@/pages/Auth/store/selectors";
import { CommonService, GovernanceService, Logger } from "@/services";
import {
CommonService,
GovernanceService,
Logger,
UserService,
} from "@/services";
import { GovernanceActions } from "@/shared/constants";
import { SpaceListVisibility } from "@/shared/interfaces";
import { Common } from "@/shared/models";
import { generateCirclesDataForCommonMember } from "@/shared/utils";
import { ProjectsStateItem } from "@/store/states";
import { commonLayoutActions, ProjectsStateItem } from "@/store/states";

interface Data {
currentCommonId: string | null;
activeItemId: string;
areProjectsFetched: boolean;
commons: ProjectsStateItem[];
Expand Down Expand Up @@ -77,8 +84,21 @@ const getProjectItemFromCommon = async (
};

export const useProjectsSubscription = (data: Data) => {
const { activeItemId, areProjectsFetched, commons, projects } = data;
const {
currentCommonId,
activeItemId,
areProjectsFetched,
commons,
projects,
} = data;
const dispatch = useDispatch();
const [updatedItemsQueue, setUpdatedItemsQueue] = useState<Common[][]>([]);
const [isCurrentCommonMember, setIsCurrentCommonMember] = useState<
boolean | null
>(null);
const previousIsCurrentCommonMember = usePreviousDistinct(
isCurrentCommonMember,
);
const user = useSelector(selectUser());
const userId = user?.uid;
const nextUpdatedItems = updatedItemsQueue[0];
Expand Down Expand Up @@ -151,4 +171,49 @@ export const useProjectsSubscription = (data: Data) => {
}
})();
}, [nextUpdatedItems]);

useEffect(() => {
setIsCurrentCommonMember(null);

if (!userId || !currentCommonId) {
return;
}

const unsubscribe = UserService.subscribeToUserMemberships(
userId,
(userMemberships) => {
setIsCurrentCommonMember(
Boolean(userMemberships && userMemberships.commons[currentCommonId]),
);
},
);

return unsubscribe;
}, [userId, currentCommonId]);

useEffect(() => {
if (
typeof isCurrentCommonMember !== "boolean" ||
typeof previousIsCurrentCommonMember !== "boolean"
) {
return;
}

if (!previousIsCurrentCommonMember && isCurrentCommonMember) {
dispatch(commonLayoutActions.clearProjects());
return;
}

if (
currentCommonId &&
previousIsCurrentCommonMember &&
!isCurrentCommonMember
) {
dispatch(
commonLayoutActions.removeMembershipFromProjectsByRootCommonId(
currentCommonId,
),
);
}
}, [isCurrentCommonMember]);
};
8 changes: 8 additions & 0 deletions src/store/states/commonLayout/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,14 @@ export const reducer = createReducer<CommonLayoutState, Action>(initialState)
return;
}

const common = nextState.commons.find(
({ commonId }) => commonId === rootCommonId,
);

if (common) {
common.hasMembership = false;
}

nextState.projects = nextState.projects.filter(
(project) => project.listVisibility !== SpaceListVisibility.Members,
);
Expand Down

0 comments on commit c43cc50

Please sign in to comment.