Projekt

Obecné

Profil

Stáhnout (4.64 KB) Statistiky
| Větev: | Tag: | Revize:
1
import {
2
    Button,
3
    Divider,
4
    Grid,
5
    Paper,
6
    Typography,
7
} from '@mui/material'
8
import { Fragment, useEffect, useState } from 'react'
9
import { useParams } from 'react-router-dom'
10
import axiosInstance from '../../api/api'
11
import { CatalogItemDto } from '../../swagger/data-contracts'
12
import ShowErrorIfPresent from '../Reusables/ShowErrorIfPresent'
13
import ContentLoading from '../Reusables/ContentLoading'
14
import CatalogItemMap from './CatalogItemMap'
15
import ArrowBackIosIcon from '@mui/icons-material/ArrowBackIos'
16
import { Link as RouterLink } from 'react-router-dom'
17

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

    
21
const CatalogItemDetail = () => {
22
    // itemId from query params
23
    const { itemId } = useParams()
24

    
25
    const [item, setItem] = useState<CatalogItemDto | undefined>(undefined)
26
    const [isItemLoading, setIsItemLoading] = useState(true)
27
    const [err, setErr] = useState<string | undefined>(undefined)
28

    
29
    // Fetch the item from the api after mounting the component
30
    useEffect(() => {
31
        // Function to fetch the item from the api
32
        const fetchItem = async () => {
33
            try {
34
                const { data, status } = await axiosInstance.get(
35
                    `/catalog-items/${itemId}`
36
                )
37
                if (status !== 200) {
38
                    setErr(apiError)
39
                    return
40
                }
41

    
42
                setItem(data)
43
                setIsItemLoading(false)
44
            } catch (err: any) {
45
                setErr(apiError)
46
            }
47
        }
48

    
49
        fetchItem()
50
    }, [itemId])
51

    
52
    // Maps catalogItem property to corresponding table row
53
    const mapToRow = (rowName: string, items: string[]) => (
54
        <Fragment>
55
            <Grid sx={{ my: 2 }} container justifyContent="space-around">
56
                <Grid item xs={8} sx={{ px: 1 }}>
57
                    <Typography fontWeight={500}>{rowName}</Typography>
58
                </Grid>
59
                <Grid item xs={4} sx={{ ml: 'auto' }}>
60
                    {items.map((item) => (
61
                        <Typography key={item}>{item}</Typography>
62
                    ))}
63
                </Grid>
64
            </Grid>
65
        </Fragment>
66
    )
67

    
68
    // Catalog item rows
69
    const rows = [
70
        {
71
            rowName: 'Name',
72
            items: [item?.name],
73
        },
74
        {
75
            rowName: 'Alternative Names',
76
            items: item?.alternativeNames,
77
        },
78
        {
79
            rowName: 'Written Forms',
80
            items: item?.writtenForms,
81
        },
82
        {
83
            rowName: 'Type',
84
            items: item?.types,
85
        },
86
        {
87
            rowName: 'State or Territory',
88
            items: item?.countries,
89
        },
90
        {
91
            rowName: 'Coordinates',
92
            items: [`${item?.longitude}°, ${item?.latitude}°`],
93
        },
94
        {
95
            rowName: 'Certainty',
96
            items: [item?.certainty],
97
        },
98
        {
99
            rowName: 'Bibliography',
100
            items: item?.bibliography,
101
        },
102
    ]
103

    
104
    return (
105
        // TODO remove min height
106
        <Fragment>
107
            <Button
108
                startIcon={<ArrowBackIosIcon />}
109
                variant="contained"
110
                component={RouterLink}
111
                to="/catalog"
112
                color="primary"
113
                sx={{ mb: 2 }}
114
            >
115
                Return To Catalog
116
            </Button>
117
            <ShowErrorIfPresent err={err} />
118

    
119
            <Paper style={{ minHeight: '100vh' }} variant="outlined">
120

    
121
                {isItemLoading && !err ? <ContentLoading /> : null}
122
                {!isItemLoading && item ? (
123
                    <Grid container justifyContent="space-around">
124
                        <Grid item xs={12} md={6} sx={{ px: 2 }}>
125
                            {rows.map((row, idx) => {
126
                                const maxIdx = rows.length - 1
127
                                return (
128
                                    <Fragment>
129
                                        {mapToRow(
130
                                            row.rowName as string,
131
                                            row.items as string[]
132
                                        )}
133
                                        {idx === maxIdx ? null : <Divider />}
134
                                    </Fragment>
135
                                )
136
                            })}
137
                        </Grid>
138

    
139
                        <Grid item md={6} xs={12}>
140
                            <CatalogItemMap item={item} />
141
                        </Grid>
142
                    </Grid>
143
                ) : null}
144
            </Paper>
145
        </Fragment>
146
    )
147
}
148

    
149
export default CatalogItemDetail
(3-3/7)