Add Contexts
This commit is contained in:
@@ -3,23 +3,23 @@ import React, { useContext, useEffect, useState } from 'react';
|
||||
const FileUploadContext = React.createContext();
|
||||
|
||||
export const FileUploadProvider = ({ children }) => {
|
||||
const [file, setFile] = useState(null);
|
||||
const [uploading, setUploading] = useState(false);
|
||||
const [progress, setProgress] = useState(0);
|
||||
const [file, setFile] = useState(null);
|
||||
const [uploading, setUploading] = useState(false);
|
||||
const [progress, setProgress] = useState(0);
|
||||
|
||||
const upload = (file) => {
|
||||
const upload = (file) => {
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
return (
|
||||
<FileUploadContext.Provider value={{
|
||||
uploading,
|
||||
progress,
|
||||
upload,
|
||||
}}>
|
||||
{children}
|
||||
</FileUploadContext.Provider>
|
||||
);
|
||||
return (
|
||||
<FileUploadContext.Provider value={{
|
||||
uploading,
|
||||
progress,
|
||||
upload,
|
||||
}}>
|
||||
{children}
|
||||
</FileUploadContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export const useFileUploadContext = () => useContext(FileUploadContext);
|
||||
|
||||
@@ -1,35 +1,100 @@
|
||||
import React, { useContext, useEffect, useState } from 'react';
|
||||
|
||||
const AUTH_URL = 'https://dev-auth.fiskerdps.com';
|
||||
const UserContext = React.createContext();
|
||||
|
||||
export const UserProvider = ({ children }) => {
|
||||
const [signedIn, setSignedIn] = useState(false);
|
||||
const [user, setUser] = useState(null);
|
||||
const [token, setToken] = useState(null);
|
||||
const [signInError, setSignInError] = useState(null);
|
||||
const [signUpError, setSignUpError] = useState(null);
|
||||
const [fetching, setFetching] = useState(false);
|
||||
const [signedIn, setSignedIn] = useState(false);
|
||||
const [user, setUser] = useState(null);
|
||||
const [token, setToken] = useState(null);
|
||||
const [error, setError] = useState(null);
|
||||
|
||||
const signIn = (email, password) => {
|
||||
const signIn = (username, password) => {
|
||||
if (!username) throw new Error('Email is required');
|
||||
if (!password) throw new Error('Password is required');
|
||||
setFetching(true);
|
||||
setError(null);
|
||||
return fetch(`${AUTH_URL}/auth/login`, {
|
||||
method: "POST",
|
||||
mode: "no-cors",
|
||||
headers: {
|
||||
"Content-Type":"text/plain"
|
||||
},
|
||||
body: JSON.stringify({
|
||||
username,
|
||||
password,
|
||||
})
|
||||
})
|
||||
.then((response) => {
|
||||
debugger;
|
||||
return response.json();
|
||||
})
|
||||
.then((result) => {
|
||||
debugger;
|
||||
setFetching(false);
|
||||
if (result.error) setError(result.error);
|
||||
return result;
|
||||
})
|
||||
.catch((error) => {
|
||||
setError(error.message);
|
||||
setFetching(false);
|
||||
return null;
|
||||
});
|
||||
};
|
||||
|
||||
};
|
||||
const signUp = (username, password, confirmPassword) => {
|
||||
if (!username) throw new Error('Email is required');
|
||||
if (!password) throw new Error('Password is required');
|
||||
if (password !== confirmPassword) throw new Error('Passwords do not match');
|
||||
setFetching(true);
|
||||
setError(null);
|
||||
return fetch(`${AUTH_URL}/auth/register`, {
|
||||
method: "POST",
|
||||
mode: "cors",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify({
|
||||
username,
|
||||
password,
|
||||
})
|
||||
})
|
||||
.then((response) => {
|
||||
debugger;
|
||||
return response.json();
|
||||
})
|
||||
.then((result) => {
|
||||
if (result.error) setError(result.error);
|
||||
setFetching(false);
|
||||
return result;
|
||||
})
|
||||
.catch((error) => {
|
||||
setError(error.message);
|
||||
setFetching(false);
|
||||
return null;
|
||||
});
|
||||
};
|
||||
|
||||
const signUp = (email, password, confirmPassword) => {
|
||||
const signOut = async () => {
|
||||
setUser(null);
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
return (
|
||||
<UserContext.Provider value={{
|
||||
signedIn,
|
||||
user,
|
||||
token,
|
||||
signInError,
|
||||
signUpError,
|
||||
signIn,
|
||||
signUp,
|
||||
}}>
|
||||
{children}
|
||||
</UserContext.Provider>
|
||||
);
|
||||
return (
|
||||
<UserContext.Provider value={{
|
||||
fetching,
|
||||
signedIn,
|
||||
user,
|
||||
token,
|
||||
error,
|
||||
setError,
|
||||
signIn,
|
||||
signUp,
|
||||
signOut,
|
||||
}}>
|
||||
{children}
|
||||
</UserContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export const useUserContext = () => useContext(UserContext);
|
||||
|
||||
Reference in New Issue
Block a user