1
|
import {
|
2
|
Avatar,
|
3
|
Checkbox,
|
4
|
ListItem,
|
5
|
ListItemAvatar,
|
6
|
ListItemText,
|
7
|
} from '@mui/material'
|
8
|
import { Draggable } from 'react-beautiful-dnd'
|
9
|
import { MapPoint, PathVariant } from '../Map/pathUtils'
|
10
|
import LocationOnIcon from '@mui/icons-material/LocationOn'
|
11
|
import { CatalogItemDto } from '../../../swagger/data-contracts'
|
12
|
import { useDispatch } from 'react-redux'
|
13
|
import { updateMapMarker, updateMapMarkerWithId } from '../trackingToolSlice'
|
14
|
import { useMemo } from 'react'
|
15
|
|
16
|
export type DraggableListItemProps = {
|
17
|
list: PathVariant
|
18
|
idx: number
|
19
|
}
|
20
|
|
21
|
const getFormattedLocationOrEmpty = (catalogItem: CatalogItemDto) => {
|
22
|
if (!catalogItem || !catalogItem.latitude || !catalogItem.longitude) {
|
23
|
return 'Location Unknown'
|
24
|
}
|
25
|
|
26
|
return `${catalogItem.latitude}°, ${catalogItem.longitude}°`
|
27
|
}
|
28
|
|
29
|
const DraggableListItem = ({ list, idx }: DraggableListItemProps) => {
|
30
|
const item = list[idx]
|
31
|
const dispatch = useDispatch()
|
32
|
|
33
|
// useMemo to prevent unnecessary re-renders which will make the list jumpy
|
34
|
return useMemo(() => {
|
35
|
const toggleHidden = () => {
|
36
|
dispatch(
|
37
|
updateMapMarkerWithId({
|
38
|
item: {
|
39
|
...item,
|
40
|
active: !item?.active,
|
41
|
} as MapPoint,
|
42
|
id: item.id,
|
43
|
})
|
44
|
)
|
45
|
}
|
46
|
|
47
|
return (
|
48
|
<Draggable
|
49
|
key={`${item.id}`}
|
50
|
draggableId={`${item.id}`}
|
51
|
index={idx}
|
52
|
>
|
53
|
{(provided, snapshot) => (
|
54
|
<ListItem
|
55
|
ref={provided.innerRef}
|
56
|
{...provided.draggableProps}
|
57
|
{...provided.dragHandleProps}
|
58
|
sx={{
|
59
|
background: snapshot.isDragging
|
60
|
? 'rgb(235,235,235)'
|
61
|
: 'inherit',
|
62
|
}}
|
63
|
>
|
64
|
<ListItemAvatar>
|
65
|
<Avatar>
|
66
|
<LocationOnIcon />
|
67
|
</Avatar>
|
68
|
</ListItemAvatar>
|
69
|
<ListItemText
|
70
|
primary={item.catalogItem.name ?? 'Unknown name'}
|
71
|
secondary={getFormattedLocationOrEmpty(
|
72
|
item.catalogItem
|
73
|
)}
|
74
|
/>
|
75
|
<Checkbox
|
76
|
checked={item.active}
|
77
|
onChange={toggleHidden}
|
78
|
/>
|
79
|
</ListItem>
|
80
|
)}
|
81
|
</Draggable>
|
82
|
)
|
83
|
}, [item, idx, dispatch])
|
84
|
}
|
85
|
|
86
|
export default DraggableListItem
|