diff --git a/src/core/App.tsx b/src/core/App.tsx index 5138d3d38b750cfc9b5877ba2ae4da963f1e1b00..91934945bfaea6d8c8210ece4a269dd5332487e5 100644 --- a/src/core/App.tsx +++ b/src/core/App.tsx @@ -4,7 +4,7 @@ import styled from 'styled-components'; import Footer from './components/Footer'; import Header from './components/Header'; import MainRouting from './components/MainRouting'; -import UserContext from './components/UserContext'; +import UserContext, { IProfile } from './context/UserContext'; const Container = styled.div` height: 100%; @@ -18,14 +18,17 @@ const MainContent = styled.div` `; function App() { - const [user, setUser] = useState({ loggedIn: false, name: '' }); + const [user, setUser] = useState({ loggedIn: false, profile: {} as IProfile }); useEffect(() => { fetch('/api/v1/users/me') - .then((response) => response.json()) + .then((response) => { + if (!response.ok) return false; + return response.json(); + }) .then((profile) => { - if (profile.loggedIn) { - setUser({ loggedIn: true, name: profile.name }); + if (profile) { + setUser({ loggedIn: true, profile }); } }); }, []); diff --git a/src/core/components/ContextTest.tsx b/src/core/components/ContextTest.tsx index 74169c6493ca04f756f6f01f89bea323e49065c7..6dcc599d9623150517d7908dbd8b5a5e5497ec1e 100644 --- a/src/core/components/ContextTest.tsx +++ b/src/core/components/ContextTest.tsx @@ -1,10 +1,10 @@ import React, { useContext } from 'react'; -import UserContext from './UserContext'; +import UserContext from '../context/UserContext'; const ContextTest = () => { const user = useContext(UserContext); - - return <div>{user.loggedIn ? user.name : 'Not logged in!'}</div>; + console.log(user.profile); + return <div>{user.loggedIn ? user.profile.name : 'Not logged in!'}</div>; }; export default ContextTest; diff --git a/src/core/components/UserContext.tsx b/src/core/components/UserContext.tsx deleted file mode 100644 index 1778af8bc084a3c21a696dfdb74b8d18acf2ffc8..0000000000000000000000000000000000000000 --- a/src/core/components/UserContext.tsx +++ /dev/null @@ -1,5 +0,0 @@ -import { createContext } from 'react'; - -const UserContext = createContext({ loggedIn: false, name: '' }); - -export default UserContext; diff --git a/src/core/context/UserContext.tsx b/src/core/context/UserContext.tsx new file mode 100644 index 0000000000000000000000000000000000000000..c231730911a2aed4e60d3d6d0d78f1926b9755d8 --- /dev/null +++ b/src/core/context/UserContext.tsx @@ -0,0 +1,31 @@ +import { createContext } from 'react'; + +interface IWarnings extends Document { + text: string; + date: Date; + given_by: { + _id: string; + name: string; + }; +} + +enum Role { + Admin, + Staff, + User, +} + +export interface IProfile { + external_id: string; + studentCardNumber?: string; + roomNumber?: string; + picture?: string; + role: Role.Admin | Role.Staff | Role.User; + email: string; + name: string; + warnings: [IWarnings] | []; +} + +const UserContext = createContext({ loggedIn: false, profile: {} as IProfile }); + +export default UserContext;