File tree Expand file tree Collapse file tree 2 files changed +35
-3
lines changed Expand file tree Collapse file tree 2 files changed +35
-3
lines changed Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ import { UploadContext } from '../../providers/UploadProvider';
1111
1212function FileList ( ) {
1313 const themeContext = useContext ( ThemeContext ) ;
14- const { uploadedFiles } = useContext ( UploadContext ) ;
14+ const { uploadedFiles, handleDelete } = useContext ( UploadContext ) ;
1515
1616 return (
1717 < Container >
@@ -26,7 +26,11 @@ function FileList() {
2626 < strong > { file . name } </ strong >
2727 < span >
2828 { file . readableSize }
29- { ! ! file . url && < button onClick = { ( ) => { } } > Excluir</ button > }
29+ { ! ! file . url && (
30+ < button onClick = { ( e ) => handleDelete ( file . id ) } >
31+ Excluir
32+ </ button >
33+ ) }
3034 </ span >
3135 </ div >
3236 </ FileInfo >
Original file line number Diff line number Diff line change @@ -14,6 +14,28 @@ export default function UploadProvider({ children }) {
1414
1515 const [ uploading , setUploading ] = useState ( false ) ;
1616
17+ useEffect ( ( ) => {
18+ async function loadFiles ( ) {
19+ await api
20+ . get ( '/posts' )
21+ . then ( ( { data } ) => {
22+ setUploadedFiles (
23+ data . map ( ( file ) => ( {
24+ id : file . _id ,
25+ name : file . name ,
26+ readableSize : filesize ( file . size ) ,
27+ preview : file . url ,
28+ uploaded : true ,
29+ url : file . url ,
30+ } ) )
31+ ) ;
32+ } )
33+ . catch ( ( ) => { } ) ;
34+ }
35+
36+ loadFiles ( ) ;
37+ } , [ ] ) ;
38+
1739 useEffect ( ( ) => {
1840 function updateFile ( id , data ) {
1941 setFilesToUpload (
@@ -88,9 +110,15 @@ export default function UploadProvider({ children }) {
88110 setUploading ( true ) ;
89111 }
90112
113+ async function handleDelete ( fileId ) {
114+ await api . delete ( `/posts/${ fileId } ` ) ;
115+
116+ setUploadedFiles ( uploadedFiles . filter ( ( file ) => file . id !== fileId ) ) ;
117+ }
118+
91119 return (
92120 < UploadContext . Provider
93- value = { { handleUpload, uploadedFiles, setUploadedFiles } }
121+ value = { { handleDelete , handleUpload, uploadedFiles, setUploadedFiles } }
94122 >
95123 { children }
96124 </ UploadContext . Provider >
You can’t perform that action at this time.
0 commit comments