8
8
Table ,
9
9
ScrollArea ,
10
10
TextInput ,
11
+ Notification ,
11
12
} from "@mantine/core" ;
12
13
import {
13
14
IconDownload ,
@@ -23,6 +24,39 @@ interface PreviewCardProps {
23
24
}
24
25
25
26
function PreviewCard ( { selectedData } : PreviewCardProps ) {
27
+ const [ loading , setLoading ] = useState ( false ) ;
28
+ const [ error , setError ] = useState < string | null > ( null ) ;
29
+ const [ success , setSuccess ] = useState < string | null > ( null ) ;
30
+
31
+ const handleDelete = async ( ) => {
32
+ setLoading ( true )
33
+ setError ( null ) ;
34
+ setSuccess ( null )
35
+ try {
36
+ const response = await fetch ( `${ import . meta. env . VITE_API_URL } /mcaps/${ selectedData ?. id } ` , {
37
+ method : 'DELETE' ,
38
+ } ) ;
39
+
40
+ if ( ! response . ok ) {
41
+ if ( response . status === 503 ) {
42
+ const errorMsg = await response . text ( ) ;
43
+ setError ( `Failed to delete: ${ errorMsg } \nTry again in a few minutes!` ) ;
44
+ } else {
45
+ const errorMsg = await response . text ( ) ;
46
+ setError ( `Failed to delete: ${ errorMsg } ` ) ;
47
+ }
48
+ } else {
49
+ const result = await response . json ( ) ;
50
+ setSuccess ( 'File deleted successfully!' ) ;
51
+ console . log ( 'Delete successful:' , result ) ;
52
+ }
53
+ } catch ( error ) {
54
+ console . error ( 'Error sending Delete request:' , error ) ;
55
+ setError ( 'An error occurred during file deletion.' ) ;
56
+ }
57
+ setLoading ( false )
58
+ }
59
+
26
60
const formatDate = ( dateString : string ) => {
27
61
const date = new Date ( dateString ) ;
28
62
return date . toLocaleDateString ( "en-US" , {
@@ -101,23 +135,37 @@ function PreviewCard({ selectedData }: PreviewCardProps) {
101
135
bottom : 0 ,
102
136
left : 0 ,
103
137
padding : 20 ,
104
- gap : "10px " ,
138
+ gap : "8px " ,
105
139
} }
106
140
>
107
- { selectedData . mcap_files . map ( ( item ) => (
108
- < DownloadButton
109
- buttonText = "MCAP"
110
- fileName = { item . file_name }
111
- signedUrl = { item . signed_url ?? null }
112
- />
113
- ) ) }
114
- { selectedData . mat_files . map ( ( item ) => (
115
- < DownloadButton
116
- buttonText = "MAT"
117
- fileName = { item . file_name }
118
- signedUrl = { item . signed_url }
119
- />
120
- ) ) }
141
+ < div >
142
+ { selectedData . mcap_files . map ( ( item ) => (
143
+ < DownloadButton
144
+ buttonText = "MCAP"
145
+ fileName = { item . file_name }
146
+ signedUrl = { item . signed_url ?? null }
147
+ />
148
+ ) ) }
149
+ { selectedData . mat_files . map ( ( item ) => (
150
+ < DownloadButton
151
+ buttonText = "MAT"
152
+ fileName = { item . file_name }
153
+ signedUrl = { item . signed_url }
154
+ />
155
+ ) ) }
156
+ < Button loading = { loading } loaderProps = { { type : 'dots' } } size = "compact-md" color = "red" onClick = { handleDelete } > Delete</ Button >
157
+ { success && (
158
+ < Notification color = "green" onClose = { ( ) => setSuccess ( null ) } style = { { marginTop : 10 } } >
159
+ { success }
160
+ </ Notification >
161
+ ) }
162
+ { error && (
163
+ < Notification color = "red" onClose = { ( ) => setError ( null ) } style = { { marginTop : 10 } } >
164
+ { error }
165
+ </ Notification >
166
+ ) }
167
+ </ div >
168
+
121
169
</ div >
122
170
</ >
123
171
) : (
0 commit comments