Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 8c57f958

Přidáno uživatelem Václav Honzík před asi 2 roky(ů)

refactor to use Redux instead of localstate

re #9629

Zobrazit rozdíly:

frontend/src/features/TrackingTool/MapPath.tsx
1
import { Fragment, FunctionComponent, useState } from 'react'
1
import { Fragment, FunctionComponent, useEffect, useState } from 'react'
2 2
import { CatalogItemDto } from '../../swagger/data-contracts'
3 3
import { PathVariant } from './buildPathVariants'
4 4
import TextPath from 'react-leaflet-textpath'
......
6 6
import { Checkbox, FormControlLabel, Stack, Typography } from '@mui/material'
7 7
import { formatHtmlStringToReactDom } from '../../utils/formatting/HtmlUtils'
8 8
import { DialogCatalogItemDetail as CatalogItemDetailDialog } from '../Catalog/CatalogItemDetail'
9
import { useDispatch, useSelector } from 'react-redux'
10
import { RootState } from '../redux/store'
9 11

  
10 12
// CatalogItemDto wrapper to keep track whether the item is active or not
11 13
class DisplayableMapPoint {
......
17 19

  
18 20
export interface MapPathProps {
19 21
    pathVariant: PathVariant // aka CatalogItemDto[]
20
    active: boolean // whether this component is active
21 22
    idx: number // index of path in the list
22
    primaryIdx: number // reference to index of path which has primary color
23
    setPrimary: (idx: number) => void // callback to set the primary path
24 23
}
25 24

  
26 25
// Blue
......
29 28
// Grey
30 29
export const secondaryPathColor = '#878e9c'
31 30

  
32
// Map path component 
33
const MapPath: FunctionComponent<MapPathProps> = ({
34
    pathVariant,
35
    active,
36
    primaryIdx,
37
    idx,
38
    setPrimary
39
}) => {
31
// Map path component
32
const MapPath: FunctionComponent<MapPathProps> = ({ idx, pathVariant }) => {
40 33
    // List of all map points that belong to the path
41 34
    const [mapPoints, setMapPoints] = useState<DisplayableMapPoint[]>(
42 35
        pathVariant
43 36
            .filter((item) => item.latitude && item.longitude)
44 37
            .map((item) => new DisplayableMapPoint(item))
45 38
    )
46
    
39

  
40
    // Set of all active paths
41
    const activePaths = useSelector(
42
        (state: RootState) => state.trackingTool.activePaths
43
    )
44
    // Index of the primary path
45
    const primaryPathIdx = useSelector(
46
        (state: RootState) => state.trackingTool.primaryPathIdx
47
    )
48

  
49
    // Whether the path is active or not
50
    const [active, setActive] = useState(false)
51
    useEffect(() => {
52
        setActive(activePaths.has(idx))
53
    }, [activePaths, idx])
47 54

  
48 55
    const getActiveMapPoints = () => mapPoints.filter((item) => item.active)
49 56

  
57
    const dispatch = useDispatch()
58

  
50 59
    // Builds all edges of the path
51 60
    const buildEdges = () => {
52 61
        const activeMapPoints = getActiveMapPoints()
......
73 82
                    attributes={{
74 83
                        'font-size': 25,
75 84
                        // Set to primaryPathColor if primary index in the tracking tool is equal to this index
76
                        fill: primaryIdx === idx ? primaryPathColor : secondaryPathColor,
85
                        fill:
86
                            primaryPathIdx === idx
87
                                ? primaryPathColor
88
                                : secondaryPathColor,
77 89
                    }}
78 90
                    onClick={() => {
79
                        setPrimary(idx)
80
                        console.log('hehehe')
81
                    }
82
                    }
91
                        dispatch(setPrimaryIdx(idx))
92
                    }}
83 93
                    repeat
84 94
                    center
85 95
                    weight={9}
......
111 121
                                fontWeight="bold"
112 122
                                fontSize={16}
113 123
                            >
114
                                {formatHtmlStringToReactDom(mapPoint.catalogItem.name as string)}
124
                                {formatHtmlStringToReactDom(
125
                                    mapPoint.catalogItem.name as string
126
                                )}
115 127
                            </Typography>
116 128
                            <FormControlLabel
117 129
                                control={
......
126 138
                                labelPlacement="end"
127 139
                                label="Active"
128 140
                            />
129
                            <CatalogItemDetailDialog itemId={mapPoint.catalogItem.id ?? ''} />
141
                            <CatalogItemDetailDialog
142
                                itemId={mapPoint.catalogItem.id ?? ''}
143
                            />
130 144
                        </Stack>
131 145
                    </Fragment>
132 146
                </Popup>
......
136 150

  
137 151
    return (
138 152
        <Fragment>
139
            {active && <Fragment>
140
                {buildVertices()}
141
                {buildEdges()}
142
                </Fragment>}
153
            {active && (
154
                <Fragment>
155
                    {buildVertices()}
156
                    {buildEdges()}
157
                </Fragment>
158
            )}
143 159
        </Fragment>
144 160
    )
145 161
}
146 162

  
147 163
export default MapPath
164
function setPrimaryIdx(idx: number): any {
165
    throw new Error('Function not implemented.')
166
}

Také k dispozici: Unified diff