@@ -3,12 +3,13 @@ import * as FileActionCreators from '../../Context/ActionCreators/FileActionCrea
33import * as AuthActionCreators from '../../Context/ActionCreators/AuthActionCreater' ;
44import { FileContext } from '../../Context/Contexts/FileContext' ;
55import { AuthContext } from '../../Context/Contexts/AuthContext' ;
6+ import { FileUploadContext } from '../../Context/Contexts/FileUploadContext' ;
7+ import * as FileUploadActionCreators from '../../Context/ActionCreators/FileUploadActionCreater' ;
68import axios from '../../utils/axios' ;
79import { toast } from 'react-toastify' ;
810import { useDropzone } from 'react-dropzone' ;
9- import getFileSize from '../../utils/fileSize' ;
1011import getFileType from '../../utils/fileType' ;
11-
12+ import { v4 as uuidv4 } from 'uuid' ;
1213if ( ( typeof TextDecoder === 'undefined' || typeof TextEncoder === 'undefined' ) && typeof require !== 'undefined' ) {
1314 global . TextDecoder = require ( 'util' ) . TextDecoder
1415 global . TextEncoder = require ( 'util' ) . TextEncoder
@@ -33,48 +34,15 @@ const img = {
3334 height : '50px' ,
3435 width : '50px'
3536} ;
36- const FileForm = ( { setCurrentPage} ) => {
37- const { fileState, fileDispatch} = useContext ( FileContext )
37+ const FileForm = ( { setCurrentPage, toggleFileFormModal, toggleFileUploadStatusModal} ) => {
38+ const { fileState, fileDispatch} = useContext ( FileContext ) ;
39+ const { fileUploadDispatch} = useContext ( FileUploadContext ) ;
3840 const { authState} = useContext ( AuthContext ) ;
3941 const [ files , setFiles ] = useState ( [ ] ) ;
40- const [ isDragActive , setIsDragActive ] = useState ( true ) ;
4142 useEffect ( ( ) => {
4243 series ( files [ 0 ] , 0 ) ;
4344 } , [ files ] )
44- const setStatus = ( index , status , URL = undefined ) => {
45- const temp = files ;
46- temp [ index ] . file_status = status ;
47- if ( URL ) {
48- temp [ index ] . default = process . env . REACT_APP_FRONTENDURL + "/" + URL ;
49- }
50- setFiles ( temp ) ;
51- }
52- const renderStatus = ( status , URL ) => {
53- if ( status == "uploaded" ) {
54- return (
55- < >
56- < span className = "fa fa-clipboard mx-2" role = "button" onClick = { ( ) => { navigator . clipboard . writeText ( URL ) } } > </ span >
57- < span className = "text-success" >
58- < i className = "fa fa-check-circle fa-lg" > </ i >
59- </ span >
60- </ >
61- )
62- }
63- else if ( status == "uploading" ) {
64- return (
65- < div class = "spinner-grow" role = "status" >
66- < span class = "sr-only" > Loading...</ span >
67- </ div >
68- )
69- }
70- else if ( status == "error" ) {
71- return (
72- < span className = "text-danger" >
73- < i class = "fa fa-exclamation-circle fa-lg" > </ i >
74- </ span >
75- )
76- }
77- }
45+
7846 async function getBuffer ( file ) {
7947 const reader = new window . FileReader ( ) ;
8048 return new Promise ( ( resolve , reject ) => {
@@ -87,7 +55,8 @@ const FileForm = ({setCurrentPage}) => {
8755 }
8856 const series = ( file , index ) => {
8957 if ( file ) {
90- setIsDragActive ( false ) ;
58+ toggleFileFormModal ( ) ;
59+ toggleFileUploadStatusModal ( ) ;
9160 handleSubmit ( file , index , ( ) => {
9261 series ( files [ index + 1 ] , index + 1 ) ;
9362 } ) ;
@@ -114,12 +83,9 @@ const FileForm = ({setCurrentPage}) => {
11483 fileDispatch ( FileActionCreators . updateStartDate ( undefined ) ) ;
11584 FileActionCreators . loadFiles ( fileDispatch , 1 , undefined , fileState . searchParam ) ;
11685 setCurrentPage ( 1 ) ;
117- setIsDragActive ( true ) ;
11886 }
11987 }
12088 const handleSubmit = async ( file , i , callback ) => {
121- const index = i
122-
12389 try {
12490 if ( file == undefined ) {
12591 throw new Error ( "Invalid File" )
@@ -140,69 +106,47 @@ const FileForm = ({setCurrentPage}) => {
140106 if ( response . data . error ) {
141107 throw new Error ( response . data . error ) ;
142108 }
143- setStatus ( index , "uploaded" , response . data . body . default ) ;
109+ file . file_status = "uploaded" ;
110+ file . default = response . data . body . default ;
111+ fileUploadDispatch ( FileUploadActionCreators . fileUploadStateUpdateFile ( file ) )
144112 authState . auth . storage_used += fileData . size
145113 authState . auth . storage_used = parseFloat ( authState . auth . storage_used . toFixed ( 4 ) )
146114 AuthActionCreators . authStateUpdate ( authState ) ;
147115 localStorage . setItem ( "auth" , JSON . stringify ( authState . auth ) ) ;
148116 callback ( ) ;
149117 } catch ( err ) {
150118 toast . error ( err . message ) ;
151- setStatus ( index , "error" ) ;
119+ file . status = "error" ;
120+ fileUploadDispatch ( FileUploadActionCreators . fileUploadStateUpdateFile ( file ) )
152121 callback ( ) ;
153122 }
154123 }
155124
156125
157126 const { getRootProps, getInputProps} = useDropzone ( {
158127 onDrop : acceptedFiles => {
159- setFiles ( acceptedFiles . map ( file => Object . assign ( file , {
128+ const fileData = acceptedFiles . map ( file => Object . assign ( file , {
160129 preview : URL . createObjectURL ( file ) ,
161130 file_type : getFileType ( file . name ) ,
162131 file_name : file . name ,
163132 file_size : file . size ,
164- file_status : "uploading"
165- } ) ) ) ;
133+ file_status : "uploading" ,
134+ file_id : uuidv4 ( )
135+ } ) )
136+ fileUploadDispatch ( FileUploadActionCreators . fileUploadStateAddFiles ( fileData ) ) ;
137+ setFiles ( fileData ) ;
138+
166139 }
167140 } ) ;
168- const thumbFile = ( file ) => {
169- return (
170- < div className = "d-flex justify-content-between align-items-center" >
171- < div className = "p-1 d-flex flex-row align-items-center" >
172- < img src = { file . preview } style = { img } alt = "file" />
173- < span >
174- < span className = "px-2 font-weight-bold text-monospace" > { file . file_name } </ span >
175- < span className = "badge badge-info" > { getFileSize ( file . file_size ) } </ span >
176- </ span >
177- </ div >
178- < span >
179- { renderStatus ( file . file_status , file . default ) }
180- </ span >
181- </ div >
182- )
183- }
184- const thumbs = files . map ( file => {
185- return (
186- thumbFile ( file )
187- )
188- } ) ;
189141 return (
190142 < div className = "text-center" >
191- { isDragActive ?
192- < div { ...getRootProps ( { style :baseStyle } ) } >
193- < input { ...getInputProps ( ) } />
194- < div className = "" >
195- < img src = "https://res.cloudinary.com/code-gambit/image/upload/v1621421198/Web%20App/file_40x40_sea3kj.png" />
196- < br > </ br >
197- < p > Drag and Drop your files here</ p >
198- </ div >
143+ < div { ...getRootProps ( { style :baseStyle } ) } >
144+ < input { ...getInputProps ( ) } />
145+ < div className = "" >
146+ < img src = "https://res.cloudinary.com/code-gambit/image/upload/v1621421198/Web%20App/file_40x40_sea3kj.png" />
147+ < br > </ br >
148+ < p > Drag and Drop your files here</ p >
199149 </ div >
200- :
201- ""
202- }
203- < div >
204-
205- { thumbs }
206150 </ div >
207151 </ div >
208152
0 commit comments