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" ;
3
3
import { toastr } from "react-redux-toastr" ;
4
4
import BaseModal from "components/BaseModal" ;
5
5
import Button from "components/Button" ;
6
6
import { removeTeamMember } from "../../actions" ;
7
7
import CenteredSpinner from "components/CenteredSpinner" ;
8
8
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" ;
10
13
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" ;
12
37
13
38
function DeleteModal ( { selected, open, onClose, teamId } ) {
14
39
const [ loading , setLoading ] = useState ( false ) ;
40
+ const { userId } = useSelector ( ( state ) => state . authUser ) ;
15
41
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 ;
22
46
}
23
- }
47
+ } , [ selected ] ) ;
24
48
25
49
const dispatch = useDispatch ( ) ;
26
50
@@ -30,47 +54,45 @@ function DeleteModal({ selected, open, onClose, teamId }) {
30
54
. then ( ( ) => {
31
55
setLoading ( false ) ;
32
56
toastr . success (
33
- "Member Removed" ,
34
- `You have successfully removed ${ handle } from the team`
57
+ getSuccessTitle ( isSelf ) ,
58
+ getSuccessText ( isSelf , userHandleOrEmail )
35
59
) ;
36
60
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
+ }
37
65
} )
38
66
. catch ( ( err ) => {
39
67
setLoading ( false ) ;
40
- toastr . error ( "Failed to Remove Member" , err . message ) ;
68
+ toastr . error ( getFailedTitle ( isSelf ) , err . message ) ;
41
69
} ) ;
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 ] ) ;
53
71
54
72
const button = (
55
73
< Button
56
74
type = "warning"
57
75
size = "medium"
58
- onClick = { ( ) => deleteMember ( ) }
76
+ onClick = { deleteMember }
59
77
disabled = { loading }
60
78
>
61
- Remove member
79
+ { getDeleteButtonText ( isSelf ) }
62
80
</ Button >
63
81
) ;
64
82
65
83
return (
66
84
< BaseModal
67
85
open = { open }
68
86
onClose = { onClose }
69
- title = { loading ? DELETE_MEMBER_TITLE : MEMBER_TITLE }
87
+ title = { loading ? getDeletingTitle ( isSelf ) : getTitle ( isSelf ) }
70
88
button = { button }
71
89
disabled = { loading }
72
90
>
73
- { loading ? < CenteredSpinner /> : < p > { displayText ( ) } </ p > }
91
+ { loading ? (
92
+ < CenteredSpinner />
93
+ ) : (
94
+ < p > { getText ( isSelf , userHandleOrEmail ) } </ p >
95
+ ) }
74
96
</ BaseModal >
75
97
) ;
76
98
}
0 commit comments