Projekt

Obecné

Profil

Stáhnout (5.66 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 {
10
    getMapPointSemanticColor,
11
    MapPointType,
12
    PathVariant,
13
} from '../trackingToolUtils'
14
import { CatalogItemDto } from '../../../swagger/data-contracts'
15
import { useDispatch } from 'react-redux'
16
import { removeMapMarker, updateMapMarker } from '../trackingToolSlice'
17
import { useMemo } from 'react'
18
import DragHandleIcon from '@mui/icons-material/DragHandle'
19
import VisibilityIcon from '@mui/icons-material/Visibility'
20
import VisibilityOffIcon from '@mui/icons-material/VisibilityOff'
21
import AddRoadIcon from '@mui/icons-material/AddRoad'
22
import RemoveRoadIcon from '@mui/icons-material/RemoveRoad'
23
import DeleteForeverIcon from '@mui/icons-material/DeleteForever'
24

    
25
export type DraggableListItemProps = {
26
    list: PathVariant
27
    idx: number
28
}
29

    
30
const getFormattedLocationOrEmpty = (catalogItem: CatalogItemDto) => {
31
    if (!catalogItem || !catalogItem.latitude || !catalogItem.longitude) {
32
        return 'Location unavailable'
33
    }
34

    
35
    return `${catalogItem.latitude.toFixed(
36
        3
37
    )}°, ${catalogItem.longitude.toFixed(3)}°`
38
}
39

    
40
const MapPointDraggableListItem = ({ list, idx }: DraggableListItemProps) => {
41
    const item = list[idx]
42
    const dispatch = useDispatch()
43

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

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

    
80
        const deleteItem = () => {
81
            dispatch(
82
                removeMapMarker({
83
                    ...item,
84
                    idx,
85
                })
86
            )
87
        }
88

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

    
161
export default MapPointDraggableListItem
(3-3/3)