Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 287652cf

Přidáno uživatelem Michal Schwob před asi 2 roky(ů)

Refactor of administration page and user registration
re #9627

Zobrazit rozdíly:

frontend/src/features/Administration/Administration.tsx
1 1
import {
2 2
    Button,
3
    Container, Grid,
4
    Link,
3
    Grid,
5 4
    List,
6 5
    ListItem,
7
    ListItemIcon,
8 6
    ListItemText,
9
    NativeSelect,
10 7
    Paper,
11
    Select, SelectChangeEvent
12 8
} from '@mui/material'
13
import {Fragment, ReactNode, useEffect, useState} from 'react'
9
import {Fragment, useEffect, useState} from 'react'
14 10
import { useDispatch, useSelector } from 'react-redux'
15 11
import { RootState } from '../redux/store'
16
import axiosInstance from "../../api/api"
17 12
import 'react-quill/dist/quill.snow.css'
18 13
import ContentLoading from "../Reusables/ContentLoading"
19 14
import {UserDto} from "../../swagger/data-contracts"
20 15
import UserDetail from "./UserDetail"
21
import EditIcon from "@mui/icons-material/Edit"
22 16
import {Link as RouterLink} from "react-router-dom"
23 17
import AddIcon from '@mui/icons-material/Add';
24

  
25

  
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"
26 27

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

  
30 31
const Administration = () => {
31
    const [users, setUsers] = useState<UserDto[] | undefined>(undefined)
32
    const [selectedUser, setSelectedUser] = useState<UserDto | undefined>(undefined)
33
    const [selectedUserID, setSelectedUserID] = useState<number | undefined>(undefined)
34
    const [isContentLoading, setIsContentLoading] = useState(true)
35
    const [err, setErr] = useState<string | undefined>(undefined)
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()
36 45

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

  
41 50
    const handleClick = (userID: number) => {
42
        console.log("clicked " + userID)
51
        console.log("clicked "+ userID)
43 52
        if (users) {
44
            setSelectedUser(users.at(userID))
45
            setSelectedUserID(userID)
53
            setSelectedUserId(userID)
54
            dispatch(setSelectedUser(users.at(userID)))
55
            console.log("after " + selectedUser)
46 56
        }
47 57
    }
48 58

  
49
    // Function to fetch the item from the api
50
    const fetchItem = async () => {
51
        try {
52
            const { data, status } = await axiosInstance.get(
53
                `/users`
54
            )
55
            if (status !== 200) {
56
                setErr(apiError)
57
                return
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

  
59 68

  
60
            setUsers(data)
61
            setIsContentLoading(false)
62
        } catch (err: any) {
63
            setErr(apiError)
69
    // Use effect to read the error and consume it
70
    useEffect(() => {
71
        if (apiError) {
72
            setDisplayError(apiError)
73
            dispatch(consumeError())
64 74
        }
65
    }
75
    }, [apiError, dispatch])
66 76

  
67 77
    // Fetch the item from the api after mounting the component
68 78
    useEffect(() => {
69
        fetchItem()
70
    }, [])
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])
71 89

  
72 90
    const childClosed = () => {
73
        setSelectedUser(undefined)
74
        setSelectedUserID(undefined)
75
        fetchItem()
91
        dispatch(clear())
92
        dispatch(fetchUsers())
93
        setSelectedUserId(0)
94
        dispatch(setSelectedUser(users.at(0)))
76 95
    }
77 96

  
78 97
    return (
79 98
        <Fragment>
80

  
81
            {isContentLoading && !err ? <ContentLoading /> : null}
82
            {!isContentLoading && users && isAdmin ? (
99
            <ShowErrorIfPresent err={displayError} />
100
            {loading && !apiError ? <ContentLoading /> : null}
101
            {!loading && users && isAdmin ? (
83 102
                <Grid container justifyContent="space-around">
84
                    <Grid item xs={3} md={2} sx={{ px: 2 }}>
103
                    <Grid item xs={6} md={3} sx={{ px: 2 }}>
85 104
                        <Paper style={{ minHeight: '80vh', display:'flex', justifyContent: 'space-between', flexDirection:'column'}} variant="outlined">
86 105
                            <List>
87 106
                                {users.map((user, id) => (
88 107
                                    <ListItem
89 108
                                        key={id}
90 109
                                        className={
91
                                            selectedUserID === id
110
                                            selectedUserId === id
92 111
                                                ? 'clicked-user'
93 112
                                                : ''
94 113
                                        }
......
108 127
                            </Button>
109 128
                        </Paper>
110 129
                    </Grid>
111
                    <Grid item md={10} xs={9}>
112
                        {selectedUser ?
113
                            <UserDetail user={selectedUser} onClose={childClosed} />
130
                    <Grid item md={9} xs={6}>
131
                        {selectedUserId !== undefined  ?
132
                            <UserDetail user={selectedUser as UserDto} onClose={childClosed} />
114 133
                        : null }
115 134
                    </Grid>
116 135
                </Grid>

Také k dispozici: Unified diff