Projekt

Obecné

Profil

Stáhnout (5.02 KB) Statistiky
| Větev: | Tag: | Revize:
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
    setLoading,
23
    setSelectedUser
24
} from "./userDetailSlice"
25
import ShowErrorIfPresent from "../Reusables/ShowErrorIfPresent"
26
import {number} from "yup"
27

    
28
const apiError =
29
    'Error while fetching data from the server, please try again later.'
30

    
31
const Administration = () => {
32
    // Items, loading and error from api
33
    const users = useSelector((state: RootState) => state.usersDetail.users)
34
    const loading = useSelector((state: RootState) => state.usersDetail.loading)
35
    const apiError = useSelector((state: RootState) => state.usersDetail.error)
36
    const selectedUser = useSelector((state: RootState) => state.usersDetail.selectedUser)
37
    const [selectedUserId, setSelectedUserId] = useState<number | undefined>(undefined)
38

    
39
    // Local state to display any error relevant error
40
    const [displayError, setDisplayError] = useState<string | undefined>(
41
        undefined
42
    )
43

    
44
    const dispatch = useDispatch()
45

    
46
    const isAdmin = useSelector(
47
        (state: RootState) => state.user.roles.includes("ROLE_ADMIN")
48
    )
49

    
50
    const handleClick = (userID: number) => {
51
        console.log("clicked "+ userID)
52
        if (users) {
53
            setSelectedUserId(userID)
54
            dispatch(setSelectedUser(users.at(userID)))
55
            console.log("after " + selectedUser)
56
        }
57
    }
58

    
59
    // Use effect to read the error and consume it
60
    useEffect(() => {
61
        if (users.length > 0) {
62
            setSelectedUserId(0)
63
            dispatch(setSelectedUser(users.at(0)))
64
            console.log("selected id: " + selectedUser)
65
        }
66
    }, [users])
67

    
68

    
69
    // Use effect to read the error and consume it
70
    useEffect(() => {
71
        if (apiError) {
72
            setDisplayError(apiError)
73
            dispatch(consumeError())
74
        }
75
    }, [apiError, dispatch])
76

    
77
    // Fetch the item from the api after mounting the component
78
    useEffect(() => {
79
        console.log("before fetch")
80
        dispatch(fetchUsers())
81
        console.log("after fetch: " + loading)
82

    
83
        return () => {
84
            // Invalidate the state when unmounting so that the old list is not rerendered when the user returns to the page
85
            dispatch(setLoading())
86
            console.log("set loading: " + loading)
87
        }
88
    }, [dispatch])
89

    
90
    const childClosed = () => {
91
        dispatch(clear())
92
        dispatch(fetchUsers())
93
        setSelectedUserId(0)
94
        dispatch(setSelectedUser(users.at(0)))
95
    }
96

    
97
    return (
98
        <Fragment>
99
            <ShowErrorIfPresent err={displayError} />
100
            {loading && !apiError ? <ContentLoading /> : null}
101
            {!loading && users && isAdmin ? (
102
                <Grid container justifyContent="space-around">
103
                    <Grid item xs={6} md={3} sx={{ px: 2 }}>
104
                        <Paper style={{ minHeight: '80vh', display:'flex', justifyContent: 'space-between', flexDirection:'column'}} variant="outlined">
105
                            <List>
106
                                {users.map((user, id) => (
107
                                    <ListItem
108
                                        key={id}
109
                                        className={
110
                                            selectedUserId === id
111
                                                ? 'clicked-user'
112
                                                : ''
113
                                        }
114
                                        onClick={() => handleClick(id)}
115
                                    >
116
                                        <ListItemText primary={user.name} />
117
                                    </ListItem>
118
                                ))}
119
                            </List>
120
                            <Button startIcon={<AddIcon />}
121
                                    variant="contained"
122
                                    component={RouterLink}
123
                                    to="/register"
124
                                    color="primary"
125
                                    sx={{ m: 2 }} >
126
                                Add user
127
                            </Button>
128
                        </Paper>
129
                    </Grid>
130
                    <Grid item md={9} xs={6}>
131
                        {selectedUserId !== undefined  ?
132
                            <UserDetail user={selectedUser as UserDto} onClose={childClosed} />
133
                        : null }
134
                    </Grid>
135
                </Grid>
136
            ) : null}
137
        </Fragment>
138
    )
139
}
140

    
141
export default Administration
(1-1/4)