-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #499 from CSCfi/feature/delete-draft-templates
Enable deletion of draft templates
- Loading branch information
Showing
5 changed files
with
155 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
//@flow | ||
import React, { useState } from "react" | ||
|
||
import IconButton from "@material-ui/core/IconButton" | ||
import Menu from "@material-ui/core/Menu" | ||
import MenuItem from "@material-ui/core/MenuItem" | ||
import MoreVertIcon from "@material-ui/icons/MoreVert" | ||
import { useDispatch } from "react-redux" | ||
|
||
import { ResponseStatus } from "constants/responseStatus" | ||
import { updateStatus } from "features/statusMessageSlice" | ||
import { deleteTemplateByAccessionId } from "features/userSlice" | ||
import templateAPI from "services/templateAPI" | ||
|
||
const UserDraftTemplateActions = (props: { item: { schema: string, accessionId: string } }): React$Element<any> => { | ||
const { item } = props | ||
const dispatch = useDispatch() | ||
const [anchorEl, setAnchorEl] = useState(null) | ||
const menuOpen = Boolean(anchorEl) | ||
|
||
const handleMenuClick = event => { | ||
setAnchorEl(event.currentTarget) | ||
} | ||
|
||
const handleCloseMenu = () => { | ||
setAnchorEl(null) | ||
} | ||
|
||
const deleteTemplate = async (schema, accessionId) => { | ||
const objectType = schema.split("template-")[1] | ||
|
||
const response = await templateAPI.deleteTemplateByAccessionId(objectType, accessionId) | ||
|
||
if (response.ok) { | ||
dispatch(deleteTemplateByAccessionId(accessionId)) | ||
} else { | ||
dispatch( | ||
updateStatus({ | ||
status: ResponseStatus.error, | ||
response: response, | ||
errorPrefix: "Unable to delete template", | ||
}) | ||
) | ||
} | ||
} | ||
|
||
return ( | ||
<React.Fragment> | ||
<IconButton | ||
aria-label="more" | ||
id={`template-more-button-${item.accessionId}`} | ||
aria-controls="template-menu" | ||
aria-expanded={menuOpen ? "true" : undefined} | ||
aria-haspopup="true" | ||
onClick={handleMenuClick} | ||
> | ||
<MoreVertIcon /> | ||
</IconButton> | ||
|
||
<Menu | ||
id="template-menu" | ||
MenuListProps={{ | ||
"aria-labelledby": "template-more-button", | ||
}} | ||
anchorEl={anchorEl} | ||
open={menuOpen} | ||
onClose={handleCloseMenu} | ||
> | ||
<MenuItem onClick={() => deleteTemplate(item.schema, item.accessionId)} data-testid="delete-template"> | ||
Delete | ||
</MenuItem> | ||
</Menu> | ||
</React.Fragment> | ||
) | ||
} | ||
|
||
export default UserDraftTemplateActions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters