1
|
import {
|
2
|
Button,
|
3
|
Grid,
|
4
|
List,
|
5
|
ListItem,
|
6
|
ListItemText,
|
7
|
Paper,
|
8
|
} from '@mui/material'
|
9
|
import {Fragment, useEffect, useState} from 'react'
|
10
|
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
|
import {fetchUsers} from "./userDetailThunks"
|
19
|
import {
|
20
|
clear,
|
21
|
consumeError,
|
22
|
resetIsRequestCompleted,
|
23
|
setLoading,
|
24
|
setSelectedUser
|
25
|
} from "./userDetailSlice"
|
26
|
import ShowErrorIfPresent from "../Reusables/ShowErrorIfPresent"
|
27
|
import {number} from "yup"
|
28
|
|
29
|
const apiError =
|
30
|
'Error while fetching data from the server, please try again later.'
|
31
|
|
32
|
const Administration = () => {
|
33
|
// 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
|
const isRequestCompleted = useSelector((state: RootState) => state.usersDetail.isRequestCompleted)
|
39
|
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
|
|
48
|
const isAdmin = useSelector(
|
49
|
(state: RootState) => state.user.roles.includes("ROLE_ADMIN")
|
50
|
)
|
51
|
|
52
|
const handleClick = (userID: number) => {
|
53
|
console.log("clicked "+ userID)
|
54
|
if (users) {
|
55
|
setSelectedUserId(userID)
|
56
|
dispatch(setSelectedUser(users.at(userID)))
|
57
|
console.log("after " + selectedUser)
|
58
|
}
|
59
|
}
|
60
|
|
61
|
// Use effect to read the error and consume it
|
62
|
useEffect(() => {
|
63
|
if (users.length > 0) {
|
64
|
// 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
|
}
|
79
|
}, [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
|
|
97
|
|
98
|
// Use effect to read the error and consume it
|
99
|
useEffect(() => {
|
100
|
if (apiError) {
|
101
|
setDisplayError(apiError)
|
102
|
dispatch(consumeError())
|
103
|
}
|
104
|
}, [apiError, dispatch])
|
105
|
|
106
|
// Fetch the item from the api after mounting the component
|
107
|
useEffect(() => {
|
108
|
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
|
|
119
|
return (
|
120
|
<Fragment>
|
121
|
<ShowErrorIfPresent err={displayError} />
|
122
|
{loading && !apiError ? <ContentLoading /> : null}
|
123
|
{!loading && users && isAdmin ? (
|
124
|
<Grid container justifyContent="space-around">
|
125
|
<Grid item xs={6} md={3} sx={{ px: 2 }}>
|
126
|
<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
|
selectedUserId === id
|
133
|
? '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
|
<Grid item md={9} xs={6}>
|
153
|
{selectedUserId !== undefined ?
|
154
|
<UserDetail user={selectedUser as UserDto}/>
|
155
|
: null }
|
156
|
</Grid>
|
157
|
</Grid>
|
158
|
) : null}
|
159
|
</Fragment>
|
160
|
)
|
161
|
}
|
162
|
|
163
|
export default Administration
|