1
|
import {
|
2
|
Button,
|
3
|
Grid,
|
4
|
Checkbox,
|
5
|
Paper,
|
6
|
Typography,
|
7
|
FormGroup,
|
8
|
FormControlLabel
|
9
|
} from '@mui/material'
|
10
|
import React, {Fragment, useEffect, useState} from 'react'
|
11
|
import axiosInstance from "../../api/api"
|
12
|
import 'react-quill/dist/quill.snow.css'
|
13
|
import {PermissionDto, UserDto} from "../../swagger/data-contracts"
|
14
|
import ClearIcon from "@mui/icons-material/Clear"
|
15
|
import CheckIcon from '@mui/icons-material/Check';
|
16
|
import {useDispatch, useSelector} from "react-redux"
|
17
|
import {deleteUser, savePermissions} from "./userDetailThunks"
|
18
|
import {RootState} from "../redux/store"
|
19
|
import {setSelectedUserPermissions} from "./userDetailSlice"
|
20
|
import AddIcon from "@mui/icons-material/Add"
|
21
|
import {Link as RouterLink} from "react-router-dom"
|
22
|
import ManageAccountsIcon from '@mui/icons-material/ManageAccounts';
|
23
|
|
24
|
export interface UserDetailProps {
|
25
|
user: UserDto
|
26
|
}
|
27
|
|
28
|
const UserDetail = (props: UserDetailProps) => {
|
29
|
const selectedUser: UserDto | undefined = useSelector((state: RootState) => state.usersDetail.selectedUser)
|
30
|
const [canRead, setCanRead] = useState(selectedUser?.permissions?.canRead)
|
31
|
const [canWrite, setCanWrite] = useState(selectedUser?.permissions?.canWrite)
|
32
|
const [canDelete, setCanDelete] = useState(selectedUser?.permissions?.canDelete)
|
33
|
|
34
|
const dispatch = useDispatch()
|
35
|
|
36
|
useEffect(()=> {
|
37
|
setCanRead(selectedUser?.permissions?.canRead)
|
38
|
setCanWrite(selectedUser?.permissions?.canWrite)
|
39
|
setCanDelete(selectedUser?.permissions?.canDelete)
|
40
|
}, [selectedUser]);
|
41
|
|
42
|
// Maps user property to corresponding table row
|
43
|
const mapToRow = (rowName: string, item: string) => (
|
44
|
<Fragment>
|
45
|
<Grid sx={{my: 2}} container justifyContent="space-around">
|
46
|
<Grid item xs={4} md={3} lg={2} sx={{px: 1}}>
|
47
|
<Typography fontWeight={500}>{rowName}</Typography>
|
48
|
</Grid>
|
49
|
<Grid item xs={8} md={9} lg={10} sx={{ml: 'auto'}}>
|
50
|
<Typography key={item}>{item}</Typography>
|
51
|
</Grid>
|
52
|
</Grid>
|
53
|
</Fragment>
|
54
|
)
|
55
|
|
56
|
const rows = [
|
57
|
{
|
58
|
rowName: 'Name:',
|
59
|
item: [props.user?.name],
|
60
|
},
|
61
|
{
|
62
|
rowName: 'E-mail:',
|
63
|
item: selectedUser?.email,
|
64
|
}
|
65
|
]
|
66
|
const rights = {
|
67
|
canWrite: 'Add and edit locations',
|
68
|
canDelete: 'Delete locations',
|
69
|
canRead: 'Access to tracking tool',
|
70
|
}
|
71
|
|
72
|
|
73
|
const prepareDeleteUser = async () => {
|
74
|
dispatch(deleteUser())
|
75
|
}
|
76
|
|
77
|
const prepareSavePermissions = () => {
|
78
|
dispatch(setSelectedUserPermissions({canRead: canRead, canWrite: canWrite, canDelete: canDelete}))
|
79
|
dispatch(savePermissions())
|
80
|
}
|
81
|
|
82
|
return (
|
83
|
<Fragment>
|
84
|
<Paper style={{minHeight: '80vh'}} variant="outlined">
|
85
|
<Grid container justifyContent="space-around">
|
86
|
<Grid item xs={12} md={7} sx={{p: 2}}>
|
87
|
<Typography fontWeight={800}>Information</Typography>
|
88
|
{rows.map((row) => (
|
89
|
|
90
|
<Fragment>
|
91
|
{mapToRow(
|
92
|
row.rowName as string,
|
93
|
row.item as string
|
94
|
)}
|
95
|
</Fragment>
|
96
|
))}
|
97
|
</Grid>
|
98
|
<Grid item xs={12} md={5} sx={{p: 2}}>
|
99
|
<Typography fontWeight={800}>Rights</Typography>
|
100
|
<FormGroup>
|
101
|
<FormControlLabel
|
102
|
control={
|
103
|
<Checkbox
|
104
|
checked={canWrite}
|
105
|
onChange={e => setCanWrite(e.target.checked)}
|
106
|
/>}
|
107
|
label={rights.canWrite}/>
|
108
|
<FormControlLabel
|
109
|
control={
|
110
|
<Checkbox
|
111
|
checked={canRead}
|
112
|
onChange={e => setCanRead(e.target.checked)}
|
113
|
/>}
|
114
|
label={rights.canRead}/>
|
115
|
<FormControlLabel
|
116
|
control={
|
117
|
<Checkbox
|
118
|
checked={canDelete}
|
119
|
onChange={e => setCanDelete(e.target.checked)}
|
120
|
/>}
|
121
|
label={rights.canDelete}/>
|
122
|
</FormGroup>
|
123
|
</Grid>
|
124
|
</Grid>
|
125
|
<Button startIcon={<CheckIcon />}
|
126
|
variant="contained"
|
127
|
color="primary"
|
128
|
onClick={prepareSavePermissions}
|
129
|
sx={{ m: 2 }} >
|
130
|
Save
|
131
|
</Button>
|
132
|
<Button startIcon={<ClearIcon />}
|
133
|
variant="contained"
|
134
|
color="primary"
|
135
|
onClick={prepareDeleteUser}
|
136
|
sx={{ m: 2 }} >
|
137
|
Delete
|
138
|
</Button>
|
139
|
<Button startIcon={<ManageAccountsIcon />}
|
140
|
variant="contained"
|
141
|
component={RouterLink}
|
142
|
to="/changePassword"
|
143
|
color="primary"
|
144
|
sx={{ m: 2 }} >
|
145
|
Change Password
|
146
|
</Button>
|
147
|
</Paper>
|
148
|
</Fragment>
|
149
|
)
|
150
|
}
|
151
|
|
152
|
export default UserDetail
|