1 |
b523c74d
|
Schwobik
|
import {
|
2 |
|
|
Button,
|
3 |
287652cf
|
Schwobik
|
Grid,
|
4 |
b523c74d
|
Schwobik
|
List,
|
5 |
|
|
ListItem,
|
6 |
|
|
ListItemText,
|
7 |
|
|
Paper,
|
8 |
|
|
} from '@mui/material'
|
9 |
287652cf
|
Schwobik
|
import {Fragment, useEffect, useState} from 'react'
|
10 |
b523c74d
|
Schwobik
|
import { useDispatch, useSelector } from 'react-redux'
|
11 |
|
|
import { RootState } from '../redux/store'
|
12 |
|
|
import 'react-quill/dist/quill.snow.css'
|
13 |
|
|
import ContentLoading from "../Reusables/ContentLoading"
|
14 |
|
|
import {UserDto} from "../../swagger/data-contracts"
|
15 |
|
|
import UserDetail from "./UserDetail"
|
16 |
|
|
import {Link as RouterLink} from "react-router-dom"
|
17 |
|
|
import AddIcon from '@mui/icons-material/Add';
|
18 |
287652cf
|
Schwobik
|
import {fetchUsers} from "./userDetailThunks"
|
19 |
|
|
import {
|
20 |
|
|
clear,
|
21 |
|
|
consumeError,
|
22 |
fdf6c32d
|
Schwobik
|
resetIsRequestCompleted,
|
23 |
287652cf
|
Schwobik
|
setLoading,
|
24 |
|
|
setSelectedUser
|
25 |
|
|
} from "./userDetailSlice"
|
26 |
|
|
import ShowErrorIfPresent from "../Reusables/ShowErrorIfPresent"
|
27 |
|
|
import {number} from "yup"
|
28 |
b523c74d
|
Schwobik
|
|
29 |
|
|
const apiError =
|
30 |
|
|
'Error while fetching data from the server, please try again later.'
|
31 |
|
|
|
32 |
|
|
const Administration = () => {
|
33 |
287652cf
|
Schwobik
|
// Items, loading and error from api
|
34 |
|
|
const users = useSelector((state: RootState) => state.usersDetail.users)
|
35 |
|
|
const loading = useSelector((state: RootState) => state.usersDetail.loading)
|
36 |
|
|
const apiError = useSelector((state: RootState) => state.usersDetail.error)
|
37 |
|
|
const selectedUser = useSelector((state: RootState) => state.usersDetail.selectedUser)
|
38 |
fdf6c32d
|
Schwobik
|
const isRequestCompleted = useSelector((state: RootState) => state.usersDetail.isRequestCompleted)
|
39 |
287652cf
|
Schwobik
|
const [selectedUserId, setSelectedUserId] = useState<number | undefined>(undefined)
|
40 |
|
|
|
41 |
|
|
// Local state to display any error relevant error
|
42 |
|
|
const [displayError, setDisplayError] = useState<string | undefined>(
|
43 |
|
|
undefined
|
44 |
|
|
)
|
45 |
|
|
|
46 |
|
|
const dispatch = useDispatch()
|
47 |
b523c74d
|
Schwobik
|
|
48 |
|
|
const isAdmin = useSelector(
|
49 |
|
|
(state: RootState) => state.user.roles.includes("ROLE_ADMIN")
|
50 |
|
|
)
|
51 |
|
|
|
52 |
|
|
const handleClick = (userID: number) => {
|
53 |
287652cf
|
Schwobik
|
console.log("clicked "+ userID)
|
54 |
b523c74d
|
Schwobik
|
if (users) {
|
55 |
287652cf
|
Schwobik
|
setSelectedUserId(userID)
|
56 |
|
|
dispatch(setSelectedUser(users.at(userID)))
|
57 |
|
|
console.log("after " + selectedUser)
|
58 |
b523c74d
|
Schwobik
|
}
|
59 |
|
|
}
|
60 |
|
|
|
61 |
287652cf
|
Schwobik
|
// Use effect to read the error and consume it
|
62 |
|
|
useEffect(() => {
|
63 |
|
|
if (users.length > 0) {
|
64 |
fdf6c32d
|
Schwobik
|
// if there is user selected, leave that state - handled by useEffect on isRequestCompleted
|
65 |
|
|
if (selectedUser !== undefined) {
|
66 |
|
|
const selUser = users.find(u => u.email === selectedUser.email)
|
67 |
|
|
if (selUser !== undefined) {
|
68 |
|
|
console.log("Selected from before")
|
69 |
|
|
console.log(selUser)
|
70 |
|
|
setSelectedUserId(users.findIndex(u => u.email === selectedUser.email))
|
71 |
|
|
dispatch(setSelectedUser(selUser))
|
72 |
|
|
return
|
73 |
|
|
}
|
74 |
|
|
} else {
|
75 |
|
|
setSelectedUserId(0)
|
76 |
|
|
dispatch(setSelectedUser(users.at(0)))
|
77 |
|
|
}
|
78 |
287652cf
|
Schwobik
|
}
|
79 |
fdf6c32d
|
Schwobik
|
}, [users, dispatch])
|
80 |
|
|
|
81 |
|
|
useEffect(() => {
|
82 |
|
|
if (isRequestCompleted) {
|
83 |
|
|
dispatch(resetIsRequestCompleted())
|
84 |
|
|
// dispatch(clear())
|
85 |
|
|
dispatch(fetchUsers())
|
86 |
|
|
// setSelectedUserId(0)
|
87 |
|
|
// dispatch(setSelectedUser(users.at(0)))
|
88 |
|
|
//
|
89 |
|
|
// if (users.length > 0) {
|
90 |
|
|
// setSelectedUserId(0)
|
91 |
|
|
// dispatch(setSelectedUser(users.at(0)))
|
92 |
|
|
// console.log("selected id: " + selectedUser)
|
93 |
|
|
// }
|
94 |
|
|
}
|
95 |
|
|
}, [isRequestCompleted, dispatch])
|
96 |
287652cf
|
Schwobik
|
|
97 |
b523c74d
|
Schwobik
|
|
98 |
287652cf
|
Schwobik
|
// Use effect to read the error and consume it
|
99 |
|
|
useEffect(() => {
|
100 |
|
|
if (apiError) {
|
101 |
|
|
setDisplayError(apiError)
|
102 |
|
|
dispatch(consumeError())
|
103 |
b523c74d
|
Schwobik
|
}
|
104 |
287652cf
|
Schwobik
|
}, [apiError, dispatch])
|
105 |
b523c74d
|
Schwobik
|
|
106 |
|
|
// Fetch the item from the api after mounting the component
|
107 |
|
|
useEffect(() => {
|
108 |
287652cf
|
Schwobik
|
console.log("before fetch")
|
109 |
|
|
dispatch(fetchUsers())
|
110 |
|
|
console.log("after fetch: " + loading)
|
111 |
|
|
|
112 |
|
|
return () => {
|
113 |
|
|
// Invalidate the state when unmounting so that the old list is not rerendered when the user returns to the page
|
114 |
|
|
dispatch(setLoading())
|
115 |
|
|
console.log("set loading: " + loading)
|
116 |
|
|
}
|
117 |
|
|
}, [dispatch])
|
118 |
b523c74d
|
Schwobik
|
|
119 |
|
|
return (
|
120 |
|
|
<Fragment>
|
121 |
287652cf
|
Schwobik
|
<ShowErrorIfPresent err={displayError} />
|
122 |
|
|
{loading && !apiError ? <ContentLoading /> : null}
|
123 |
|
|
{!loading && users && isAdmin ? (
|
124 |
b523c74d
|
Schwobik
|
<Grid container justifyContent="space-around">
|
125 |
287652cf
|
Schwobik
|
<Grid item xs={6} md={3} sx={{ px: 2 }}>
|
126 |
b523c74d
|
Schwobik
|
<Paper style={{ minHeight: '80vh', display:'flex', justifyContent: 'space-between', flexDirection:'column'}} variant="outlined">
|
127 |
|
|
<List>
|
128 |
|
|
{users.map((user, id) => (
|
129 |
|
|
<ListItem
|
130 |
|
|
key={id}
|
131 |
|
|
className={
|
132 |
287652cf
|
Schwobik
|
selectedUserId === id
|
133 |
b523c74d
|
Schwobik
|
? 'clicked-user'
|
134 |
|
|
: ''
|
135 |
|
|
}
|
136 |
|
|
onClick={() => handleClick(id)}
|
137 |
|
|
>
|
138 |
|
|
<ListItemText primary={user.name} />
|
139 |
|
|
</ListItem>
|
140 |
|
|
))}
|
141 |
|
|
</List>
|
142 |
|
|
<Button startIcon={<AddIcon />}
|
143 |
|
|
variant="contained"
|
144 |
|
|
component={RouterLink}
|
145 |
|
|
to="/register"
|
146 |
|
|
color="primary"
|
147 |
|
|
sx={{ m: 2 }} >
|
148 |
|
|
Add user
|
149 |
|
|
</Button>
|
150 |
|
|
</Paper>
|
151 |
|
|
</Grid>
|
152 |
287652cf
|
Schwobik
|
<Grid item md={9} xs={6}>
|
153 |
|
|
{selectedUserId !== undefined ?
|
154 |
fdf6c32d
|
Schwobik
|
<UserDetail user={selectedUser as UserDto}/>
|
155 |
b523c74d
|
Schwobik
|
: null }
|
156 |
|
|
</Grid>
|
157 |
|
|
</Grid>
|
158 |
|
|
) : null}
|
159 |
|
|
</Fragment>
|
160 |
|
|
)
|
161 |
|
|
}
|
162 |
|
|
|
163 |
|
|
export default Administration
|