11import React , { Component , PropTypes } from 'react' ;
22import DropZone from '../common/DropZone' ;
3+ import GlyphButton from '../common/GlyphButton' ;
4+ import FileInfo from '../common/FileInfo' ;
5+
6+ import fetch from 'isomorphic-fetch' ;
37
48export default class FileUpload extends Component {
59 static propTypes = {
@@ -11,11 +15,34 @@ export default class FileUpload extends Component {
1115 } ;
1216
1317 onDrop = ( files ) => {
18+ //TODO Check file and size. Avoid duplicated files
1419 let fileArray = [ ...this . state . files , ...files ] ;
20+
1521 this . setState ( { files : fileArray } ) ;
1622 } ;
1723
24+ onClick = ( event ) => {
25+ // TODO Handle response status for upload service
26+ event . preventDefault ( ) ;
27+
28+ const { files } = this . state ;
29+ const { url } = this . props ;
30+
31+ let fileData = new FormData ( ) ;
32+
33+ files . forEach ( ( file ) => {
34+ fileData . append ( file . name , file ) ;
35+ } ) ;
36+
37+ fetch ( url , {
38+ method : "POST" ,
39+ body : fileData
40+ } ) ;
41+ } ;
42+
1843 render ( ) {
44+ let { files } = this . state ;
45+
1946 return (
2047 < div >
2148 < label > Files</ label >
@@ -25,6 +52,15 @@ export default class FileUpload extends Component {
2552 Try dropping some files here, or click to select files to upload.
2653 </ div >
2754 </ DropZone >
55+ < div > {
56+ files . map ( ( file , index ) => (
57+ < FileInfo key = { index } file = { file } />
58+ ) )
59+ }
60+ </ div >
61+ < div >
62+ < GlyphButton text = "Upload" onClick = { this . onClick } />
63+ </ div >
2864 </ div >
2965 </ div >
3066 ) ;
0 commit comments