Projekt

Obecné

Profil

Stáhnout (5.03 KB) Statistiky
| Větev: | Tag: | Revize:
1
import {
2
    Avatar,
3
    DialogContent,
4
    DialogTitle,
5
    IconButton,
6
    ListItem,
7
    ListItemText,
8
    Stack,
9
    Typography,
10
} from '@mui/material'
11
import { Fragment, FunctionComponent, useEffect, useState } from 'react'
12
import { useDispatch, useSelector } from 'react-redux'
13
import { RootState } from '../../redux/store'
14
import ButtonOpenableDialog from '../../Reusables/ButtonOpenableDialog'
15
import CloseIcon from '@mui/icons-material/Close'
16
import VisibilityIcon from '@mui/icons-material/Visibility'
17
import DeleteIcon from '@mui/icons-material/Delete'
18
import List from '@mui/material/List'
19
import { ExternalPath } from '../trackingToolUtils'
20
import VisibilityOffIcon from '@mui/icons-material/VisibilityOff'
21
import { removeFilePath, toggleFilePathVisibility } from '../trackingToolSlice'
22
import ConfirmationDialog from '../../Reusables/ConfirmationDialog'
23
import { showNotification } from '../../Notification/notificationSlice'
24
import ClearAllIcon from '@mui/icons-material/ClearAll'
25

    
26
export interface FilePathMenuItemProps {
27
    path: ExternalPath
28
}
29

    
30
const FilePathMenuItem: FunctionComponent<FilePathMenuItemProps> = ({
31
    path,
32
}) => {
33
    const dispatch = useDispatch()
34
    const toggleVisibility = () => {
35
        dispatch(
36
            toggleFilePathVisibility({ idx: path.idx, visible: !path.visible })
37
        )
38
    }
39

    
40
    const deletePath = () => {
41
        dispatch(removeFilePath({ idx: path.idx }))
42
        dispatch(
43
            showNotification({
44
                message: `Removed file path: ${path.path}`,
45
                severity: 'success',
46
                autohideSecs: 5,
47
            })
48
        )
49
    }
50

    
51
    const [deleteConfirmOpen, setDeleteConfirmOpen] = useState(false)
52

    
53
    return (
54
        <ListItem>
55
            <Avatar style={{ backgroundColor: path.color }}> </Avatar>
56
            <ListItemText
57
                primary={
58
                    <Typography sx={{ ml: 2 }} noWrap>
59
                        {path.filename}
60
                    </Typography>
61
                }
62
            />
63
            <Stack justifyContent="space-between" direction="row">
64
                <ConfirmationDialog
65
                    title="Delete File Path"
66
                    secondaryText={`Are you sure you want to delete file path (${path.filename})?`}
67
                    open={deleteConfirmOpen}
68
                    setOpen={setDeleteConfirmOpen}
69
                    onConfirmCallback={deletePath}
70
                    maxWidth="xs"
71
                />
72
                <Stack justifyContent="space-between" direction="row">
73
                    <Stack direction="row">
74
                        <IconButton onClick={toggleVisibility}>
75
                            {path.visible ? (
76
                                <VisibilityIcon />
77
                            ) : (
78
                                <VisibilityOffIcon />
79
                            )}
80
                        </IconButton>
81
                        <IconButton
82
                            onClick={() => {
83
                                setDeleteConfirmOpen(true)
84
                            }}
85
                        >
86
                            <DeleteIcon />
87
                        </IconButton>
88
                    </Stack>
89
                </Stack>
90
            </Stack>
91
        </ListItem>
92
    )
93
}
94

    
95
/**
96
 * Component for managing file paths from imported files
97
 */
98
const ManageFilePaths = () => {
99
    const filePaths = useSelector(
100
        (state: RootState) => state.trackingTool.importedPaths
101
    )
102
    const [open, setOpen] = useState(false)
103
    useEffect(() => {
104
        if (filePaths && filePaths.length === 0) {
105
            setOpen(false)
106
        }
107
    }, [filePaths])
108

    
109
    return (
110
        <Fragment>
111
            {filePaths && filePaths.length > 0 && (
112
                <ButtonOpenableDialog
113
                    open={open}
114
                    setOpen={setOpen}
115
                    buttonColor="primary"
116
                    buttonText="Manage Imported Paths"
117
                    buttonVariant="contained"
118
                    maxWidth="xs"
119
                    startIcon={<ClearAllIcon />}
120
                >
121
                    <DialogTitle>
122
                        <Stack
123
                            direction="row"
124
                            justifyContent="space-between"
125
                            alignItems="center"
126
                        >
127
                            <Typography variant="h6" fontWeight="bold">
128
                                Manage File Paths
129
                            </Typography>
130
                            <IconButton onClick={() => setOpen(false)}>
131
                                <CloseIcon />
132
                            </IconButton>
133
                        </Stack>
134
                    </DialogTitle>
135
                    <DialogContent>
136
                        <List>
137
                            {filePaths.map((item, idx) => (
138
                                <FilePathMenuItem key={idx} path={item} />
139
                            ))}
140
                        </List>
141
                    </DialogContent>
142
                </ButtonOpenableDialog>
143
            )}
144
        </Fragment>
145
    )
146
}
147

    
148
export default ManageFilePaths
(1-1/6)