Projekt

Obecné

Profil

Stáhnout (4.47 KB) Statistiky
| Větev: | Tag: | Revize:
1
import {
2
    Box,
3
    Divider,
4
    Grid,
5
    Paper,
6
    Skeleton,
7
    Stack,
8
    Typography,
9
} from '@mui/material'
10
import { Fragment, useEffect } from 'react'
11
import { useDispatch, useSelector } from 'react-redux'
12
import { useParams } from 'react-router-dom'
13
import { RootState } from '../redux/store'
14
import ConsumeLastError from '../Reusables/ConsumeLastError'
15
import ContentLoading from '../Reusables/ContentLoading'
16
import { consumeCatalogItemLastErr, setItemLoading } from './catalogSlice'
17
import { getCatalogItem } from './catalogThunks'
18

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

    
23
    // Loaded item
24
    const item = useSelector((state: RootState) => state.catalog.catalogItem)
25

    
26
    // Whether the item is loading
27
    const isItemLoading = useSelector(
28
        (state: RootState) => state.catalog.isItemLoading
29
    )
30

    
31
    // Redux dispatch
32
    const dispatch = useDispatch()
33

    
34
    const lastErr = useSelector(
35
        (state: RootState) => state.catalog.catalogItemLastErr
36
    )
37

    
38
    // Fetch the item from the api after mounting the component
39
    useEffect(() => {
40
        dispatch(setItemLoading(true))
41
        dispatch(getCatalogItem(itemId as string))
42
    }, [dispatch, itemId])
43

    
44
    // Maps catalogItem property to corresponding table row
45
    const mapToRow = (rowName: string, items: string[]) => (
46
        <Fragment>
47
            <Grid sx={{ my: 2 }} container justifyContent="space-around">
48
                <Grid item xs={8} sx={{ px: 1 }}>
49
                    <Typography fontWeight={500}>{rowName}</Typography>
50
                </Grid>
51
                <Grid item xs={4} sx={{ ml: 'auto' }}>
52
                    {items.map((item) => (
53
                        <Typography>{item}</Typography>
54
                    ))}
55
                </Grid>
56
            </Grid>
57
        </Fragment>
58
    )
59

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

    
96
    return (
97
        // TODO remove min height
98
        <Paper style={{ minHeight: '100vh', borderRadius: 0 }} elevation={2}>
99
            <ConsumeLastError
100
                consumeFn={consumeCatalogItemLastErr}
101
                err={lastErr}
102
            />
103

    
104
            {isItemLoading ? <ContentLoading /> : null}
105
            {!isItemLoading && item ? (
106
                <Grid container justifyContent="space-around">
107
                    <Grid item xs={6} sx={{ px: 2 }}>
108
                        {rows.map((row, idx) => {
109
                            const maxIdx = rows.length - 1
110
                            return (
111
                                <Fragment>
112
                                    {mapToRow(
113
                                        row.rowName as string,
114
                                        row.items as string[]
115
                                    )}
116
                                    {idx === maxIdx ? null : <Divider />}
117
                                </Fragment>
118
                            )
119
                        })}
120
                    </Grid>
121

    
122
                    <Grid item xs={6}>
123
                        <Box sx={{ px: 2, py: 4 }}>
124
                            <Typography
125
                                variant="h4"
126
                                sx={{ mb: 4 }}
127
                                fontWeight="bold"
128
                            >
129
                                Map
130
                            </Typography>
131

    
132
                            <Skeleton
133
                                animation="pulse"
134
                                variant="rectangular"
135
                                width="100%"
136
                                height={400}
137
                            />
138
                        </Box>
139
                    </Grid>
140
                </Grid>
141
            ) : null}
142
        </Paper>
143
    )
144
}
145

    
146
export default CatalogItemDetail
(2-2/5)