1
- import React , { useState } from 'react' ;
2
- import { Button } from '../ui/button' ;
3
1
import { Trash2 } from 'lucide-react' ;
4
- import { api } from '~/utils/api' ;
5
2
import { useRouter } from 'next/router' ;
6
- import {
7
- AlertDialog ,
8
- AlertDialogAction ,
9
- AlertDialogCancel ,
10
- AlertDialogContent ,
11
- AlertDialogDescription ,
12
- AlertDialogFooter ,
13
- AlertDialogHeader ,
14
- AlertDialogTitle ,
15
- AlertDialogTrigger ,
16
- } from '../ui/alert-dialog' ;
3
+ import React from 'react' ;
17
4
import { toast } from 'sonner' ;
5
+ import { api } from '~/utils/api' ;
6
+ import { Button } from '../ui/button' ;
7
+ import { SimpleConfirmationDialog } from '../ui/SimpleConfirmationDialog' ;
18
8
19
9
export const DeleteFriend : React . FC < {
20
10
friendId : number ;
21
11
disabled : boolean ;
22
12
} > = ( { friendId, disabled } ) => {
23
13
const router = useRouter ( ) ;
24
- const [ showTrigger , setShowTrigger ] = useState ( false ) ;
25
14
26
15
const deleteFriendMutation = api . user . deleteFriend . useMutation ( ) ;
27
16
const utils = api . useUtils ( ) ;
@@ -33,48 +22,27 @@ export const DeleteFriend: React.FC<{
33
22
toast . error ( 'Failed to delete user' ) ;
34
23
return ;
35
24
}
36
- setShowTrigger ( false ) ;
37
25
utils . user . getBalances . invalidate ( ) . catch ( console . error ) ;
38
26
39
27
await router . replace ( `/balances` ) ;
40
28
} ;
41
29
42
30
return (
43
- < div >
44
- < AlertDialog
45
- open = { showTrigger }
46
- onOpenChange = { ( status ) => ( status !== showTrigger ? setShowTrigger ( status ) : null ) }
47
- >
48
- < AlertDialogTrigger asChild >
49
- < Button variant = "ghost" className = "px-0" >
50
- < Trash2 className = "text-red-500" size = { 20 } />
51
- </ Button >
52
- </ AlertDialogTrigger >
53
- < AlertDialogContent className = "max-w-xs rounded-lg" >
54
- < AlertDialogHeader >
55
- < AlertDialogTitle > { disabled ? '' : 'Are you absolutely sure?' } </ AlertDialogTitle >
56
- < AlertDialogDescription >
57
- { disabled
58
- ? "Can't remove friend with outstanding balances. Settle up first"
59
- : 'Do you really want to continue' }
60
- </ AlertDialogDescription >
61
- </ AlertDialogHeader >
62
- < AlertDialogFooter >
63
- < AlertDialogCancel > Cancel</ AlertDialogCancel >
64
- { ! disabled ? (
65
- < Button
66
- size = "sm"
67
- variant = "destructive"
68
- onClick = { onDeleteFriend }
69
- disabled = { deleteFriendMutation . isLoading }
70
- loading = { deleteFriendMutation . isLoading }
71
- >
72
- Delete
73
- </ Button >
74
- ) : null }
75
- </ AlertDialogFooter >
76
- </ AlertDialogContent >
77
- </ AlertDialog >
78
- </ div >
31
+ < SimpleConfirmationDialog
32
+ title = { disabled ? '' : 'Are you absolutely sure?' }
33
+ description = {
34
+ disabled
35
+ ? "Can't remove friend with outstanding balances. Settle up first"
36
+ : 'Do you really want to continue'
37
+ }
38
+ hasPermission = { ! disabled }
39
+ onConfirm = { onDeleteFriend }
40
+ loading = { deleteFriendMutation . isLoading }
41
+ variant = "destructive"
42
+ >
43
+ < Button variant = "ghost" className = "px-0" >
44
+ < Trash2 className = "text-red-500" size = { 20 } />
45
+ </ Button >
46
+ </ SimpleConfirmationDialog >
79
47
) ;
80
48
} ;
0 commit comments