Remove redux

This commit is contained in:
Daniel Mason 2021-03-31 13:16:42 +13:00
parent 0c56281bcd
commit ebd88b3bb0
Signed by: idanoo
GPG key ID: 387387CDBC02F132
30 changed files with 263 additions and 696 deletions

View file

@ -0,0 +1,5 @@
import React from 'react';
const AuthContext = React.createContext();
export default AuthContext;

View file

@ -0,0 +1,65 @@
import React, { useState, useEffect } from 'react';
import { toast } from 'react-toastify';
import AuthContext from './AuthContext';
import { PostLogin, PostRegister } from '../Api/index';
const AuthContextProvider = ({ children }) => {
const [user, setUser] = useState();
const [loading, setLoading] = useState(false);
useEffect(() => {
setLoading(true)
const user = JSON.parse(localStorage.getItem('user'));
if (user && user.jwt) {
setUser(user)
}
setLoading(false)
}, []);
const Login = (formValues) => {
setLoading(true);
PostLogin(formValues).then(user => {
if (user) {
setUser(user);
const { history } = this.props;
history.push("/dashboard");
}
setLoading(false);
})
}
const Register = (formValues) => {
const { history } = this.props;
setLoading(true);
return PostRegister(formValues).then(response => {
if (response) {
history.push("/login");
}
setLoading(false);
});
};
const Logout = () => {
localStorage.removeItem("user");
setUser(null)
toast.success('Successfully logged out.');
};
return (
<AuthContext.Provider
value={{
Logout,
Login,
Register,
loading,
user,
}}
>
{children}
</AuthContext.Provider>
);
};
export default AuthContextProvider;