1
- // react
2
1
import React , { useState } from "react" ;
3
- // next
4
2
import { useRouter } from "next/router" ;
3
+ import { mutate } from "swr" ;
4
+ import { useTheme } from "next-themes" ;
5
+ import { Dialog , Transition } from "@headlessui/react" ;
6
+ import { AlertTriangle } from "lucide-react" ;
7
+ // mobx store
8
+ import { useMobxStore } from "lib/mobx/store-provider" ;
9
+ // ui
10
+ import { Button } from "@plane/ui" ;
5
11
// hooks
6
12
import useToast from "hooks/use-toast" ;
7
13
// services
8
14
import { AuthService } from "services/auth.service" ;
9
- // headless ui
10
- import { Dialog , Transition } from "@headlessui/react" ;
11
- // icons
12
- import { Trash2 } from "lucide-react" ;
13
- import { UserService } from "services/user.service" ;
14
- import { useTheme } from "next-themes" ;
15
- import { mutate } from "swr" ;
16
15
17
16
type Props = {
18
17
isOpen : boolean ;
19
18
onClose : ( ) => void ;
20
19
} ;
21
20
22
21
const authService = new AuthService ( ) ;
23
- const userService = new UserService ( ) ;
24
22
25
- const DeleteAccountModal : React . FC < Props > = ( props ) => {
23
+ export const DeactivateAccountModal : React . FC < Props > = ( props ) => {
26
24
const { isOpen, onClose } = props ;
27
- const [ isDeleteLoading , setIsDeleteLoading ] = useState ( false ) ;
25
+
26
+ // states
27
+ const [ switchingAccount , setSwitchingAccount ] = useState ( false ) ;
28
+ const [ isDeactivating , setIsDeactivating ] = useState ( false ) ;
29
+
30
+ const {
31
+ user : { deactivateAccount } ,
32
+ } = useMobxStore ( ) ;
28
33
29
34
const router = useRouter ( ) ;
35
+
30
36
const { setTheme } = useTheme ( ) ;
37
+
31
38
const { setToastAlert } = useToast ( ) ;
32
39
33
- const handleSignOut = async ( ) => {
40
+ const handleClose = ( ) => {
41
+ setSwitchingAccount ( false ) ;
42
+ setIsDeactivating ( false ) ;
43
+ onClose ( ) ;
44
+ } ;
45
+
46
+ const handleSwitchAccount = async ( ) => {
47
+ setSwitchingAccount ( true ) ;
48
+
34
49
await authService
35
50
. signOut ( )
36
51
. then ( ( ) => {
37
52
mutate ( "CURRENT_USER_DETAILS" , null ) ;
38
53
setTheme ( "system" ) ;
39
54
router . push ( "/" ) ;
55
+ handleClose ( ) ;
40
56
} )
41
57
. catch ( ( ) =>
42
58
setToastAlert ( {
43
59
type : "error" ,
44
60
title : "Error!" ,
45
61
message : "Failed to sign out. Please try again." ,
46
62
} )
47
- ) ;
63
+ )
64
+ . finally ( ( ) => setSwitchingAccount ( false ) ) ;
48
65
} ;
49
66
50
67
const handleDeleteAccount = async ( ) => {
51
- setIsDeleteLoading ( true ) ;
52
- await userService
53
- . deleteAccount ( )
68
+ setIsDeactivating ( true ) ;
69
+
70
+ await deactivateAccount ( )
54
71
. then ( ( ) => {
55
72
setToastAlert ( {
56
73
type : "success" ,
57
74
title : "Success!" ,
58
75
message : "Account deleted successfully." ,
59
76
} ) ;
60
- mutate ( "CURRENT_USER_DETAILS" , null ) ;
61
- setTheme ( "system" ) ;
77
+ handleClose ( ) ;
62
78
router . push ( "/" ) ;
63
79
} )
64
80
. catch ( ( err ) =>
65
81
setToastAlert ( {
66
82
type : "error" ,
67
83
title : "Error!" ,
68
- message : err ?. data ?. error ,
84
+ message : err ?. error ,
69
85
} )
70
- ) ;
71
- setIsDeleteLoading ( false ) ;
72
- } ;
73
-
74
- const handleClose = ( ) => {
75
- onClose ( ) ;
86
+ )
87
+ . finally ( ( ) => setIsDeactivating ( false ) ) ;
76
88
} ;
77
89
78
90
return (
@@ -105,32 +117,29 @@ const DeleteAccountModal: React.FC<Props> = (props) => {
105
117
< div className = "px-4 pt-5 pb-4 sm:p-6 sm:pb-4" >
106
118
< div className = "" >
107
119
< div className = "flex items-center gap-x-4" >
108
- < div className = "mx-auto flex h-12 w-12 flex-shrink-0 items-center justify-center rounded-full bg-red-100 sm:mx-0 sm:h-10 sm:w-10 " >
109
- < Trash2 className = "h-5 w-5 text-red-600" aria-hidden = "true" />
120
+ < div className = "grid place- items-center rounded-full bg-red-500/20 p-4 " >
121
+ < AlertTriangle className = "h-6 w-6 text-red-600" aria-hidden = "true" />
110
122
</ div >
111
123
< Dialog . Title as = "h3" className = "text-2xl font-medium leading-6 text-onboarding-text-100" >
112
- Not the right workspace ?
124
+ Deactivate account ?
113
125
</ Dialog . Title >
114
126
</ div >
115
127
116
128
< div className = "mt-6 px-4" >
117
129
< ul className = "text-onboarding-text-300 list-disc font-normal text-base" >
118
- < li > Delete this account if you have another and won’ t use this account.</ li >
119
- < li > Switch to another account if you’ d like to come back to this account another time.</ li >
130
+ < li > Deactivate this account if you have another and won{ "'" } t use this account.</ li >
131
+ < li > Switch to another account if you{ "'" } d like to come back to this account another time.</ li >
120
132
</ ul >
121
133
</ div >
122
134
</ div >
123
135
</ div >
124
- < div className = "flex items-center justify-end gap-4 p-4 mb-2 sm:px-6" >
125
- < span className = "text-sm font-medium hover:cursor-pointer" onClick = { handleSignOut } >
126
- Switch account
127
- </ span >
128
- < button
129
- className = "py-1.5 px-3 font-medium rounded-sm text-red-500 border border-red-500 text-sm "
130
- onClick = { handleDeleteAccount }
131
- >
132
- { isDeleteLoading ? "Deleting..." : "Delete account" }
133
- </ button >
136
+ < div className = "flex items-center justify-end gap-2 p-4 mb-2 sm:px-6" >
137
+ < Button variant = "link-primary" onClick = { handleSwitchAccount } loading = { switchingAccount } >
138
+ { switchingAccount ? "Switching..." : "Switch account" }
139
+ </ Button >
140
+ < Button variant = "outline-danger" onClick = { handleDeleteAccount } >
141
+ { isDeactivating ? "Deactivating..." : "Deactivate account" }
142
+ </ Button >
134
143
</ div >
135
144
</ Dialog . Panel >
136
145
</ Transition . Child >
@@ -140,5 +149,3 @@ const DeleteAccountModal: React.FC<Props> = (props) => {
140
149
</ Transition . Root >
141
150
) ;
142
151
} ;
143
-
144
- export default DeleteAccountModal ;
0 commit comments