diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2020-09-20 22:39:04 -0700 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2020-09-20 22:39:04 -0700 |
commit | 37dd8129f8e07d00eb93262210bf1ecd4ce95532 (patch) | |
tree | 80f115342fb4624675856a9164970b81f1f5b263 /modern/src/EditCollectionView.js | |
parent | 787c9dc0ec684d3524ec060b6422ffbaea5012ac (diff) | |
download | trackermap-web-37dd8129f8e07d00eb93262210bf1ecd4ce95532.tar.gz trackermap-web-37dd8129f8e07d00eb93262210bf1ecd4ce95532.tar.bz2 trackermap-web-37dd8129f8e07d00eb93262210bf1ecd4ce95532.zip |
Refactor collection editing
Diffstat (limited to 'modern/src/EditCollectionView.js')
-rw-r--r-- | modern/src/EditCollectionView.js | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/modern/src/EditCollectionView.js b/modern/src/EditCollectionView.js new file mode 100644 index 00000000..59ea2db0 --- /dev/null +++ b/modern/src/EditCollectionView.js @@ -0,0 +1,73 @@ +import React, { useState } from 'react'; +import { makeStyles } from '@material-ui/core/styles'; +import { useHistory } from 'react-router-dom'; +import Menu from '@material-ui/core/Menu'; +import MenuItem from '@material-ui/core/MenuItem'; +import Fab from '@material-ui/core/Fab'; +import AddIcon from '@material-ui/icons/Add'; + +import t from './common/localization'; +import RemoveDialog from './RemoveDialog'; + +const useStyles = makeStyles(theme => ({ + fab: { + position: 'absolute', + bottom: theme.spacing(2), + right: theme.spacing(2), + }, +})); + +const EditCollectionView = ({ content, editPath, endpoint }) => { + const classes = useStyles(); + const history = useHistory(); + + const [selectedId, setSelectedId] = useState(null); + const [selectedAnchorEl, setSelectedAnchorEl] = useState(null); + const [removeDialogShown, setRemoveDialogShown] = useState(false); + + const menuShow = (anchorId, itemId) => { + setSelectedAnchorEl(anchorId); + setSelectedId(itemId); + } + + const menuHide = () => { + setSelectedAnchorEl(null); + } + + const handleAdd = () => { + history.push(editPath); + menuHide(); + } + + const handleEdit = () => { + history.push(`${editPath}/${selectedId}`); + menuHide(); + } + + const handleRemove = () => { + setRemoveDialogShown(true); + menuHide(); + } + + const handleRemoveResult = () => { + setRemoveDialogShown(false); + } + + const Content = content; + + return ( + <> + <Content onMenuClick={menuShow} /> + <Fab size="medium" color="primary" className={classes.fab} onClick={handleAdd}> + <AddIcon /> + </Fab> + <Menu open={!!selectedAnchorEl} anchorEl={selectedAnchorEl} onClose={menuHide}> + <MenuItem onClick={handleEdit}>{t('sharedEdit')}</MenuItem> + <MenuItem onClick={handleRemove}>{t('sharedRemove')}</MenuItem> + </Menu> + <RemoveDialog open={removeDialogShown} endpoint={endpoint} itemId={selectedId} onResult={handleRemoveResult} /> + </> + ); +} + +export default EditCollectionView; |