-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathImageUploader.tsx
58 lines (49 loc) · 2.01 KB
/
ImageUploader.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { useState } from 'react';
import { auth, storage, STATE_CHANGED } from '@lib/firebase';
import Loader from './Loader';
import { ref, uploadBytesResumable, getDownloadURL } from 'firebase/storage';
// Uploads images to Firebase Storage
export default function ImageUploader(): JSX.Element {
const [uploading, setUploading] = useState(false);
const [progress, setProgress] = useState(0);
const [downloadURL, setDownloadURL] = useState(null);
// Creates a Firebase Upload Task
const uploadFile = async (e: any) => {
// Get the file
const file = Array.from(e.target.files)[0] as Blob;
const extension = file.type.split('/')[1];
// Makes reference to the storage bucket location
const uid: any = auth?.currentUser?.uid;
const fileRef = ref(storage, `uploads/${uid}/${Date.now()}.${extension}`);
setUploading(true);
// Starts the upload
const task = uploadBytesResumable(fileRef, file);
// Listen to updates to upload task
task.on(STATE_CHANGED, (snapshot) => {
const pct: any = ((snapshot.bytesTransferred / snapshot.totalBytes) * 100).toFixed(0);
setProgress(pct);
});
// Get downloadURL AFTER task resolves (Note: this is not a native Promise)
task
.then(() => getDownloadURL(fileRef))
.then((url: any) => {
setDownloadURL(url);
setUploading(false);
});
};
return (
<div className="box">
<Loader show={uploading} />
{uploading && <h3>{progress}%</h3>}
{!uploading && (
<>
<label className="btn">
📸 Upload Img
<input type="file" onChange={uploadFile} accept="image/x-png,image/gif,image/jpeg" />
</label>
</>
)}
{downloadURL && <code className="upload-snippet">{``}</code>}
</div>
);
}