-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the file upload to the server store hook
- Loading branch information
1 parent
e443b89
commit 86c80c8
Showing
3 changed files
with
51 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* eslint-disable */ | ||
import React from 'react'; | ||
import FileUpload from '../hooks/Hookstorage'; | ||
|
||
const ProgressBar = () => ( | ||
<div className="progress" /> | ||
); | ||
|
||
export default ProgressBar; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* eslint-disable */ | ||
import { useState, useEffect } from 'react'; | ||
import * as firebase from 'firebase/app'; | ||
import 'firebase/storage'; | ||
import 'firebase/firestore'; | ||
|
||
const projectStore = firebase.storage(); | ||
|
||
const FileUpload = (file) => { | ||
const [progress, setProgress] = useState(null); | ||
const [error, setError] = useState(null); | ||
const [url, setUrl] = useState(null); | ||
|
||
useEffect(() => { | ||
// References | ||
const storageRef = projectStore.ref(file.name); | ||
|
||
storageRef.put(file).on('state_changed', (snap) => { | ||
let percentage = (snap.bytesTransferred / snap.totalBytes) * 100; | ||
setProgress(percentage); | ||
}, (error) => { | ||
setError(error); | ||
}, async () => { | ||
const url = await storageRef.getDownloadURL(); | ||
setUrl(url); | ||
}) | ||
}, [file]) | ||
|
||
return { progress, url, error } | ||
|
||
} | ||
|
||
export default FileUpload; |