Projekt

Obecné

Profil

Stáhnout (5.51 KB) Statistiky
| Větev: | Tag: | Revize:
1
import {
2
    IconButton,
3
    ListItem,
4
    ListItemAvatar,
5
    ListItemText,
6
    Typography,
7
} from '@mui/material'
8
import { Draggable } from 'react-beautiful-dnd'
9
import { getMapPointSemanticColor as getMapPointSemanticColor, MapPoint, MapPointType, PathVariant } from '../trackingToolUtils'
10
import { CatalogItemDto } from '../../../swagger/data-contracts'
11
import { useDispatch } from 'react-redux'
12
import { removeMapMarker, updateMapMarker } from '../trackingToolSlice'
13
import { useMemo } from 'react'
14
import DragHandleIcon from '@mui/icons-material/DragHandle'
15
import VisibilityIcon from '@mui/icons-material/Visibility'
16
import VisibilityOffIcon from '@mui/icons-material/VisibilityOff'
17
import AddRoadIcon from '@mui/icons-material/AddRoad'
18
import RemoveRoadIcon from '@mui/icons-material/RemoveRoad'
19
import DeleteForeverIcon from '@mui/icons-material/DeleteForever'
20

    
21
export type DraggableListItemProps = {
22
    list: PathVariant
23
    idx: number
24
}
25

    
26
const getFormattedLocationOrEmpty = (catalogItem: CatalogItemDto) => {
27
    if (!catalogItem || !catalogItem.latitude || !catalogItem.longitude) {
28
        return 'Location unavailable'
29
    }
30

    
31
    return `${catalogItem.latitude.toFixed(
32
        3
33
    )}°, ${catalogItem.longitude.toFixed(3)}°`
34
}
35

    
36
const MapPointDraggableListItem = ({ list, idx }: DraggableListItemProps) => {
37
    const item = list[idx]
38
    const dispatch = useDispatch()
39

    
40
    // useMemo to prevent unnecessary re-renders which will make the list jumpy
41
    return useMemo(() => {
42
        const toggleAddToPath = () => {
43
            dispatch(
44
                // updateMapMarkerWithId({
45
                //     item: {
46
                //         ...item,
47
                //         addToPath: !item?.addToPath,
48
                //     } as MapPoint,
49
                //     id: item.id,
50
                // })
51
                updateMapMarker({
52
                    ...item,
53
                    addToPath: !item?.addToPath,
54
                    idx,
55
                })
56
            )
57
        }
58

    
59
        const toggleHidden = () => {
60
            dispatch(
61
                // updateMapMarkerWithId({
62
                //     item: {
63
                //         ...item,
64
                //         hidden: !item?.hidden,
65
                //     } as MapPoint,
66
                //     id: item.id,
67
                // })
68
                updateMapMarker({
69
                    ...item,
70
                    hidden: !item?.hidden,
71
                    idx,
72
                })
73
            )
74
        }
75

    
76
        const deleteItem = () => {
77
            dispatch(
78
                removeMapMarker({
79
                    ...item,
80
                    idx,
81
                })
82
            )
83
        }
84

    
85
        return (
86
            item && (
87
                <Draggable
88
                    key={`${item.id}`}
89
                    draggableId={`${item.id}`}
90
                    index={idx}
91
                >
92
                    {(provided, snapshot) => (
93
                        <ListItem
94
                            ref={provided.innerRef}
95
                            {...provided.draggableProps}
96
                            {...provided.dragHandleProps}
97
                        >
98
                            <ListItemAvatar>
99
                                <DragHandleIcon />
100
                            </ListItemAvatar>
101
                            <ListItemText
102
                                primary={
103
                                    <Typography
104
                                        style={{ color: getMapPointSemanticColor(item) }}
105
                                    >
106
                                        {item.catalogItem.name ??
107
                                            'Unknown name'}
108
                                    </Typography>
109
                                }
110
                                secondary={getFormattedLocationOrEmpty(
111
                                    item.catalogItem
112
                                )}
113
                            />
114
                            {item.type !== MapPointType.LocalCatalog && (
115
                                <IconButton sx={{ mr: 1 }} onClick={deleteItem}>
116
                                    <DeleteForeverIcon />
117
                                </IconButton>
118
                            )}
119
                            <IconButton sx={{ mr: 1 }} onClick={toggleHidden}>
120
                                {item.hidden ? (
121
                                    <VisibilityOffIcon />
122
                                ) : (
123
                                    <VisibilityIcon />
124
                                )}
125
                            </IconButton>
126
                            <IconButton
127
                                sx={{ mr: 1 }}
128
                                onClick={toggleAddToPath}
129
                            >
130
                                {item.addToPath ? (
131
                                    <AddRoadIcon />
132
                                ) : (
133
                                    <RemoveRoadIcon />
134
                                )}
135
                            </IconButton>
136
                            {/* <FormControlLabel
137
                            control={
138
                                <Checkbox
139
                                    checked={item.addToPath}
140
                                    onChange={toggleAddToPath}
141
                                />
142
                            }
143
                            label="Add to path"
144
                        /> */}
145
                        </ListItem>
146
                    )}
147
                </Draggable>
148
            )
149
        )
150
    }, [item, idx, dispatch])
151
}
152

    
153
export default MapPointDraggableListItem
(3-3/3)