Projekt

Obecné

Profil

Stáhnout (3.95 KB) Statistiky
| Větev: | Tag: | Revize:
1
import {
2
    Checkbox,
3
    FormControlLabel,
4
    IconButton,
5
    ListItem,
6
    ListItemAvatar,
7
    ListItemText,
8
} from '@mui/material'
9
import { Draggable } from 'react-beautiful-dnd'
10
import { MapPoint, PathVariant } from '../Map/pathUtils'
11
import { CatalogItemDto } from '../../../swagger/data-contracts'
12
import { useDispatch } from 'react-redux'
13
import { updateMapMarkerWithId } from '../trackingToolSlice'
14
import { useMemo } from 'react'
15
import DragHandleIcon from '@mui/icons-material/DragHandle'
16
import VisibilityIcon from '@mui/icons-material/Visibility'
17
import VisibilityOffIcon from '@mui/icons-material/VisibilityOff'
18
import AddRoadIcon from '@mui/icons-material/AddRoad'
19
import RemoveRoadIcon from '@mui/icons-material/RemoveRoad'
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 DraggableListItem = ({ 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
            )
52
        }
53

    
54
        const toggleHidden = () => {
55
            dispatch(
56
                updateMapMarkerWithId({
57
                    item: {
58
                        ...item,
59
                        hidden: !item?.hidden,
60
                    } as MapPoint,
61
                    id: item.id,
62
                })
63
            )
64
        }
65

    
66
        return (
67
            <Draggable
68
                key={`${item.id}`}
69
                draggableId={`${item.id}`}
70
                index={idx}
71
            >
72
                {(provided, snapshot) => (
73
                    <ListItem
74
                        ref={provided.innerRef}
75
                        {...provided.draggableProps}
76
                        {...provided.dragHandleProps}
77
                    >
78
                        <ListItemAvatar>
79
                            <DragHandleIcon />
80
                        </ListItemAvatar>
81
                        <ListItemText
82
                            primary={item.catalogItem.name ?? 'Unknown name'}
83
                            secondary={getFormattedLocationOrEmpty(
84
                                item.catalogItem
85
                            )}
86
                        />
87
                        <IconButton sx={{ mr: 1 }} onClick={toggleHidden}>
88
                            {item.hidden ? (
89
                                <VisibilityOffIcon />
90
                            ) : (
91
                                <VisibilityIcon />
92
                            )}
93
                        </IconButton>
94
                        <IconButton sx={{ mr: 1 }} onClick={toggleAddToPath}>
95
                            {item.addToPath ? (
96
                                <AddRoadIcon />
97
                            ) : (
98
                                <RemoveRoadIcon />
99
                            )}
100
                        </IconButton>
101
                        {/* <FormControlLabel
102
                            control={
103
                                <Checkbox
104
                                    checked={item.addToPath}
105
                                    onChange={toggleAddToPath}
106
                                />
107
                            }
108
                            label="Add to path"
109
                        /> */}
110
                    </ListItem>
111
                )}
112
            </Draggable>
113
        )
114
    }, [item, idx, dispatch])
115
}
116

    
117
export default DraggableListItem
(2-2/3)