11import Button from './buttons' ;
2+ import Loader from './loaders' ;
3+ import { EditorContext , getCsrfCookie } from '../util' ;
24
35
46export function FormInput ( { label, help_text, error, inputRef, ...props } ) {
@@ -128,12 +130,15 @@ export function dataURItoBlob(dataURI) {
128130
129131
130132export class FormFileInput extends React . Component {
133+ static contextType = EditorContext ;
134+
131135 constructor ( props ) {
132136 super ( props ) ;
133137
134138 this . state = {
135139 value : props . value ,
136- fileName : this . getFileName ( )
140+ fileName : this . getFileName ( ) ,
141+ loading : false
137142 } ;
138143
139144 this . inputRef = React . createRef ( ) ;
@@ -176,31 +181,71 @@ export class FormFileInput extends React.Component {
176181
177182 handleChange = ( e ) => {
178183 if ( this . props . type === 'data-url' ) {
184+ let file = e . target . files [ 0 ] ;
185+ let fileName = file . name
179186
180- }
181-
182- let file = e . target . files [ 0 ] ;
183- let fileName = file . name
187+ let reader = new FileReader ( ) ;
184188
185- let reader = new FileReader ( ) ;
189+ reader . onload = ( ) => {
186190
187- reader . onload = ( ) => {
191+ // this.setState({src: reader.result});
188192
189- // this.setState({src: reader.result});
193+ // we create a fake event object
194+ let event = {
195+ target : {
196+ type : 'text' ,
197+ value : this . addNameToDataURL ( reader . result , fileName ) ,
198+ name : this . props . name
199+ }
200+ } ;
190201
191- // we create a fake event object
192- let event = {
193- target : {
194- type : 'text' ,
195- value : this . addNameToDataURL ( reader . result , fileName ) ,
196- name : this . props . name
197- }
198- } ;
202+ this . props . onChange ( event ) ;
199203
200- this . props . onChange ( event ) ;
204+ }
205+ reader . readAsDataURL ( file ) ;
206+ } else if ( this . props . type === 'file-url' ) {
207+ let endpoint = this . context . fileUploadEndpoint ;
208+ if ( ! endpoint ) {
209+ console . error (
210+ "Error: fileUploadEndpoint option need to be passed "
211+ + "while initializing editor for enabling file uploads." ) ;
212+ alert ( "Files can't be uploaded." ) ;
213+ return ;
214+ }
215+
216+ this . setState ( { loading : true } ) ;
217+
218+ let formData = new FormData ( ) ;
219+ formData . append ( 'file' , e . target . files [ 0 ] ) ;
220+
221+ fetch ( endpoint , {
222+ method : 'POST' ,
223+ headers : {
224+ 'X-CSRFToken' : getCsrfCookie ( ) ,
225+ } ,
226+ body : formData
227+ } )
228+ . then ( ( response ) => response . json ( ) )
229+ . then ( ( result ) => {
230+ // we create a fake event object
231+ let event = {
232+ target : {
233+ type : 'text' ,
234+ value : result . file_path ,
235+ name : this . props . name
236+ }
237+ } ;
238+
239+ this . props . onChange ( event ) ;
240+ this . setState ( { loading : false } ) ;
241+ } )
242+ . catch ( ( error ) => {
243+ alert ( 'Something went wrong while uploading file' ) ;
244+ console . error ( 'Error:' , error ) ;
245+ this . setState ( { loading : false } ) ;
246+ } ) ;
201247
202248 }
203- reader . readAsDataURL ( file ) ;
204249
205250 }
206251
@@ -220,10 +265,14 @@ export class FormFileInput extends React.Component {
220265 { this . state . value &&
221266 < div className = "rjf-current-file-name" > Current file: < span > { this . state . fileName } </ span > </ div >
222267 }
223- { this . state . value && 'Change:' }
268+ { this . state . value && ! this . state . loading && 'Change:' }
269+ { this . state . loading ?
270+ < div className = "rjf-file-field-loading" > < Loader /> Uploading...</ div >
271+ :
224272 < div className = "rjf-file-field-input" >
225273 < FormInput { ...props } inputRef = { this . inputRef } />
226274 </ div >
275+ }
227276 </ div >
228277 </ div >
229278 ) ;
0 commit comments