Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 2918e8d

Browse files
committed
feat: current user "leaves" team instead of remove
ref issue #14
1 parent fe479df commit 2918e8d

File tree

4 files changed

+68
-36
lines changed

4 files changed

+68
-36
lines changed

src/components/BaseModal/styles.module.scss

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
font-weight: normal;
66
font-size: 34px;
77
line-height: 40px;
8-
margin: 0 0 37px 0;
8+
margin: 0 0 24px 0;
99
overflow-wrap: anywhere;
1010
padding: 0;
1111
text-transform: uppercase;
1212
}
1313

1414
.content {
1515
@include font-roboto;
16+
line-height: 140%;
1617
}
1718

1819
.button-group {

src/constants/permissions.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export const PERMISSIONS = {
9494
UPDATE_JOB_CANDIDATE: {
9595
meta: {
9696
group: "Job Candidate",
97-
title: 'Update Job Candidate',
97+
title: "Update Job Candidate",
9898
},
9999
projectRoles: true,
100100
topcoderRoles: [TOPCODER_ROLE.BOOKING_MANAGER, TOPCODER_ROLE.ADMINISTRATOR],

src/hoc/withAuthentication/index.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ import { useParams } from "@reach/router";
2828
export default function withAuthentication(Component) {
2929
const AuthenticatedComponent = (props) => {
3030
const dispatch = useDispatch();
31-
const { isLoggedIn, authError, teamId, teamMembersLoaded, teamMembersLoadingError } = useSelector(
32-
(state) => state.authUser
33-
);
31+
const {
32+
isLoggedIn,
33+
authError,
34+
teamId,
35+
teamMembersLoaded,
36+
teamMembersLoadingError,
37+
} = useSelector((state) => state.authUser);
3438
const params = useParams();
3539

3640
/*
@@ -87,10 +91,15 @@ export default function withAuthentication(Component) {
8791
{/* Show loading indicator until we know if user is logged-in or no.
8892
Also, show loading indicator if we need to know team members but haven't loaded them yet.
8993
In we got error during this process, show error */}
90-
{isLoggedIn === null || (params.teamId && !teamMembersLoaded) && <LoadingIndicator error={authError || teamMembersLoadingError} />}
94+
{isLoggedIn === null ||
95+
(params.teamId && !teamMembersLoaded && (
96+
<LoadingIndicator error={authError || teamMembersLoadingError} />
97+
))}
9198

9299
{/* Show component only if user is logged-in and if we don't need team members or we already loaded them */}
93-
{isLoggedIn === true && (!params.teamId || teamMembersLoaded) ? <Component {...props} /> : null}
100+
{isLoggedIn === true && (!params.teamId || teamMembersLoaded) ? (
101+
<Component {...props} />
102+
) : null}
94103
</>
95104
);
96105
};

src/routes/TeamAccess/components/DeleteModal/index.jsx

+51-29
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,50 @@
1-
import React, { useCallback, useState } from "react";
2-
import { useDispatch } from "react-redux";
1+
import React, { useCallback, useMemo, useState } from "react";
2+
import { useDispatch, useSelector } from "react-redux";
33
import { toastr } from "react-redux-toastr";
44
import BaseModal from "components/BaseModal";
55
import Button from "components/Button";
66
import { removeTeamMember } from "../../actions";
77
import CenteredSpinner from "components/CenteredSpinner";
88

9-
const MEMBER_TITLE = "You're about to delete a member from the team";
9+
const getTitle = (isSelf) =>
10+
isSelf
11+
? "You're about to leave the team"
12+
: "You're about to delete a member from the team";
1013

11-
const DELETE_MEMBER_TITLE = "Deleting Member...";
14+
const getDeletingTitle = (isSelf) =>
15+
isSelf ? "Leaving Team..." : "Deleting Member...";
16+
17+
const getText = (isSelf, userHandleOrEmail) =>
18+
isSelf
19+
? `You are about to remove yourself from the team. You might lose
20+
access to the team and couldn't see or interact with it anymore. Do
21+
you still want to leave the team?`
22+
: `You are about to remove ${userHandleOrEmail} from your team. They might lose all
23+
access to the team and couldn't see or interact with it anymore. Do
24+
you still want to remove the member?`;
25+
26+
const getSuccessTitle = (isSelf) =>
27+
isSelf ? "Team Left" : "Member Removed";
28+
29+
const getSuccessText = (isSelf, userHandleOrEmail) =>
30+
isSelf ? "You have successfully left the team" : `You have successfully removed ${userHandleOrEmail} from the team`;
31+
32+
const getFailedTitle = (isSelf) =>
33+
isSelf ? "Failed to Leave the Team" : "Failed to Remove Member";
34+
35+
const getDeleteButtonText = (isSelf) =>
36+
isSelf ? "Leave Team" : "Remove Member";
1237

1338
function DeleteModal({ selected, open, onClose, teamId }) {
1439
const [loading, setLoading] = useState(false);
40+
const { userId } = useSelector((state) => state.authUser);
1541

16-
let handle;
17-
if (selected) {
18-
if (selected.handle) {
19-
handle = selected.handle;
20-
} else {
21-
handle = selected.email;
42+
const isSelf = useMemo(() => selected && selected.userId === userId, [selected, userId]);
43+
const userHandleOrEmail = useMemo(() => {
44+
if (selected) {
45+
return selected.handle ? selected.handle : selected.email;
2246
}
23-
}
47+
}, [selected]);
2448

2549
const dispatch = useDispatch();
2650

@@ -30,47 +54,45 @@ function DeleteModal({ selected, open, onClose, teamId }) {
3054
.then(() => {
3155
setLoading(false);
3256
toastr.success(
33-
"Member Removed",
34-
`You have successfully removed ${handle} from the team`
57+
getSuccessTitle(isSelf),
58+
getSuccessText(isSelf, userHandleOrEmail)
3559
);
3660
onClose();
61+
if (isSelf) {
62+
// redirect to the team list after leaving the team just in case if the current user lost access
63+
window.history.pushState({}, null, `/taas/myteams`);
64+
}
3765
})
3866
.catch((err) => {
3967
setLoading(false);
40-
toastr.error("Failed to Remove Member", err.message);
68+
toastr.error(getFailedTitle(isSelf), err.message);
4169
});
42-
}, [dispatch, selected]);
43-
44-
const displayText = useCallback(() => {
45-
return (
46-
"You are about to remove " +
47-
handle +
48-
" from your team. They will lose all rights to the project " +
49-
"and can't see or interact with it anymore. Do you still " +
50-
"want to remove the member?"
51-
);
52-
}, [selected]);
70+
}, [dispatch, selected, isSelf]);
5371

5472
const button = (
5573
<Button
5674
type="warning"
5775
size="medium"
58-
onClick={() => deleteMember()}
76+
onClick={deleteMember}
5977
disabled={loading}
6078
>
61-
Remove member
79+
{getDeleteButtonText(isSelf)}
6280
</Button>
6381
);
6482

6583
return (
6684
<BaseModal
6785
open={open}
6886
onClose={onClose}
69-
title={loading ? DELETE_MEMBER_TITLE : MEMBER_TITLE}
87+
title={loading ? getDeletingTitle(isSelf) : getTitle(isSelf)}
7088
button={button}
7189
disabled={loading}
7290
>
73-
{loading ? <CenteredSpinner /> : <p>{displayText()}</p>}
91+
{loading ? (
92+
<CenteredSpinner />
93+
) : (
94+
<p>{getText(isSelf, userHandleOrEmail)}</p>
95+
)}
7496
</BaseModal>
7597
);
7698
}

0 commit comments

Comments
 (0)