diff --git a/src/App.js b/src/App.js index 3940c2d..8de6da0 100644 --- a/src/App.js +++ b/src/App.js @@ -1,13 +1,26 @@ +import { useState, useEffect } from 'react'; + import Loading from './components/Loading'; import Error from './components/Error'; import TaskList from './components/TaskList'; +import Header from './components/Header'; import { useFetch } from './hooks/useFetch'; +import { useAuth } from './providers/Auth'; const App = () => { - const [state, tasks, update, add] = useFetch( - process.env.REACT_APP_API_ENDPOINT, - ); + const { currentUser } = useAuth(); + const [url, setUrl] = useState(); + const [state, tasks, update, add] = useFetch(url); + + useEffect(() => { + if (!currentUser) { + setUrl(undefined); + return; + } + + setUrl(`${process.env.REACT_APP_API_ENDPOINT}?userId=${currentUser}`); + }, [currentUser]); const updateTask = (task) => { update(task.id, task); @@ -19,7 +32,7 @@ const App = () => { return ( <> -

TODO List

+
{state === 'PENDING' && } {state === 'SUCCESS' && ( diff --git a/src/components/Header.js b/src/components/Header.js new file mode 100644 index 0000000..373837e --- /dev/null +++ b/src/components/Header.js @@ -0,0 +1,16 @@ +import SignIn from './SignIn'; +import SignOut from './SignOut'; + +import { useAuth } from '../providers/Auth'; + +const Header = () => { + const { currentUser } = useAuth(); + return ( +
+

TODO List

+ {currentUser ? : } +
+ ); +}; + +export default Header; diff --git a/src/components/SignIn.js b/src/components/SignIn.js new file mode 100644 index 0000000..759d334 --- /dev/null +++ b/src/components/SignIn.js @@ -0,0 +1,52 @@ +import { useState } from 'react'; + +import { useInput } from '../hooks/useInput'; +import { useAuth } from '../providers/Auth'; + +const SignIn = () => { + const { signIn } = useAuth(); + const [error, setError] = useState(); + const [{ username, password }, update, reset] = useInput({ + username: '', + password: '', + }); + + const handleInputChange = (event) => { + update(event); + }; + + const handleSubmit = (event) => { + event.preventDefault(); + const signedIn = signIn(username, password); + + if (signedIn) { + setError(undefined); + reset(); + } else { + setError('user or password incorrect'); + } + }; + + return ( +
+ + + + {error &&

{error}

} +
+ ); +}; + +export default SignIn; diff --git a/src/components/SignOut.js b/src/components/SignOut.js new file mode 100644 index 0000000..90617ff --- /dev/null +++ b/src/components/SignOut.js @@ -0,0 +1,13 @@ +import { useAuth } from '../providers/Auth'; + +const SignOut = () => { + const { signOut } = useAuth(); + + const handleButtonClick = () => { + signOut(); + }; + + return ; +}; + +export default SignOut; diff --git a/src/hooks/useFetch.js b/src/hooks/useFetch.js index 06010f0..cb79f5f 100644 --- a/src/hooks/useFetch.js +++ b/src/hooks/useFetch.js @@ -6,6 +6,8 @@ export const useFetch = (url) => { useEffect(() => { if (!url) { + setRequestState('INITIAL'); + setRequestResponse(undefined); return; } diff --git a/src/index.js b/src/index.js index f452ad2..c45d471 100644 --- a/src/index.js +++ b/src/index.js @@ -2,10 +2,13 @@ import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; import App from './App'; +import { AuthProvider } from './providers/Auth'; ReactDOM.render( - + + + , document.getElementById('root'), ); diff --git a/src/providers/Auth.js b/src/providers/Auth.js new file mode 100644 index 0000000..1c5147f --- /dev/null +++ b/src/providers/Auth.js @@ -0,0 +1,33 @@ +import { useState, createContext, useContext } from 'react'; + +const AuthContext = createContext(); + +export const useAuth = () => useContext(AuthContext); + +export const AuthProvider = ({ children }) => { + const [currentUser, setCurrentUser] = useState(); + + const signIn = (username, password) => { + // mock login logic + if ( + username && + username == password && + !isNaN(username) && + username > 0 && + username < 5 + ) { + setCurrentUser(username); + return true; + } + + return false; + }; + + const signOut = () => { + setCurrentUser(null); + }; + + const value = { currentUser, signIn, signOut }; + + return {children}; +};