Skip to content

Commit

Permalink
Merge pull request #2151 from daostack/bugfix/CW-2051-commons-leave
Browse files Browse the repository at this point in the history
there are spaces and commons that it is impossible to leave #2051
  • Loading branch information
andreymikhadyuk authored Oct 5, 2023
2 parents 827857c + b148e78 commit 6dce881
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,6 @@ const CommonMenu: FC<CommonMenuProps> = (props) => {
onClose={handleMenuClose}
commonId={common.id}
memberCount={common.memberCount}
memberCircleIds={Object.values(currentCommonMember.circles.map)}
/>
)}
<CreateCommonModal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import React, { FC, useCallback, useEffect, useMemo, useState } from "react";
import React, { FC, useCallback, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { useHistory } from "react-router-dom";
import { selectUser } from "@/pages/Auth/store/selectors";
import { leaveCommon } from "@/pages/OldCommon/store/actions";
import { isRequestError } from "@/services/Api";
import { Loader, Modal } from "@/shared/components";
import { ROUTE_PATHS } from "@/shared/constants";
import { ErrorCode, ROUTE_PATHS } from "@/shared/constants";
import { useNotification } from "@/shared/hooks";
import { useCommonMembersWithCircleIdsAmount } from "@/shared/hooks/useCases";
import { ModalProps } from "@/shared/interfaces";
import { emptyFunction } from "@/shared/utils";
import { DeleteCommonRequest } from "./DeleteCommonRequest";
Expand All @@ -19,7 +18,6 @@ interface LeaveCommonModalProps
extends Pick<ModalProps, "isShowing" | "onClose"> {
commonId: string;
memberCount: number;
memberCircleIds: string[];
onSuccessfulLeave?: () => void;
isSubCommon?: boolean;
}
Expand All @@ -30,31 +28,18 @@ const LeaveCommonModal: FC<LeaveCommonModalProps> = (props) => {
onClose,
commonId,
memberCount,
memberCircleIds = [],
onSuccessfulLeave,
isSubCommon = false,
} = props;
const {
loading: areMemberAmountsLoading,
fetched: areMemberAmountsFetched,
data: memberAmountsWithCircleId,
fetchCommonMembersWithCircleIdAmount,
} = useCommonMembersWithCircleIdsAmount();
const dispatch = useDispatch();
const { notify } = useNotification();
const history = useHistory();
const [isLeaving, setIsLeaving] = useState(false);
const [errorText, setErrorText] = useState("");
const [isLastMemberInCircle, setIsLastMemberInCircle] = useState(false);
const user = useSelector(selectUser());
const userId = user?.uid;
const isDeleteCommonRequest = memberCount === 1;
const isLoading = !isDeleteCommonRequest && !areMemberAmountsFetched;
const isLastMemberInCircle = useMemo(
() =>
areMemberAmountsFetched &&
memberAmountsWithCircleId.some(({ amount }) => amount <= 1),
[areMemberAmountsFetched, memberAmountsWithCircleId],
);
const commonWord = isSubCommon ? "space" : "common";

const handleLeave = useCallback(() => {
Expand All @@ -79,6 +64,14 @@ const LeaveCommonModal: FC<LeaveCommonModalProps> = (props) => {
: "";

setIsLeaving(false);
setIsLastMemberInCircle(
Boolean(
isRequestError(error) &&
error.response?.data?.error?.includes(
ErrorCode.LastInCriticalCircle,
),
),
);
setErrorText(errorText);

if (!isFinishedSuccessfully) {
Expand All @@ -96,25 +89,6 @@ const LeaveCommonModal: FC<LeaveCommonModalProps> = (props) => {
);
}, [dispatch, notify, history, commonId, userId]);

useEffect(() => {
if (
isDeleteCommonRequest ||
areMemberAmountsLoading ||
areMemberAmountsFetched
) {
return;
}

fetchCommonMembersWithCircleIdAmount(commonId, memberCircleIds);
}, [
isDeleteCommonRequest,
areMemberAmountsLoading,
areMemberAmountsFetched,
fetchCommonMembersWithCircleIdAmount,
commonId,
memberCircleIds,
]);

const renderStep = () => {
if (isDeleteCommonRequest) {
return (
Expand Down Expand Up @@ -146,7 +120,7 @@ const LeaveCommonModal: FC<LeaveCommonModalProps> = (props) => {
title={`Leave ${commonWord}`}
className="leave-common-modal"
>
{isLoading ? <Loader /> : renderStep()}
{renderStep()}
</Modal>
);
};
Expand Down
1 change: 0 additions & 1 deletion src/pages/common/providers/CommonData/CommonData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ const CommonData: FC<CommonDataProps> = (props) => {
onClose={handleMenuClose}
commonId={common.id}
memberCount={common.memberCount}
memberCircleIds={Object.values(commonMember.circles.map)}
onSuccessfulLeave={
selectedMenuItem === CommonMenuItem.LeaveProject
? handleSuccessfulProjectLeave
Expand Down

0 comments on commit 6dce881

Please sign in to comment.