1
+ import { useSelector } from "react-redux"
2
+ import { clearUser , selectUser } from "../../../reducers/authSlice"
3
+ import { AlertDialog , AlertDialogBody , AlertDialogContent , AlertDialogFooter , AlertDialogHeader , AlertDialogOverlay , Button , Card , CardBody , Heading , Input , useDisclosure , useToast } from "@chakra-ui/react" ;
4
+ import React , { useState } from "react" ;
5
+ import { deleteUser } from "../../../api/user" ;
6
+ import { useDispatch } from "react-redux" ;
7
+
8
+
9
+ export default function DeleteUserCard ( ) {
10
+ const user = useSelector ( selectUser ) ;
11
+ const [ enteredName , setEnteredName ] = useState ( '' ) ;
12
+ const { isOpen, onOpen, onClose } = useDisclosure ( )
13
+ const cancelRef = React . useRef < HTMLButtonElement > ( null )
14
+ const [ isLoading , setIsLoading ] = useState ( false ) ;
15
+ const dispatch = useDispatch ( )
16
+ const toast = useToast ( ) ;
17
+
18
+ const onConfirmDelete = async ( ) => {
19
+ try {
20
+ setIsLoading ( true ) ;
21
+ const response = await deleteUser ( user ! . id )
22
+ } catch ( error : any ) {
23
+ toast ( {
24
+ title : 'Deletion failed!' ,
25
+ description : error . message ,
26
+ status : 'error'
27
+ } )
28
+ } finally {
29
+ setIsLoading ( false ) ;
30
+ onClose ( ) ;
31
+ dispatch ( clearUser ( ) ) ;
32
+ }
33
+
34
+ }
35
+
36
+
37
+ return (
38
+ < >
39
+ < Card variant = { "elevated" } colorScheme = "red" backgroundColor = "pink" >
40
+ < CardBody >
41
+ < Button w = "100%" colorScheme = "red" onClick = { onOpen } >
42
+ < Heading size = 'sm' > Delete My Account</ Heading >
43
+ </ Button >
44
+ </ CardBody >
45
+ </ Card >
46
+ < AlertDialog
47
+ isOpen = { isOpen }
48
+ leastDestructiveRef = { cancelRef }
49
+ onClose = { onClose }
50
+ >
51
+ < AlertDialogOverlay >
52
+ < AlertDialogContent >
53
+ < AlertDialogHeader fontSize = 'lg' fontWeight = 'bold' >
54
+ Delete Account
55
+ </ AlertDialogHeader >
56
+
57
+ < AlertDialogBody >
58
+ Are you sure? You can't undo this action afterwards.
59
+
60
+ Enter your username to confirm this action.
61
+ < Input type = 'text'
62
+ name = 'username'
63
+ value = { enteredName }
64
+ onChange = { ( e ) => { setEnteredName ( e . target . value ) } }
65
+ autoComplete = { "off" }
66
+ >
67
+ </ Input >
68
+
69
+ </ AlertDialogBody >
70
+
71
+ < AlertDialogFooter >
72
+ < Button ref = { cancelRef } onClick = { onClose } >
73
+ Cancel
74
+ </ Button >
75
+ < Button colorScheme = 'red' onClick = { onConfirmDelete } ml = { 3 }
76
+ isDisabled = { enteredName !== user ?. username } isLoading = { isLoading } >
77
+ Delete
78
+ </ Button >
79
+ </ AlertDialogFooter >
80
+ </ AlertDialogContent >
81
+ </ AlertDialogOverlay >
82
+ </ AlertDialog >
83
+ </ >
84
+ )
85
+
86
+
87
+
88
+ }
0 commit comments