Skip to content

Commit 6dbac5b

Browse files
committed
use React context hook to replace useState for search queries
1 parent 0541bb6 commit 6dbac5b

File tree

6 files changed

+76
-364
lines changed

6 files changed

+76
-364
lines changed

VRMS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 7042af0f43f0b034977c7f64c613e9d8b7199edb

client/src/App.jsx

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import React from 'react';
21
import { AuthProvider } from './context/authContext';
32
import { Route, Redirect, Switch } from 'react-router-dom';
4-
53
import Home from './pages/Home';
64
import Navbar from './components/Navbar';
75
import Footer from './components/Footer';
@@ -26,13 +24,9 @@ import addProject from './components/manageProjects/addProject';
2624
import HealthCheck from './pages/HealthCheck';
2725
import SecretPassword from './pages/SecretPassword';
2826
import UserWelcome from './pages/UserWelcome';
29-
// Added User Permission Search component
30-
import UserPermissionSearch from './pages/UserPermissionSearch';
3127
import UserPermission from './pages/UserPermission';
32-
3328
import { Box, ThemeProvider } from '@mui/material';
3429
import theme from './theme';
35-
3630
import './App.scss';
3731

3832
/*
@@ -44,6 +38,7 @@ import './App.scss';
4438
Return <ComponentName {...props} auth={auth} /> if user is logged in
4539
*/
4640
import withAuth from './hooks/withAuth';
41+
import { SearchTextProvider } from './context/searchContext';
4742

4843
const routes = [
4944
{ path: '/', name: 'home', Component: Home },
@@ -93,47 +88,49 @@ const App = () => {
9388
return (
9489
<ThemeProvider theme={theme}>
9590
<AuthProvider>
96-
<Box
97-
sx={{
98-
height: '100%',
99-
width: '100vw',
100-
display: 'flex',
101-
justifyContent: 'center',
102-
alignItems: 'center',
103-
overflow: 'hidden',
104-
maxHeight: '90vh',
105-
margin: '5vh 0',
106-
}}
107-
>
91+
<SearchTextProvider>
10892
<Box
10993
sx={{
110-
position: 'relative',
111-
maxWidth: '500px',
112-
width: '100%',
113-
backgroundColor: 'white',
94+
height: '100%',
95+
width: '100vw',
96+
display: 'flex',
97+
justifyContent: 'center',
98+
alignItems: 'center',
11499
overflow: 'hidden',
115-
borderRadius: '10px',
116-
padding: '15px',
100+
maxHeight: '90vh',
101+
margin: '5vh 0',
117102
}}
118103
>
119-
<Navbar />
120104
<Box
121-
component="main"
122105
sx={{
123-
height: 'calc(90vh - 160px)',
124-
overflowY: 'scroll',
106+
position: 'relative',
107+
maxWidth: '500px',
108+
width: '100%',
109+
backgroundColor: 'white',
110+
overflow: 'hidden',
111+
borderRadius: '10px',
112+
padding: '15px',
125113
}}
126114
>
127-
<Switch>
128-
{routes.map(({ path, Component }) => (
129-
<Route key={path} exact path={path} component={Component} />
130-
))}
131-
<Redirect to="/" />
132-
</Switch>
115+
<Navbar />
116+
<Box
117+
component="main"
118+
sx={{
119+
height: 'calc(90vh - 160px)',
120+
overflowY: 'scroll',
121+
}}
122+
>
123+
<Switch>
124+
{routes.map(({ path, Component }) => (
125+
<Route key={path} exact path={path} component={Component} />
126+
))}
127+
<Redirect to="/" />
128+
</Switch>
129+
</Box>
130+
<Footer />
133131
</Box>
134-
<Footer />
135132
</Box>
136-
</Box>
133+
</SearchTextProvider>
137134
</AuthProvider>
138135
</ThemeProvider>
139136
);

client/src/components/user-admin/UserManagement.jsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import React, { useState } from 'react';
21
import {
32
Box,
43
Button,
@@ -11,6 +10,7 @@ import {
1110
} from '@mui/material';
1211

1312
import '../../sass/UserAdmin.scss';
13+
import { useSearchText } from '../../context/searchContext';
1414

1515
const Buttonsx = {
1616
px: 2,
@@ -19,8 +19,7 @@ const Buttonsx = {
1919

2020
const UserManagement = ({ users, setUserToEdit }) => {
2121
let searchResults = [];
22-
const [searchResultType, setSearchResultType] = useState('name'); // Which results will diplay
23-
const [searchTerm, setSearchTerm] = useState(''); // Serch term for the user/email search
22+
const { searchText, setSearchText, searchResultType, setSearchResultType } = useSearchText(); // React context hook
2423

2524
// Swaps the buttons and displayed panels for the search results, by email or by name
2625
const buttonSwap = () =>
@@ -30,10 +29,10 @@ const UserManagement = ({ users, setUserToEdit }) => {
3029

3130
// Handle change on input in search form
3231
const handleChange = (event) => {
33-
setSearchTerm(event.target.value);
32+
setSearchText(event.target.value);
3433
};
3534

36-
if (!searchTerm) {
35+
if (!searchText) {
3736
searchResults = [];
3837
} else {
3938
searchResults =
@@ -42,14 +41,14 @@ const UserManagement = ({ users, setUserToEdit }) => {
4241
.filter((user) =>
4342
user.email
4443
?.toLowerCase()
45-
.includes(searchTerm.toLowerCase().trim())
44+
.includes(searchText.toLowerCase().trim())
4645
)
4746
.sort((a, b) => a.email.localeCompare(b.email))
4847
: Object.values(users)
4948
.filter((user) =>
5049
`${user.name?.firstName} ${user.name?.lastName}`
5150
.toLowerCase()
52-
.includes(searchTerm.toLowerCase().trim())
51+
.includes(searchText.toLowerCase().trim())
5352
)
5453
.sort((a, b) =>
5554
a.name?.firstName
@@ -103,7 +102,7 @@ const UserManagement = ({ users, setUserToEdit }) => {
103102
type="text"
104103
placeholder="Enter name and / or email to find a user."
105104
variant="standard"
106-
value={searchTerm}
105+
value={searchText}
107106
onChange={handleChange}
108107
/>
109108
<Box

client/src/components/user-admin/UserPermissionSearch.jsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useEffect } from 'react';
1+
import { useEffect } from 'react';
22
import {
33
Box,
44
Button,
@@ -11,6 +11,7 @@ import {
1111
ListItemButton,
1212
} from '@mui/material';
1313
import { useLocation } from 'react-router-dom';
14+
import { useSearchText } from '../../context/searchContext';
1415

1516
import '../../sass/UserAdmin.scss';
1617

@@ -101,9 +102,7 @@ const DummyComponent = ({ data, isProjectLead, setUserToEdit }) => {
101102
};
102103

103104
const UserPermissionSearch = ({ admins, projectLeads, setUserToEdit }) => {
104-
const [userType, setUserType] = useState('admin'); // Which results will display
105-
const [searchText, setSearchText] = useState(''); // Search term for the admin/PM search
106-
const [isProjectLead, setIsProjectLead] = useState(false);
105+
const { searchText, setSearchText, userType, setUserType, isProjectLead, setIsProjectLead } = useSearchText(); // React context hook
107106

108107
const location = useLocation();
109108

@@ -121,8 +120,15 @@ const UserPermissionSearch = ({ admins, projectLeads, setUserToEdit }) => {
121120
}, [userType]);
122121

123122
// Swaps the buttons and displayed panels for the search results, by email or by name
124-
const buttonSwap = () =>
125-
isProjectLead ? setIsProjectLead(false) : setIsProjectLead(true);
123+
const buttonSwap = () => {
124+
if (isProjectLead) {
125+
setIsProjectLead(false);
126+
setUserType('admin');
127+
} else {
128+
setIsProjectLead(true);
129+
setUserType('projectLead');
130+
}
131+
}
126132

127133
// Handle change on input in search form
128134
const handleChange = (event) => {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { createContext, useContext, useState } from 'react';
2+
3+
const SearchTextContext = createContext();
4+
5+
export const SearchTextProvider = ({ children }) => {
6+
const [searchText, setSearchText] = useState('');
7+
const [searchResultType, setSearchResultType] = useState('name');
8+
const [userType, setUserType] = useState('admin');
9+
const [isProjectLead, setIsProjectLead] = useState(false);
10+
11+
return (
12+
<SearchTextContext.Provider value={{
13+
searchText, setSearchText,
14+
searchResultType, setSearchResultType,
15+
userType, setUserType,
16+
isProjectLead, setIsProjectLead
17+
}}>
18+
{children}
19+
</SearchTextContext.Provider>
20+
);
21+
};
22+
23+
export const useSearchText = () => useContext(SearchTextContext);

0 commit comments

Comments
 (0)