1
1
import React , { useState } from 'react' ;
2
2
import { motion } from 'framer-motion' ;
3
- import Lotte from 'lottie-react' ;
3
+ import Lottie from 'lottie-react' ;
4
4
import animationData from '../assets/95241-uploading.json' ;
5
5
6
6
function UploadNotes ( ) {
@@ -10,41 +10,44 @@ function UploadNotes() {
10
10
11
11
const handleFileChange = ( event ) => {
12
12
setSelectedFile ( event . target . files [ 0 ] ) ;
13
+ setUploadSuccess ( false ) ; // Reset upload success status when a new file is selected
13
14
} ;
14
15
15
16
const handleUpload = ( ) => {
16
17
if ( ! selectedFile ) {
17
18
return ;
18
19
}
19
-
20
+
20
21
setUploading ( true ) ;
21
-
22
+
22
23
const reader = new FileReader ( ) ;
23
-
24
+
24
25
reader . onload = ( ) => {
25
26
// Get the file contents
26
27
const fileContents = reader . result ;
27
-
28
+
28
29
// Create a Blob from the file contents
29
30
const blob = new Blob ( [ fileContents ] , { type : selectedFile . type } ) ;
30
-
31
+
31
32
// Create a file path within the Local_Storage/notes_pdf folder
32
33
const filePath = `Local_Storage/notes_pdf/${ selectedFile . name } ` ;
33
-
34
+
34
35
// Save the file locally
35
36
saveFileLocally ( filePath , blob )
36
37
. then ( ( ) => {
37
38
setUploadSuccess ( true ) ;
38
- setUploading ( false ) ;
39
39
} )
40
40
. catch ( ( error ) => {
41
41
console . error ( 'Error saving file:' , error ) ;
42
+ } )
43
+ . finally ( ( ) => {
44
+ setUploading ( false ) ;
42
45
} ) ;
43
46
} ;
44
-
47
+
45
48
reader . readAsArrayBuffer ( selectedFile ) ;
46
49
} ;
47
-
50
+
48
51
const saveFileLocally = ( filePath , file ) => {
49
52
return new Promise ( ( resolve , reject ) => {
50
53
const virtualLink = document . createElement ( 'a' ) ;
@@ -62,7 +65,6 @@ function UploadNotes() {
62
65
document . body . removeChild ( virtualLink ) ;
63
66
} ) ;
64
67
} ;
65
-
66
68
67
69
return (
68
70
< div className = "flex flex-col items-center justify-center h-screen text-center" >
@@ -72,7 +74,7 @@ function UploadNotes() {
72
74
animate = { { scale : 1 } }
73
75
transition = { { duration : 0.5 } }
74
76
>
75
- < Lotte animationData = { animationData } style = { { width : 400 , height : 300 } } />
77
+ < Lottie animationData = { animationData } style = { { width : 400 , height : 300 } } />
76
78
< h1 className = "text-3xl font-bold mb-4" > Upload Notes</ h1 >
77
79
{ ! uploadSuccess ? (
78
80
< >
@@ -91,15 +93,14 @@ function UploadNotes() {
91
93
whileHover = { ! uploading ? { scale : 1.05 } : { } }
92
94
whileTap = { ! uploading ? { scale : 0.95 } : { } }
93
95
>
94
- { uploading ? 'Uploading... ' : 'Upload' }
96
+ { uploading ? 'Uploaded ' : 'Upload' }
95
97
</ motion . button >
96
98
</ >
97
99
) : (
98
100
< motion . div
99
101
className = "text-xl"
100
102
initial = { { opacity : 0 } }
101
103
animate = { { opacity : 1 } }
102
- transition = { { duration : 0.5 } }
103
104
>
104
105
Upload successful!
105
106
</ motion . div >
0 commit comments