@@ -2,14 +2,12 @@ import React, { useState, useEffect } from "react";
2
2
import { SaveFileSize } from "../../constant/saveFileSize" ;
3
3
import Parse from "parse" ;
4
4
import sanitizeFileName from "../../primitives/sanitizeFileName" ;
5
-
5
+ import DropboxChooser from "./DropboxChoose" ;
6
6
const FileUpload = ( props ) => {
7
7
const [ parseBaseUrl ] = useState ( localStorage . getItem ( "baseUrl" ) ) ;
8
8
const [ parseAppId ] = useState ( localStorage . getItem ( "parseAppId" ) ) ;
9
9
const [ _fileupload , setFileUpload ] = useState ( "" ) ;
10
10
const [ fileload , setfileload ] = useState ( false ) ;
11
-
12
- const [ localValue , setLocalValue ] = useState ( "" ) ;
13
11
const [ Message ] = useState ( false ) ;
14
12
const [ percentage , setpercentage ] = useState ( 0 ) ;
15
13
@@ -25,7 +23,6 @@ const FileUpload = (props) => {
25
23
const onChange = ( e ) => {
26
24
try {
27
25
let files = e . target . files ;
28
- setLocalValue ( e . target . files ) ;
29
26
if ( typeof files [ 0 ] !== "undefined" ) {
30
27
if ( props . schema . filetypes && props . schema . filetypes . length > 0 ) {
31
28
var fileName = files [ 0 ] . name ;
@@ -106,6 +103,56 @@ const FileUpload = (props) => {
106
103
console . error ( "Error uploading file:" , error ) ;
107
104
}
108
105
} ;
106
+
107
+ const dropboxSuccess = async ( files ) => {
108
+ // console.log("file ", files);
109
+ setfileload ( true ) ;
110
+ const file = files [ 0 ] ;
111
+ const url = file . link ;
112
+ const size = file . bytes ;
113
+ const mb = Math . round ( file . bytes / Math . pow ( 1024 , 2 ) ) ;
114
+
115
+ if ( mb > 10 ) {
116
+ setTimeout ( ( ) => {
117
+ alert (
118
+ `The selected file size is too large. Please select a file less than 10 MB`
119
+ ) ;
120
+ } , 500 ) ;
121
+ return ;
122
+ } else {
123
+ const name = sanitizeFileName ( file . name ) ;
124
+
125
+ const parseFile = new Parse . File ( name , { uri : url } ) ;
126
+
127
+ try {
128
+ const response = await parseFile . save ( {
129
+ progress : ( progressValue , loaded , total , { type } ) => {
130
+ if ( type === "upload" && progressValue !== null ) {
131
+ const percentCompleted = Math . round ( ( loaded * 100 ) / total ) ;
132
+ // console.log("percentCompleted ", percentCompleted);
133
+ setpercentage ( percentCompleted ) ;
134
+ }
135
+ }
136
+ } ) ;
137
+ // console.log("response.url() ", response.url());
138
+ setFileUpload ( response . url ( ) ) ;
139
+ props . onChange ( response . url ( ) ) ;
140
+ setfileload ( false ) ;
141
+
142
+ if ( response . url ( ) ) {
143
+ SaveFileSize ( size , response . url ( ) ) ;
144
+ return response . url ( ) ;
145
+ }
146
+ } catch ( error ) {
147
+ setfileload ( false ) ;
148
+ setpercentage ( 0 ) ;
149
+ console . error ( "Error uploading file:" , error ) ;
150
+ }
151
+ }
152
+ } ;
153
+ const dropboxCancel = async ( ) => {
154
+ console . log ( "cancel clicked " ) ;
155
+ } ;
109
156
let fileView =
110
157
props . formData &&
111
158
props . schema . uploadtype === "s3viajw" ? null : props . formData &&
@@ -207,55 +254,51 @@ const FileUpload = (props) => {
207
254
</ div >
208
255
209
256
< >
210
- { localValue ? (
211
- < input
212
- type = "file"
213
- id = "hashfile"
214
- style = { {
215
- border : "1px solid #ccc" ,
216
- color : "gray" ,
217
- backgroundColor : "white" ,
218
- padding : "5px 10px" ,
219
- borderRadius : "4px" ,
220
- fontSize : "13px" ,
221
- width : "100%" ,
222
- fontWeight : "bold"
223
- } }
224
- accept = "application/pdf,application/vnd.ms-excel"
225
- onChange = { onChange }
226
- />
227
- ) : props . formData ? (
228
- < div
229
- style = { {
230
- border : "1px solid #ccc" ,
231
- color : "gray" ,
232
- backgroundColor : "white" ,
233
- padding : "5px 10px" ,
234
- borderRadius : "4px" ,
235
- fontSize : "13px" ,
236
- width : "100%" ,
237
- fontWeight : "bold"
238
- } }
239
- >
240
- file selected : { props . formData . split ( "/" ) [ 3 ] }
257
+ { props . formData ? (
258
+ < div className = "flex gap-2 justify-center items-center" >
259
+ < div className = "flex justify-between items-center px-2 py-[3px] w-full font-bold rounded border-[1px] border-[#ccc] text-gray-500 bg-white text-[13px]" >
260
+ < div className = "break-all" >
261
+ file selected : { props . formData . split ( "/" ) [ 3 ] }
262
+ </ div >
263
+ < div
264
+ onClick = { ( ) => {
265
+ console . log ( "clicked" ) ;
266
+ setFileUpload ( [ ] ) ;
267
+ props . onChange ( undefined ) ;
268
+ } }
269
+ className = "cursor-pointer px-[10px] text-[20px] font-bold bg-white text-red-500"
270
+ >
271
+ < i className = "fa-solid fa-xmark" > </ i >
272
+ </ div >
273
+ </ div >
274
+ < DropboxChooser
275
+ onSuccess = { dropboxSuccess }
276
+ onCancel = { dropboxCancel }
277
+ />
241
278
</ div >
242
279
) : (
243
- < input
244
- type = "file"
245
- id = "hashfile"
246
- style = { {
247
- border : "1px solid #ccc" ,
248
- color : "gray" ,
249
- backgroundColor : "white" ,
250
- padding : "5px 10px" ,
251
- borderRadius : "4px" ,
252
- fontSize : "13px" ,
253
- width : "100%" ,
254
- fontWeight : "bold"
255
- } }
256
- accept = "application/pdf,application/vnd.ms-excel"
257
- onChange = { onChange }
258
- />
280
+ < div className = "flex gap-2 justify-center items-center" >
281
+ < input
282
+ type = "file"
283
+ id = "hashfile"
284
+ style = { {
285
+ border : "1px solid #ccc" ,
286
+ color : "gray" ,
287
+ backgroundColor : "white" ,
288
+ padding : "5px 10px" ,
289
+ borderRadius : "4px" ,
290
+ fontSize : "13px" ,
291
+ width : "100%" ,
292
+ fontWeight : "bold"
293
+ } }
294
+ accept = "application/pdf,application/vnd.ms-excel"
295
+ onChange = { onChange }
296
+ />
297
+ < DropboxChooser
298
+ onSuccess = { dropboxSuccess }
299
+ onCancel = { dropboxCancel }
300
+ />
301
+ </ div >
259
302
) }
260
303
</ >
261
304
</ React . Fragment >
0 commit comments