1+ import { storage } from './App'
2+
3+ import {
4+ useRecordContext ,
5+ DeleteButton ,
6+ BulkDeleteButton ,
7+ useListContext
8+ } from 'react-admin'
9+
10+
11+ // IMPORTANT COMMENT
12+ // You can't delete the folder because is deletes itself
13+ // when you remove all the elements inside of it
14+ function deleteFileFirestore ( fileURL ) {
15+ let fileRef = storage . refFromURL ( fileURL )
16+
17+ // Delete the file using the delete() method
18+ fileRef . delete ( ) . then ( function ( ) {
19+ // File deleted successfully
20+ console . log ( "File Deleted" )
21+ } ) . catch ( function ( error ) {
22+ // Some Error occurred
23+ console . log ( "An error occured when deleting a file." )
24+ } )
25+ }
26+
27+ const getValueOfAnyWantedKey = ( obj , wantedKey , array ) => {
28+
29+ if ( Array . isArray ( obj ) ) {
30+ for ( let i = 0 ; i < obj . length ; i ++ ) {
31+ getValueOfAnyWantedKey ( obj [ i ] , wantedKey , array )
32+ }
33+ }
34+ else if ( typeof obj === "object" ) {
35+ for ( const key in obj ) {
36+ if ( key === wantedKey ) {
37+ array . push ( obj [ key ] )
38+ delete obj [ key ]
39+ }
40+ getValueOfAnyWantedKey ( obj [ key ] , wantedKey , array )
41+ }
42+ }
43+
44+ return array
45+ }
46+
47+ export const CustomBulkDeleteButton = ( props ) => {
48+ const listContext = useListContext ( ) ;
49+
50+ const filterSelectedIds = ( arr1 , arr2 ) => {
51+ let res = [ ] ;
52+ res = arr1 . filter ( el => {
53+ return arr2 . find ( element => {
54+ return element === el . id ;
55+ } ) ;
56+ } ) ;
57+ return res ;
58+ }
59+
60+ const handleDelete = ( ) => {
61+ const select = listContext . selectedIds
62+
63+ const result = filterSelectedIds ( listContext . data , select )
64+
65+ result . forEach ( function ( el ) {
66+ let sources = [ ]
67+ getValueOfAnyWantedKey ( el , 'src' , sources )
68+ // delete files in storage
69+ for ( const src of sources ) {
70+ deleteFileFirestore ( src )
71+ }
72+ } )
73+ }
74+ return (
75+ < BulkDeleteButton onClick = { handleDelete } { ...props } />
76+ )
77+ }
78+
79+
80+ export const CustomDeleteButton = ( props ) => {
81+ const record = useRecordContext ( ) ;
82+
83+ const handleClick = ( ) => {
84+ const sources = [ ]
85+ getValueOfAnyWantedKey ( record , 'src' , sources )
86+ // delete files in storage
87+ for ( const src of sources ) {
88+ deleteFileFirestore ( src )
89+ }
90+ // then delete in db but if you use DeleteButton there is no use for useDelete
91+ }
92+
93+ return < DeleteButton onClick = { handleClick } { ...props } /> ;
94+ }
95+
96+ export default CustomDeleteButton ;
0 commit comments