From 96d1f17fc95b377ec8a3c5aad7c958c48c702061 Mon Sep 17 00:00:00 2001
From: rlacko <rlacko@sch.bme.hu>
Date: Thu, 13 Aug 2020 20:08:30 +0200
Subject: [PATCH] update user context with interface

---
 src/core/App.tsx                    | 13 +++++++-----
 src/core/components/ContextTest.tsx |  6 +++---
 src/core/components/UserContext.tsx |  5 -----
 src/core/context/UserContext.tsx    | 31 +++++++++++++++++++++++++++++
 4 files changed, 42 insertions(+), 13 deletions(-)
 delete mode 100644 src/core/components/UserContext.tsx
 create mode 100644 src/core/context/UserContext.tsx

diff --git a/src/core/App.tsx b/src/core/App.tsx
index 5138d3d..9193494 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 74169c6..6dcc599 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 1778af8..0000000
--- 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 0000000..c231730
--- /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;
-- 
GitLab