From 114cd05d0354e37077f927daba0d09524ed70224 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonardo=20Mur=C3=A7a?= Date: Wed, 8 Jun 2022 15:57:57 -0300 Subject: [PATCH] Add fetch for classrooms --- src/App.js | 7 +++-- src/context/auth.js | 9 ++++-- src/context/user.js | 66 +++++++++++++++++++++++++++++++++++++-- src/screens/Home/index.js | 7 +++-- src/utils/sleep.js | 3 ++ 5 files changed, 81 insertions(+), 11 deletions(-) create mode 100644 src/utils/sleep.js 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 };