Projekt

Obecné

Profil

« Předchozí | Další » 

Revize ae770857

Přidáno uživatelem Michal Schwob před téměř 3 roky(ů)

Sources page implemented
re #9811

Zobrazit rozdíly:

frontend/src/App.tsx
18 18
import { Fragment } from 'react'
19 19
import Administration from "./features/Administration/Administration"
20 20
import Register from "./features/Auth/Register"
21
import Bibliography from "./features/Bibliography/Bibliography"
22
import EditBibliography from "./features/Bibliography/EditBibliography"
21 23

  
22 24
const App = () => {
23 25
    return (
......
39 41
                            <Route path="/logout" element={<Logout />} />
40 42
                            <Route path="/map" element={<TrackingTool />} />
41 43
                            <Route path="/administration" element={<Administration />}/>
44
                            <Route path="/sources" element={<Bibliography />}/>
45
                            <Route path="/editSources" element={<EditBibliography />}/>
42 46
                            <Route path="*" element={<NotFound />} />
43 47
                        </Routes>
44 48
                    </Box>
frontend/src/features/Bibliography/Bibliography.tsx
1
import {Button, Container, Paper, Typography} from '@mui/material'
2
import {Fragment, useEffect, useState} from 'react'
3
import { useDispatch, useSelector } from 'react-redux'
4
import { logout } from '../Auth/userSlice'
5
import { RootState } from '../redux/store'
6
import {SourcesDto, TitlePageDto} from "../../swagger/data-contracts"
7
import axiosInstance from "../../api/api"
8
import 'react-quill/dist/quill.snow.css'
9
import ContentLoading from "../Reusables/ContentLoading"
10
import EditBibliography from "./EditBibliography"
11
import {Link as RouterLink, useNavigate} from "react-router-dom"
12
import EditIcon from '@mui/icons-material/Edit'
13
import { formatHtmlStringToReactDom } from "../../utils/formatting/HtmlUtils"
14

  
15

  
16
const apiError =
17
    'Error while fetching data from the server, please try again later.'
18

  
19
const Bibliography = () => {
20
    const [sources, setSources] = useState<SourcesDto | undefined>(undefined)
21
    const [isContentLoading, setIsContentLoading] = useState(true)
22
    const [err, setErr] = useState<string | undefined>(undefined)
23

  
24
    let navigate = useNavigate()
25
    const openEditHomePage = () => {
26
        let path = '/editBibliography'
27
        navigate(path)
28
    }
29

  
30
    const isAdmin = useSelector(
31
        (state: RootState) => state.user.roles.includes("ROLE_ADMIN")
32
    )
33
    // Fetch the item from the api after mounting the component
34
    useEffect(() => {
35
        // Function to fetch the item from the api
36
        const fetchItem = async () => {
37
            try {
38
                const { data, status } = await axiosInstance.get(
39
                    `/sources`
40
                )
41
                if (status !== 200) {
42
                    setErr(apiError)
43
                    return
44
                }
45

  
46
                setSources(data)
47
                setIsContentLoading(false)
48
            } catch (err: any) {
49
                setErr(apiError)
50
            }
51
        }
52
        fetchItem()
53
    }, [])
54

  
55
    return (
56
        <Fragment>
57
            <Typography variant="h3" sx={{ mb: 2 }} fontWeight="bold">
58
                Sources
59
            </Typography>
60
            <Paper style={{ minHeight: '80vh', display:'flex', justifyContent: 'space-between', flexDirection:'column'}} variant="outlined">
61
                {isContentLoading && !err ? <ContentLoading /> : null}
62
                {!isContentLoading && sources ? (
63
                    <Container sx={{ mt: 4 }}>
64
                        {formatHtmlStringToReactDom(sources["sources"] as string)}
65
                    </Container>
66
                ) : null}
67

  
68
                {isAdmin ? (
69
                    <Container sx={{ mb: 3 }} >
70
                            <Button
71
                                startIcon={<EditIcon />}
72
                                variant="contained"
73
                                component={RouterLink}
74
                                to="/editSources"
75
                                color="primary"
76
                                sx={{ mb: 2 }}
77
                            >
78
                                Edit
79
                            </Button>
80
                    </Container>
81
                ) : null}
82
            </Paper>
83
        </Fragment>
84
    )
85
}
86

  
87
export default Bibliography
frontend/src/features/Bibliography/EditBibliography.tsx
1
import {Button, Paper} from '@mui/material'
2
import {Fragment, useEffect, useState} from 'react'
3
import { useDispatch, useSelector } from 'react-redux'
4
import { logout } from '../Auth/userSlice'
5
import { RootState } from '../redux/store'
6
import {SourcesDto} from "../../swagger/data-contracts"
7
import axiosInstance from "../../api/api"
8
import ReactQuill from "react-quill"
9
import 'react-quill/dist/quill.snow.css'
10
import ContentLoading from "../Reusables/ContentLoading"
11
import {Link as RouterLink, useNavigate} from "react-router-dom"
12
import ArrowBackIosIcon from "@mui/icons-material/ArrowBackIos"
13

  
14

  
15
const apiError =
16
    'Error while fetching data from the server, please try again later.'
17

  
18
const EditBibliography = () => {
19
    const [sources, setSources] = useState<SourcesDto | undefined>(undefined)
20
    const [isContentLoading, setIsContentLoading] = useState(true)
21
    const [err, setErr] = useState<string | undefined>(undefined)
22

  
23
    const navigate = useNavigate()
24

  
25
    const updateContent = (updated: string) => {
26
        if (sources) {
27
            sources["sources"] = updated
28
        }
29
    }
30

  
31
    const handleSubmit = () => {
32
        postData()
33
    }
34

  
35
    const postData = async () => {
36
        const {data, status} = await axiosInstance.post('/sources', sources)
37
        if (status != 200) {
38
            console.error("Sources page data submitting failed.")
39
        }
40
    }
41

  
42
    const roles = useSelector(
43
        (state: RootState) => state.user.roles
44
    )
45

  
46
    // Fetch the item from the api after mounting the component
47
    useEffect(() => {
48
        // Function to fetch the item from the api
49
        const fetchItem = async () => {
50
            try {
51
                const { data, status } = await axiosInstance.get(
52
                    `/sources`
53
                )
54
                if (status !== 200) {
55
                    setErr(apiError)
56
                    return
57
                }
58

  
59
                setSources(data)
60
                setIsContentLoading(false)
61
            } catch (err: any) {
62
                setErr(apiError)
63
            }
64
        }
65

  
66
        fetchItem()
67
    }, [])
68

  
69
    return (
70
        <Fragment>
71
            <Button
72
                startIcon={<ArrowBackIosIcon />}
73
                variant="contained"
74
                component={RouterLink}
75
                to="/sources"
76
                color="primary"
77
                sx={{ mb: 2 }}
78
            >
79
                Return To Bibliography
80
            </Button>
81
            <Paper style={{ minHeight: '100vh' }} variant="outlined">
82
    {isContentLoading && !err ? <ContentLoading /> : null}
83
    {!isContentLoading && sources ? (
84
        <form>
85
            <Button type="button" onClick={handleSubmit}>Submit</Button>
86
            <ReactQuill
87
                theme='snow'
88
                value={sources["sources"]}
89
                onChange={updateContent}
90
                style={{minHeight: '300px'}}
91
                />
92
        </form>
93
    ) : null}
94
    </Paper>
95
    </Fragment>
96
)
97
}
98

  
99
export default EditBibliography

Také k dispozici: Unified diff