-
-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathUserManagement.jsx
More file actions
165 lines (157 loc) · 4.64 KB
/
UserManagement.jsx
File metadata and controls
165 lines (157 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import {
Box,
Button,
ButtonGroup,
TextField,
Typography,
List,
ListItem,
ListItemButton,
} from '@mui/material';
import '../../sass/UserAdmin.scss';
import { useSearchText } from '../../context/searchContext';
const Buttonsx = {
px: 2,
py: 0.5,
};
const UserManagement = ({ users, setUserToEdit }) => {
let searchResults = [];
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 = () =>
searchResultType === 'email'
? setSearchResultType('name')
: setSearchResultType('email');
// Handle change on input in search form
const handleChange = (event) => {
setSearchText(event.target.value);
};
if (!searchText) {
searchResults = [];
} else {
searchResults =
searchResultType === 'email'
? Object.values(users)
.filter((user) =>
user.email
?.toLowerCase()
.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(searchText.toLowerCase().trim())
)
.sort((a, b) =>
a.name?.firstName
.concat(a.name?.lastName)
.localeCompare(b.name?.firstName.concat(b.name?.lastName))
);
}
return (
<Box className="container--usermanagement" sx={{ px: '1.8rem', mb: 0 }}>
<Box
sx={{
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
pt: 5,
height: '100%',
width: 1 / 1,
}}
>
<Typography variant="h1">User Search</Typography>
<Box className="tab-buttons">
<ButtonGroup
sx={{
display: 'flex',
flexDirection: 'row',
justifyContent: 'center',
whiteSpace: 'nowrap',
mx: 1,
}}
>
<Button
sx={Buttonsx}
type="button"
variant={searchResultType === 'name' ? 'contained' : 'secondary'}
onClick={buttonSwap}
>
Results by Name
</Button>
<Button
sx={Buttonsx}
type="button"
variant={searchResultType === 'email' ? 'contained' : 'secondary'}
onClick={buttonSwap}
>
Results by Email
</Button>
</ButtonGroup>
</Box>
<TextField
type="text"
placeholder="Enter name and / or email to find a user."
variant="standard"
value={searchText}
onChange={handleChange}
/>
<Box
sx={{
bgcolor: searchResults.length > 0 ? '#F5F5F5' : 'transparent',
my: 1.2,
borderRadius: 1,
flexGrow: 1,
width: 1 / 1,
}}
>
<Box>
<List className="search-results disablePadding">
{searchResults.map((u) => {
return (
// eslint-disable-next-line no-underscore-dangle
<ListItem
sx={{
px: 2.4,
py: 1,
borderBottom: 1.6,
borderBottomColor: 'grey.300',
}}
key={`result_${u._id}`}
>
<ListItemButton
sx={{
px: 0.25,
py: 0.36,
color: 'primary.main',
mx: 0.16,
}}
className="search-results-button"
type="button"
onClick={() => setUserToEdit(u)}
>
{searchResultType === 'name'
? `${u.name?.firstName} ${u.name?.lastName} ( ${u.email} )`
: `${u.email} ( ${u.name?.firstName} ${u.name?.lastName} )`}
</ListItemButton>
</ListItem>
);
})}
</List>
</Box>
</Box>
<Box>
{/* <Button
type='button'
variant='secondary'
>
Add a New User
</Button> */}
</Box>
</Box>
</Box>
);
};
export default UserManagement;