diff --git a/client/src/App.jsx b/client/src/App.jsx
index 5be55b97..5242b0f8 100644
--- a/client/src/App.jsx
+++ b/client/src/App.jsx
@@ -1,7 +1,5 @@
-import React from 'react';
import { AuthProvider } from './context/authContext';
import { Route, Redirect, Switch } from 'react-router-dom';
-
import Home from './pages/Home';
import Navbar from './components/Navbar';
import Footer from './components/Footer';
@@ -26,13 +24,9 @@ import addProject from './components/manageProjects/addProject';
import HealthCheck from './pages/HealthCheck';
import SecretPassword from './pages/SecretPassword';
import UserWelcome from './pages/UserWelcome';
-// Added User Permission Search component
-import UserPermissionSearch from './pages/UserPermissionSearch';
import UserPermission from './pages/UserPermission';
-
import { Box, ThemeProvider } from '@mui/material';
import theme from './theme';
-
import './App.scss';
/*
@@ -44,6 +38,7 @@ import './App.scss';
Return if user is logged in
*/
import withAuth from './hooks/withAuth';
+import { SearchTextProvider } from './context/searchContext';
const routes = [
{ path: '/', name: 'home', Component: Home },
@@ -68,7 +63,7 @@ const routes = [
{
path: '/users/permission-search',
name: 'useradmin',
- Component: UserPermission,
+ Component: withAuth(UserPermission),
},
{
path: '/projects/:projectId',
@@ -93,47 +88,49 @@ const App = () => {
return (
-
+
-
-
- {routes.map(({ path, Component }) => (
-
- ))}
-
-
+
+
+
+ {routes.map(({ path, Component }) => (
+
+ ))}
+
+
+
+
-
-
+
);
diff --git a/client/src/components/user-admin/EditUsers.jsx b/client/src/components/user-admin/EditUsers.jsx
index 30defbee..6b62bdef 100644
--- a/client/src/components/user-admin/EditUsers.jsx
+++ b/client/src/components/user-admin/EditUsers.jsx
@@ -17,6 +17,7 @@ import {
} from '@mui/material';
import '../../sass/UserAdmin.scss';
+
// child of UserAdmin. Displays form to update users.
const EditUsers = ({
userToEdit,
@@ -47,8 +48,6 @@ const EditUsers = ({
setUserManagedProjects(userToEdit.managedProjects);
}, [userToEdit]);
- console.log(userManagedProjects)
-
const userProjectsToDisplay = activeProjects.filter((item) =>
userProjects.includes(item[0])
);
@@ -64,7 +63,6 @@ const EditUsers = ({
) {
const newProjects = [...userManagedProjects, projectValue];
updateUserDb(userToEdit, projectValue, 'add');
- // updateUserDb(userToEdit, newProjects);
setUserManagedProjects(newProjects);
setProjectValue('');
} else {
@@ -78,11 +76,10 @@ const EditUsers = ({
(p) => p !== projectToRemove
);
updateUserDb(userToEdit, projectToRemove, 'remove');
- // updateUserDb(userToEdit, newProjects);
setUserManagedProjects(newProjects);
}
};
-
+
const handleSetIsActive = () => {
if (!isSuperAdmin) {
setIsActive(!isActive);
diff --git a/client/src/components/user-admin/UserManagement.jsx b/client/src/components/user-admin/UserManagement.jsx
index 1ead7238..1fa2abcd 100644
--- a/client/src/components/user-admin/UserManagement.jsx
+++ b/client/src/components/user-admin/UserManagement.jsx
@@ -1,4 +1,3 @@
-import React, { useState } from 'react';
import {
Box,
Button,
@@ -11,6 +10,7 @@ import {
} from '@mui/material';
import '../../sass/UserAdmin.scss';
+import { useSearchText } from '../../context/searchContext';
const Buttonsx = {
px: 2,
@@ -19,8 +19,7 @@ const Buttonsx = {
const UserManagement = ({ users, setUserToEdit }) => {
let searchResults = [];
- const [searchResultType, setSearchResultType] = useState('name'); // Which results will diplay
- const [searchTerm, setSearchTerm] = useState(''); // Serch term for the user/email search
+ const { searchText, setSearchText, searchResultType, setSearchResultType } = useSearchText(); // React context hook
// Swaps the buttons and displayed panels for the search results, by email or by name
const buttonSwap = () =>
@@ -30,10 +29,10 @@ const UserManagement = ({ users, setUserToEdit }) => {
// Handle change on input in search form
const handleChange = (event) => {
- setSearchTerm(event.target.value);
+ setSearchText(event.target.value);
};
- if (!searchTerm) {
+ if (!searchText) {
searchResults = [];
} else {
searchResults =
@@ -42,14 +41,14 @@ const UserManagement = ({ users, setUserToEdit }) => {
.filter((user) =>
user.email
?.toLowerCase()
- .includes(searchTerm.toLowerCase().trim())
+ .includes(searchText.toLowerCase().trim())
)
.sort((a, b) => a.email.localeCompare(b.email))
: Object.values(users)
.filter((user) =>
`${user.name?.firstName} ${user.name?.lastName}`
.toLowerCase()
- .includes(searchTerm.toLowerCase().trim())
+ .includes(searchText.toLowerCase().trim())
)
.sort((a, b) =>
a.name?.firstName
@@ -103,7 +102,7 @@ const UserManagement = ({ users, setUserToEdit }) => {
type="text"
placeholder="Enter name and / or email to find a user."
variant="standard"
- value={searchTerm}
+ value={searchText}
onChange={handleChange}
/>
{
+const ListComponent = ({ data, isProjectLead, setUserToEdit }) => {
return (
{data.map((u, idx) => {
@@ -100,14 +101,12 @@ const DummyComponent = ({ data, isProjectLead, setUserToEdit }) => {
);
};
-const UserPermissionSearch = ({ admins, projectLeads, setUserToEdit }) => {
- const [userType, setUserType] = useState('admin'); // Which results will display
- const [searchText, setSearchText] = useState(''); // Search term for the admin/PM search
- const [isProjectLead, setIsProjectLead] = useState(false);
+const UserPermissionSearch = ({ admins, projectManagers, setUserToEdit }) => {
+ const { searchText, setSearchText, userType, setUserType, isProjectLead, setIsProjectLead } = useSearchText(); // React context hook
const location = useLocation();
- const resultData = [...admins, ...projectLeads];
+ const resultData = [...admins, ...projectManagers];
useEffect(() => {
// Edit url by adding '/admin' upon loading
@@ -121,8 +120,15 @@ const UserPermissionSearch = ({ admins, projectLeads, setUserToEdit }) => {
}, [userType]);
// Swaps the buttons and displayed panels for the search results, by email or by name
- const buttonSwap = () =>
- isProjectLead ? setIsProjectLead(false) : setIsProjectLead(true);
+ const buttonSwap = () => {
+ if (isProjectLead) {
+ setIsProjectLead(false);
+ setUserType('admin');
+ } else {
+ setIsProjectLead(true);
+ setUserType('projectLead');
+ }
+ }
// Handle change on input in search form
const handleChange = (event) => {
@@ -268,7 +274,7 @@ const UserPermissionSearch = ({ admins, projectLeads, setUserToEdit }) => {
>
{/*Component to render admins and PMs*/}
- {
+ const [searchText, setSearchText] = useState('');
+ const [searchResultType, setSearchResultType] = useState('name');
+ const [userType, setUserType] = useState('admin');
+ const [isProjectLead, setIsProjectLead] = useState(false);
+
+ return (
+
+ {children}
+
+ );
+};
+
+export const useSearchText = () => useContext(SearchTextContext);
\ No newline at end of file
diff --git a/client/src/pages/UserPermission.jsx b/client/src/pages/UserPermission.jsx
index af38752a..1aeea1b6 100644
--- a/client/src/pages/UserPermission.jsx
+++ b/client/src/pages/UserPermission.jsx
@@ -29,8 +29,8 @@ const UserPermission = () => {
}, [userApiService]);
const updateUserDb = useCallback(
- async (user, managedProjects) => {
- await userApiService.updateUserDbProjects(user, managedProjects);
+ async (user, managedProjects, action) => {
+ await userApiService.updateUserDbProjects(user, managedProjects, action);
fetchAdmins();
},
[userApiService, fetchAdmins]
@@ -38,7 +38,7 @@ const UserPermission = () => {
const updateUserActiveStatus = useCallback(
async (user, isActive) => {
- await userApiService.updateUserDbIsActive(admins, isActive);
+ await userApiService.updateUserDbIsActive(user, isActive);
fetchAdmins();
},
[userApiService, fetchAdmins]
@@ -46,8 +46,8 @@ const UserPermission = () => {
// Update user's access level (admin/user)
const updateUserAccessLevel = useCallback(
- async (admin, newAccessLevel) => {
- await userApiService.updateUserAccessLevel(admin, newAccessLevel);
+ async (user, newAccessLevel) => {
+ await userApiService.updateUserAccessLevel(user, newAccessLevel);
fetchAdmins();
},
[userApiService, fetchAdmins]
@@ -62,7 +62,7 @@ const UserPermission = () => {
fetchAdmins();
fetchProjects();
fetchProjectsManagers();
- }, [fetchAdmins, fetchProjects, fetchProjectsManagers]);
+ }, [userToEdit, fetchAdmins, fetchProjects, fetchProjectsManagers]);
const backToSearch = () => {
setUserToEdit({});
@@ -76,7 +76,7 @@ const UserPermission = () => {
return (
);
diff --git a/client/src/pages/UserPermissionSearch.jsx b/client/src/pages/UserPermissionSearch.jsx
deleted file mode 100644
index a077aa33..00000000
--- a/client/src/pages/UserPermissionSearch.jsx
+++ /dev/null
@@ -1,314 +0,0 @@
-import React, { useState, useEffect } from 'react';
-import {
- Box,
- Button,
- ButtonGroup,
- Grid,
- TextField,
- Typography,
- List,
- ListItem,
- ListItemButton,
-} from '@mui/material';
-import { useLocation } from 'react-router-dom';
-
-import '../sass/UserAdmin.scss';
-
-const Buttonsx = {
- px: 2,
- py: 0.5,
-};
-
-const dummyData = [
- {
- _id: 1,
- name: {
- firstName: 'John',
- lastName: 'Doe',
- },
- accessLevel: 'admin',
- email: 'johndoe@hackforla.org',
- projects: [],
- },
- {
- _id: 2,
- name: {
- firstName: 'Vinny',
- lastName: 'Harris',
- },
- accessLevel: 'admin',
- email: 'vinnyharris@hackforla.org',
- projects: [],
- },
- {
- _id: 3,
- name: {
- firstName: 'Gary',
- lastName: 'Jones',
- },
- accessLevel: 'admin',
- email: 'garyjones@hackforla.org',
- projects: [],
- },
- {
- _id: 4,
- name: {
- firstName: 'Jane',
- lastName: 'Smith',
- },
- accessLevel: 'projectLead',
- email: 'janesmith@hackforla.org',
- projects: ['VRMS', 'Mobile'],
- },
- {
- _id: 5,
- name: {
- firstName: 'Bonnie',
- lastName: 'Wolfe',
- },
- accessLevel: 'projectLead',
- email: 'bonnie@hackforla.org',
- projects: ['Home Unite Us'],
- },
- {
- _id: 6,
- name: {
- firstName: 'Diana',
- lastName: 'Loeb',
- },
- accessLevel: 'projectLead',
- email: 'dianaloeb@hackforla.org',
- projects: ['HackforLA Mobile', 'LA TDM Calculator'],
- },
- {
- _id: 7,
- name: {
- firstName: 'Zack',
- lastName: 'Cruz',
- },
- accessLevel: 'projectLead',
- email: 'dianaloeb@hackforla.org',
- projects: ['LA TDM Calculator', 'VRMS backend'],
- },
- {
- _id: 8,
- name: {
- firstName: 'Iris',
- lastName: 'Sosa',
- },
- accessLevel: 'projectLead',
- email: 'irissosa@hackforla.org',
- projects: ['Home Unite Us', 'VRMS Support'],
- },
-];
-
-const DummyComponent = ({ data, type }) => {
- return (
-
- {data.map((user, index) => {
- // Destructure user object
- const { _id, name, email } = user;
- return type === 'admin' ? (
-
- setUserToEdit(user)}
- >
-
-
-
- {`${name.firstName.toUpperCase()} ${name.lastName.toUpperCase()} ( ${email.toUpperCase()} )`}
-
-
-
-
-
- ) : (
-
- setUserToEdit(user)}
- >
-
-
-
- {name.firstName.toUpperCase() +
- ' ' +
- name.lastName.toUpperCase()}
-
-
-
-
- {user.project}
-
-
-
-
-
- );
- })}
-
- );
-};
-
-const UserPermissionSearch = ({ users, setUserToEdit }) => {
- const [userType, setUserType] = useState('admin'); // Which results will display
- const [searchText, setSearchText] = useState(''); // Search term for the admin/PM search
-
- const location = useLocation();
-
- useEffect(() => {
- // Edits url by adding '/admin' upon loading
- let editURL = '';
- if (userType === 'admin') {
- editURL = location.pathname + '/admin';
- } else {
- editURL = location.pathname + '/projects';
- }
- window.history.replaceState({}, '', editURL);
- }, [userType]);
-
- // Swaps the buttons and displayed panels for the search results, by email or by name
- const buttonSwap = () =>
- userType === 'projectLead'
- ? setUserType('admin')
- : setUserType('projectLead');
-
- // Handle change on input in search form
- const handleChange = (event) => {
- setSearchText(event.target.value);
- };
-
- // Filtering logic
- let filteredData;
- if (!searchText) {
- filteredData = dummyData.filter((user) => user.accessLevel === userType);
- if (userType === 'admin') {
- // Default display for admins, sorted ASC based on first name
- filteredData.sort((u1, u2) =>
- u1.name?.firstName.localeCompare(u2.name?.firstName)
- );
- } else {
- // Default display of all PMs, sorted ASC based on project name, then first name
- let tempFilter = [];
- filteredData.forEach((user) => {
- user.projects.forEach((project) => {
- tempFilter.push({ ...user, project });
- });
- });
- tempFilter.sort(
- (u1, u2) =>
- u1.project.localeCompare(u2.project) ||
- u1.name?.firstName.localeCompare(u2.name?.firstName)
- );
- filteredData = [...tempFilter];
- }
- }
-
- return (
-
-
-
- User Permission Search
-
-
-
-
-
-
-
-
- 0 ? '#F5F5F5' : 'transparent',
- my: 1.2,
- borderRadius: 1,
- flexGrow: 1,
- width: 1 / 1,
- }}
- >
-
- {/*Component to render admins and PMs*/}
-
-
-
-
-
- );
-};
-
-export default UserPermissionSearch;