mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2025-11-14 16:51:33 +10:00
- implement lists
- implement generic sections - implement list actions - implement error handlers
This commit is contained in:
85
src/contexts/StorageContext.js
Normal file
85
src/contexts/StorageContext.js
Normal file
@ -0,0 +1,85 @@
|
||||
import firebase from "gatsby-plugin-firebase";
|
||||
import React, { createContext, useContext, useRef } from "react";
|
||||
import { toast } from "react-toastify";
|
||||
import { isFileImage } from "../utils";
|
||||
import ResumeContext from "./ResumeContext";
|
||||
import UserContext from "./UserContext";
|
||||
|
||||
const defaultState = {
|
||||
uploadPhotograph: async () => {},
|
||||
};
|
||||
|
||||
const StorageContext = createContext(defaultState);
|
||||
|
||||
const StorageProvider = ({ children }) => {
|
||||
const toastId = useRef(null);
|
||||
|
||||
const { user } = useContext(UserContext);
|
||||
const { state, dispatch } = useContext(ResumeContext);
|
||||
|
||||
const uploadPhotograph = async (file) => {
|
||||
if (!isFileImage(file)) {
|
||||
toast.error(
|
||||
"You tried to upload a file that was not an image. That won't look good on your resume. Please try again."
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
const uploadTask = firebase
|
||||
.storage()
|
||||
.ref(`/users/${user.uid}/photographs/${state.id}`)
|
||||
.put(file);
|
||||
|
||||
let progress = 0;
|
||||
toastId.current = toast("Firing up engines...", {
|
||||
progress,
|
||||
});
|
||||
|
||||
uploadTask.on(
|
||||
"state_changed",
|
||||
(snapshot) => {
|
||||
progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
|
||||
toast.update(toastId.current, {
|
||||
render: "Uploading...",
|
||||
progress,
|
||||
hideProgressBar: false,
|
||||
});
|
||||
},
|
||||
(error) => toast.error(error),
|
||||
async () => {
|
||||
const downloadURL = await uploadTask.snapshot.ref.getDownloadURL();
|
||||
dispatch({
|
||||
type: "on_input",
|
||||
payload: {
|
||||
path: "profile.photograph",
|
||||
value: downloadURL,
|
||||
},
|
||||
});
|
||||
|
||||
toast.update(toastId.current, {
|
||||
render:
|
||||
"Your photograph was uploaded successfully... and you look great!",
|
||||
progress,
|
||||
autoClose: 5000,
|
||||
hideProgressBar: true,
|
||||
});
|
||||
|
||||
toastId.current = null;
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<StorageContext.Provider
|
||||
value={{
|
||||
uploadPhotograph,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</StorageContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export default StorageContext;
|
||||
|
||||
export { StorageProvider };
|
||||
Reference in New Issue
Block a user