Skip to content

Commit

Permalink
Update admin dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
sarL3y committed Jan 21, 2020
1 parent 25f1f91 commit f873341
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 0 deletions.
54 changes: 54 additions & 0 deletions client/src/components/DashboardUsers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import React, { useState, useEffect } from 'react';
import { Link } from 'react-router-dom';

import '../sass/DashboardUsers.scss';
// import '../sass/EventsContainer-media-queries.scss';

const DashboardUsers = (props) => {
// const [isLoading, setIsLoading] = useState(false);
const [users, setUsers] = useState([]);
// const [isError, setIsError] = useState(false);

async function fetchData() {
try {
const res = await fetch("/api/users");
const resJson = await res.json();

setUsers(resJson);
} catch(error) {
alert(error);
}
}

useEffect(() => {
fetchData();

}, []);

return (
<div className="flexcenter-container">
<div className="events-list">
<ul>
{users.map((user, index) => {
return (
<li key={index}>
<div key={index} className="event">
<div className="user-name">
<h5>{user.name.firstName} {user.name.lastName}</h5>
</div>
<div className="user-roles">
<p>Current Role: {user.currentRole}</p>
<p>Desired Role: {user.desiredRole}</p>
</div>
</div>
</li>
)
})}
</ul>
</div>
</div>
)
};

export default DashboardUsers;

93 changes: 93 additions & 0 deletions client/src/context/authContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import React, { useState, useEffect, createContext } from 'react';

export const authContext = createContext();

export function ProvideAuth({ children }) {
const auth = useProvideAuth();

return <authContext.Provider value={auth}>
{ children }
</authContext.Provider>
};

function useProvideAuth() {
const [isAdmin, setIsAdmin] = useState(null);
const [user, setUser] = useState(null);
// const [userId, setUserId] = useState(null);

// const isAdmin = async (userId) => {
// console.log('isAdmin tentatively ran');

// try {
// fetch('/api/[email protected]')
// .then(response => response.json())
// .then(resJson => {
// if (resJson.toString() === '5e1d2df6316d2f00172ef09e') {
// console.log('It happened to be true');
// setUserId(resJson);
// setUser(true);
// } else {
// console.log('It was definitely false what were you thinking');
// setUser(false);
// }
// })
// .catch(err => {
// console.log(err);
// })

const login = async (email) => {
try {
const response = await fetch(`/api/users?email=${email}`);
const userId = await response.json();

const userResponse = await fetch(`/api/users/${userId}`);
const user = await userResponse.json();

if(user.accessLevel === 'admin') {
console.log('Hey... that worked...');
setUser(user);
setIsAdmin(true);
return true;
} else {
console.log('Nope not gonna work');
setIsAdmin(false);
return false;
}
} catch(error) {
console.log(error);
}
}


// } catch(error) {
// console.log(error);
// }
// }

// const signin = (isLoggedIn) => {
// if(isLoggedIn) {
// return setUser(isLoggedIn);
// }

// }

useEffect(() => {
console.log(user);

// const isTrue = () => {
// console.log(userId === '5e1d2df6316d2f00172ef09e');
// return userId === '5e1d2df6316d2f00172ef09e' ? setUser(true) : setUser(false);
// }
// // login/subscribe
// // user === true;
// // // login/unsubscribe
// return () => isTrue;
// return () => {isAdmin()}
}, []);

return { user, isAdmin, login };
};




9 changes: 9 additions & 0 deletions client/src/hooks/useAuth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React, { useContext } from 'react';

import { authContext } from '../context/authContext';

const useAuth = () => {
return useContext(authContext);
};

export default useAuth;
17 changes: 17 additions & 0 deletions client/src/sass/AdminLogin.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.adminlogin-container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
z-index: 1;
padding-top: 6vh;
}

.adminlogin-headers {
margin: 36px 0px;
}

.adminlogin-warning {
width: 300px;
color: red;
}
5 changes: 5 additions & 0 deletions client/src/sass/DashboardUsers.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.user-roles {
p {
margin: 0px;
}
}

0 comments on commit f873341

Please sign in to comment.