diff --git a/src/App.js b/src/App.js index 010cba1..36be6ab 100644 --- a/src/App.js +++ b/src/App.js @@ -6,21 +6,29 @@ import { useGetUser } from "./hooks"; function App() { // eslint-disable-next-line - const [{ user, isLoading, isError }, dispatch] = useGetUser(); + const [{ user, isLoading, isError, errorMessage }, dispatch] = useGetUser(); return ( - - {user ? : } + + {user ? ( + + ) : ( + + )} - - {user ? : } + + {user ? ( + + ) : ( + + )} - + - + ); diff --git a/src/hooks/index.js b/src/hooks/index.js index 5f6c6d5..08eadba 100644 --- a/src/hooks/index.js +++ b/src/hooks/index.js @@ -68,7 +68,12 @@ export const useGetUser = () => { user: action.payload, }; case FetchState.FETCH_FAILURE: - return { ...state, isLoading: false, isError: true }; + return { + ...state, + isLoading: false, + isError: true, + errorMessage: action.payload?.message, + }; default: throw new Error(); } @@ -77,6 +82,7 @@ export const useGetUser = () => { const [state, dispatch] = useReducer(reducer, { isLoading: false, isError: true, + errorMessage: null, data: [], }); diff --git a/src/pages/Login/Login.js b/src/pages/Login/Login.js index 115fa7c..c8f6be7 100644 --- a/src/pages/Login/Login.js +++ b/src/pages/Login/Login.js @@ -3,64 +3,73 @@ import api from "../../api/api"; import SignUp from "./SignUp"; import { FetchState } from "../../hooks"; -const Login = ({ dispatch }) => { +const Login = ({ dispatch, error }) => { const [email, setEmail] = useState(); const [password, setPassword] = useState(); const [register, setRegister] = useState(false); + const [loading, setLoading] = useState(false); const handleLogin = async (e) => { e.preventDefault(); + setLoading(true); dispatch({ type: FetchState.FETCH_INIT }); try { await api.createSession(email, password); const data = await api.getAccount(); dispatch({ type: FetchState.FETCH_SUCCESS, payload: data }); } catch (e) { - dispatch({ type: FetchState.FETCH_FAILURE }); + setLoading(false); + console.log(e.message); + dispatch({ + type: FetchState.FETCH_FAILURE, + payload: { message: e.message }, + }); } }; return register ? ( - + ) : ( -
-
-

Login

-

+

+
+

Login

+

{" "} Don't have an account ?{" "} setRegister(true)} > Sign Up {" "}

- + setEmail(e.target.value)} - name="email" - autoComplete="email" + name='email' + autoComplete='email' /> - + setPassword(e.target.value)} - name="password" - autoComplete="password" + name='password' + autoComplete='password' /> -
+

{error} 

+ +
diff --git a/src/pages/Login/SignUp.js b/src/pages/Login/SignUp.js index 44d7247..8dbc1be 100644 --- a/src/pages/Login/SignUp.js +++ b/src/pages/Login/SignUp.js @@ -2,73 +2,81 @@ import { useState } from "react"; import api from "../../api/api"; import { FetchState } from "../../hooks"; -const SignUp = ({ setRegister, dispatch }) => { +const SignUp = ({ setRegister, dispatch, error }) => { const [name, setName] = useState(); const [email, setEmail] = useState(); const [password, setPassword] = useState(); + const [loading, setLoading] = useState(false); const handleSignup = async (e) => { e.preventDefault(); + setLoading(true); dispatch({ type: FetchState.FETCH_INIT }); try { const user = await api.createAccount(email, password, name); await api.createSession(email, password); dispatch({ type: FetchState.FETCH_SUCCESS, payload: user }); } catch (e) { - dispatch({ type: FetchState.FETCH_FAILURE }); + setLoading(false); + dispatch({ + type: FetchState.FETCH_FAILURE, + payload: { message: e.message }, + }); } }; return ( <> -
-
-

Sign Up

-

+

+
+

Sign Up

+

{" "} Already have an account ?{" "} setRegister(false)} > Login {" "}

- + setName(e.target.value)} - name="name" - autoComplete="name" + name='name' + autoComplete='name' /> - + {/* “Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” */} setEmail(e.target.value)} - name="email" - autoComplete="email" + name='email' + autoComplete='email' /> - + setPassword(e.target.value)} - name="password" - autoComplete="password" + name='password' + autoComplete='password' /> -
+

{error} 

+ +