Projekt

Obecné

Profil

Stáhnout (2.37 KB) Statistiky
| Větev: | Tag: | Revize:
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 ? 'rgb(235,235,235)' : 'inherit',
54
                    }}
55
                >
56
                    <ListItemAvatar>
57
                        <Avatar>
58
                            <LocationOnIcon />
59
                        </Avatar>
60
                    </ListItemAvatar>
61
                    <ListItemText
62
                        primary={item.catalogItem.name ?? 'Unknown name'}
63
                        secondary={getFormattedLocationOrEmpty(
64
                            item.catalogItem
65
                        )}
66
                    />
67
                    <Checkbox checked={item.active} onChange={toggleHidden} />
68
                </ListItem>
69
            )}
70
        </Draggable>
71
    )
72
}
73

    
74
export default DraggableListItem
(2-2/3)