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