1
1
import { getUiState } from '@bearstudio/ui-state' ;
2
2
import { ORPCError } from '@orpc/client' ;
3
- import { useQuery } from '@tanstack/react-query' ;
4
- import { Link } from '@tanstack/react-router' ;
3
+ import { useQuery , useQueryClient } from '@tanstack/react-query' ;
4
+ import { Link , useRouter } from '@tanstack/react-router' ;
5
+ import { useCanGoBack } from '@tanstack/react-router' ;
5
6
import { AlertCircleIcon , PencilLineIcon , Trash2Icon } from 'lucide-react' ;
7
+ import { toast } from 'sonner' ;
6
8
7
9
import { orpc } from '@/lib/orpc/client' ;
8
10
9
11
import { BackButton } from '@/components/back-button' ;
10
12
import { PageError } from '@/components/page-error' ;
11
13
import { Button } from '@/components/ui/button' ;
12
14
import { Card , CardContent } from '@/components/ui/card' ;
15
+ import { ConfirmResponsiveDrawer } from '@/components/ui/confirm-responsive-drawer' ;
13
16
import { ResponsiveIconButton } from '@/components/ui/responsive-icon-button' ;
14
17
import { Skeleton } from '@/components/ui/skeleton' ;
15
18
import { Spinner } from '@/components/ui/spinner' ;
16
19
20
+ import { WithPermissions } from '@/features/auth/with-permission' ;
17
21
import { BookCover } from '@/features/book/book-cover' ;
18
22
import {
19
23
PageLayout ,
@@ -23,6 +27,9 @@ import {
23
27
} from '@/layout/manager/page-layout' ;
24
28
25
29
export const PageBook = ( props : { params : { id : string } } ) => {
30
+ const queryClient = useQueryClient ( ) ;
31
+ const router = useRouter ( ) ;
32
+ const canGoBack = useCanGoBack ( ) ;
26
33
const bookQuery = useQuery (
27
34
orpc . book . getById . queryOptions ( { input : { id : props . params . id } } )
28
35
) ;
@@ -39,15 +46,65 @@ export const PageBook = (props: { params: { id: string } }) => {
39
46
return set ( 'default' , { book : bookQuery . data } ) ;
40
47
} ) ;
41
48
49
+ const deleteBook = async ( ) => {
50
+ try {
51
+ await orpc . book . deleteById . call ( { id : props . params . id } ) ;
52
+ await Promise . all ( [
53
+ // Invalidate books list
54
+ queryClient . invalidateQueries ( {
55
+ queryKey : orpc . book . getAll . key ( ) ,
56
+ type : 'all' ,
57
+ } ) ,
58
+ // Remove user from cache
59
+ queryClient . removeQueries ( {
60
+ queryKey : orpc . book . getById . key ( { input : { id : props . params . id } } ) ,
61
+ } ) ,
62
+ ] ) ;
63
+
64
+ toast . success ( 'Book deleted' ) ;
65
+
66
+ // Redirect
67
+ if ( canGoBack ) {
68
+ router . history . back ( ) ;
69
+ } else {
70
+ router . navigate ( { to : '..' , replace : true } ) ;
71
+ }
72
+ } catch {
73
+ toast . error ( 'Failed to book the user' ) ;
74
+ }
75
+ } ;
76
+
42
77
return (
43
78
< PageLayout >
44
79
< PageLayoutTopBar
45
80
backButton = { < BackButton /> }
46
81
actions = {
47
82
< >
48
- < ResponsiveIconButton variant = "ghost" label = "Delete" >
49
- < Trash2Icon />
50
- </ ResponsiveIconButton >
83
+ < WithPermissions
84
+ permissions = { [
85
+ {
86
+ book : [ 'delete' ] ,
87
+ } ,
88
+ ] }
89
+ >
90
+ < ConfirmResponsiveDrawer
91
+ onConfirm = { ( ) => deleteBook ( ) }
92
+ title = { `Delete ${ bookQuery . data ?. title } ` }
93
+ description = {
94
+ < >
95
+ You are about to permanently delete this book.{ ' ' }
96
+ < strong > This action cannot be undone.</ strong > Please
97
+ confirm your decision carefully.
98
+ </ >
99
+ }
100
+ confirmText = "Delete"
101
+ confirmVariant = "destructive"
102
+ >
103
+ < ResponsiveIconButton variant = "ghost" label = "Delete" size = "sm" >
104
+ < Trash2Icon />
105
+ </ ResponsiveIconButton >
106
+ </ ConfirmResponsiveDrawer >
107
+ </ WithPermissions >
51
108
< Button asChild size = "sm" variant = "secondary" >
52
109
< Link
53
110
to = "/manager/books/$id/update"
0 commit comments