diff --git a/src/App.js b/src/App.js index 4d503dc..7265177 100644 --- a/src/App.js +++ b/src/App.js @@ -1,18 +1,19 @@ import { lazy, Suspense } from 'react'; import { Container } from '@mui/material'; -import { useUser } from './context/user'; +import { useAuthState } from './context/auth'; + import LoadingIndicator from './components/LoadingIndicator'; const AuthenticatedApp = lazy(() => import('./AuthenticatedApp')); const UnauthenticatedApp = lazy(() => import('./UnauthenticatedApp')); function App() { - const user = useUser(); + const { isAuthenticated } = useAuthState(); return ( }> - {user ? : } + {isAuthenticated ? : } ); diff --git a/src/context/auth.js b/src/context/auth.js index c759e72..1104bc9 100644 --- a/src/context/auth.js +++ b/src/context/auth.js @@ -1,12 +1,17 @@ import { createContext, useContext, useState } from 'react'; +import { sleep } from '../utils/sleep'; -const sleep = time => new Promise(resolve => setTimeout(resolve, time)); const getUser = shouldFail => sleep(3000).then(() => { if (shouldFail) { return { message: 'Falha na autenticação' }; } else { - return { username: 'Leonardo' }; + return { + id: '0021564', + username: 'leonardomurca', + name: 'Leonardo', + lastName: 'Murça', + }; } }); diff --git a/src/context/user.js b/src/context/user.js index a4139dc..c4d7cae 100644 --- a/src/context/user.js +++ b/src/context/user.js @@ -1,14 +1,74 @@ -import { createContext, useContext } from 'react'; +import { createContext, useContext, useEffect, useState } from 'react'; +import { sleep } from '../utils/sleep'; import { useAuthState } from './auth'; +const getClassrooms = userId => + sleep(3000).then(() => { + console.log('userId: ' + userId); + return { + data: [ + { + name: 'Introdução à Ciência de Dados', + abbreviation: 'ICD', + teacher: 'Carlos Alexandre Silva', + }, + { + name: 'Gestão de Projetos', + abbreviation: 'GP', + teacher: 'Míriam Lúcia Barbosa', + }, + { + name: 'Banco de Dados II', + abbreviation: 'BDII', + teacher: 'Cristiane Norbiato Targa', + }, + { + name: 'Contabilidade Básica', + abbreviation: 'CB', + teacher: 'Alexandre Couto Cardoso', + }, + { + name: 'Linguagens de Programação', + abbreviation: 'LP', + teacher: 'Gabriel Felipe Cândido Novy', + }, + ], + }; + }); + const UserContext = createContext(); function UserProvider(props) { - return ; + const { user } = useAuthState(); + const [state, setState] = useState({ + status: 'idle', + user: null, + classrooms: [], + error: null, + }); + + useEffect(() => { + setState({ user }); + }, [user]); + + const classrooms = () => { + setState({ ...state, status: 'pending' }); + getClassrooms(user.id).then(data => + setState({ status: 'success', user, classrooms: data.data, error: null }) + ); + }; + + return ; } function useUser() { - return useContext(UserContext); + const { state, classrooms } = useContext(UserContext); + const isPending = state.status === 'pending'; + + return { + isPending, + classrooms, + }; } export { UserProvider, useUser }; diff --git a/src/screens/Home/index.js b/src/screens/Home/index.js index 48c09ef..6bf7919 100644 --- a/src/screens/Home/index.js +++ b/src/screens/Home/index.js @@ -1,12 +1,13 @@ -import { useAuthState } from '../../context/auth'; +import { useUser } from '../../context/user'; function Home() { - const { logout, isPending } = useAuthState(); + const { isPending, state, classrooms } = useUser(); + console.log(state); return (

You're logged in!

- + {isPending &&

Loading...

}
); diff --git a/src/utils/sleep.js b/src/utils/sleep.js new file mode 100644 index 0000000..5b48a51 --- /dev/null +++ b/src/utils/sleep.js @@ -0,0 +1,3 @@ +const sleep = time => new Promise(resolve => setTimeout(resolve, time)); + +export { sleep };