-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Save ZTF alerts as sources by objectId #44
Merged
kmshin1397
merged 24 commits into
fritz-marshal:master
from
dmitryduev:save_alert_by_id
Sep 30, 2020
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
f9272f2
use mui-datatable in ZTFAlert.jsx
dmitryduev bb438fa
make ZTFAlert look more Source
dmitryduev 966e331
WIP: save alert as source
dmitryduev 06f5f86
backend for saving alerts as sources
dmitryduev a7468f8
backend for saving alerts as sources; async call K
dmitryduev 74a1e0a
fix linting
dmitryduev e3c6bcc
final touches
dmitryduev 8ffa4cb
hide save button upon success
dmitryduev 8ce2380
fix linting
dmitryduev 54c6015
fix linting
dmitryduev 20ff375
fix linting
dmitryduev b6bde4a
return error if objectId not found on K
dmitryduev 402b96f
fix async-related issues
dmitryduev 8eaa1a6
fix #46
dmitryduev 4d0cf5d
fix #46
dmitryduev eba0ff0
fix merge conflict due to #42
dmitryduev 8b73532
fix merge conflict due to #42
dmitryduev db3988b
update SP
dmitryduev 8ee0bc8
update K
dmitryduev 947934a
check group stream access permissions
dmitryduev f599ebe
remove option to save to all user groups by default
dmitryduev d5827ad
remove zp
dmitryduev 8c401b1
Merge branch 'master' into save_alert_by_id
dmitryduev 1d3fa64
throw error if trying to post to groups unauthorized
dmitryduev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
221 changes: 221 additions & 0 deletions
221
extensions/skyportal/static/js/components/SaveAlertButton.jsx
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,221 @@ | ||
import React, { useState, useEffect, useRef } from "react"; | ||
import PropTypes from "prop-types"; | ||
import { useDispatch } from "react-redux"; | ||
import Dialog from "@material-ui/core/Dialog"; | ||
import DialogContent from "@material-ui/core/DialogContent"; | ||
import DialogTitle from "@material-ui/core/DialogTitle"; | ||
import Checkbox from "@material-ui/core/Checkbox"; | ||
import Button from "@material-ui/core/Button"; | ||
import ButtonGroup from "@material-ui/core/ButtonGroup"; | ||
import FormControlLabel from "@material-ui/core/FormControlLabel"; | ||
import ArrowDropDownIcon from "@material-ui/icons/ArrowDropDown"; | ||
import ClickAwayListener from "@material-ui/core/ClickAwayListener"; | ||
import Grow from "@material-ui/core/Grow"; | ||
import Paper from "@material-ui/core/Paper"; | ||
import Popper from "@material-ui/core/Popper"; | ||
import MenuItem from "@material-ui/core/MenuItem"; | ||
import MenuList from "@material-ui/core/MenuList"; | ||
import { useForm, Controller } from "react-hook-form"; | ||
|
||
import * as alertActions from "../ducks/alert"; | ||
import * as sourceActions from "../ducks/source"; | ||
import FormValidationError from "./FormValidationError"; | ||
|
||
const SaveAlertButton = ({ alert, userGroups }) => { | ||
const [isSubmitting, setIsSubmitting] = useState(false); | ||
// Dialog logic: | ||
|
||
const dispatch = useDispatch(); | ||
const [dialogOpen, setDialogOpen] = useState(false); | ||
|
||
const { handleSubmit, errors, reset, control, getValues } = useForm(); | ||
|
||
useEffect(() => { | ||
reset({ | ||
group_ids: [] | ||
}); | ||
}, [reset, userGroups, alert]); | ||
|
||
const handleClickOpenDialog = () => { | ||
setDialogOpen(true); | ||
}; | ||
|
||
const handleCloseDialog = () => { | ||
setDialogOpen(false); | ||
}; | ||
|
||
const validateGroups = () => { | ||
const formState = getValues({ nest: true }); | ||
return formState.group_ids.filter((value) => Boolean(value)).length >= 1; | ||
}; | ||
|
||
const onSubmitGroupSelectSave = async (data) => { | ||
setIsSubmitting(true); | ||
data.id = alert.id; | ||
const groupIDs = userGroups.map((g) => g.id); | ||
const selectedGroupIDs = groupIDs.filter((ID, idx) => data.group_ids[idx]); | ||
|
||
data.payload = {candid: alert.candid, group_ids: selectedGroupIDs}; | ||
|
||
const result = await dispatch(alertActions.saveAlertAsSource(data)); | ||
if (result.status === "error") { | ||
setIsSubmitting(false); | ||
} else { | ||
setDialogOpen(false); | ||
reset(); | ||
await dispatch(sourceActions.fetchSource(alert.id)); | ||
} | ||
}; | ||
|
||
// Split button logic (largely copied from | ||
// https://material-ui.com/components/button-group/#split-button): | ||
|
||
const options = ["Select groups & save as a source"]; | ||
// const options = ["Select groups & save as a source", "Select filters & save as a candidate"]; | ||
|
||
const [splitButtonMenuOpen, setSplitButtonMenuOpen] = useState(false); | ||
const anchorRef = useRef(null); | ||
const [selectedIndex, setSelectedIndex] = useState(0); | ||
|
||
const handleClickMainButton = async () => { | ||
if (selectedIndex === 0) { | ||
handleClickOpenDialog(); | ||
} | ||
}; | ||
|
||
const handleMenuItemClick = (event, index) => { | ||
setSelectedIndex(index); | ||
setSplitButtonMenuOpen(false); | ||
}; | ||
|
||
const handleToggleSplitButtonMenu = () => { | ||
setSplitButtonMenuOpen((prevOpen) => !prevOpen); | ||
}; | ||
|
||
const handleCloseSplitButtonMenu = (event) => { | ||
if (anchorRef.current && anchorRef.current.contains(event.target)) { | ||
return; | ||
} | ||
setSplitButtonMenuOpen(false); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<ButtonGroup | ||
variant="contained" | ||
ref={anchorRef} | ||
aria-label="split button" | ||
> | ||
<Button | ||
onClick={handleClickMainButton} | ||
name={`initialSaveAlertButton${alert.id}`} | ||
data-testid={`saveAlertButton_${alert.id}`} | ||
disabled={isSubmitting} | ||
> | ||
{options[selectedIndex]} | ||
</Button> | ||
<Button | ||
size="small" | ||
aria-controls={splitButtonMenuOpen ? "split-button-menu" : undefined} | ||
aria-expanded={splitButtonMenuOpen ? "true" : undefined} | ||
aria-label="Save as Source" | ||
aria-haspopup="menu" | ||
name={`saveAlertButtonDropDownArrow${alert.id}`} | ||
onClick={handleToggleSplitButtonMenu} | ||
> | ||
<ArrowDropDownIcon /> | ||
</Button> | ||
</ButtonGroup> | ||
<Popper | ||
open={splitButtonMenuOpen} | ||
anchorEl={anchorRef.current} | ||
role={undefined} | ||
transition | ||
disablePortal | ||
style={{ zIndex: 1000 }} | ||
> | ||
{({ TransitionProps, placement }) => ( | ||
<Grow | ||
/* eslint-disable-next-line react/jsx-props-no-spreading */ | ||
{...TransitionProps} | ||
style={{ | ||
transformOrigin: | ||
placement === "bottom" ? "center top" : "center bottom", | ||
}} | ||
> | ||
<Paper> | ||
<ClickAwayListener onClickAway={handleCloseSplitButtonMenu}> | ||
<MenuList id="split-button-menu"> | ||
{options.map((option, index) => ( | ||
<MenuItem | ||
key={option} | ||
name={`buttonMenuOption${alert.id}_${option}`} | ||
selected={index === selectedIndex} | ||
onClick={(event) => handleMenuItemClick(event, index)} | ||
> | ||
{option} | ||
</MenuItem> | ||
))} | ||
</MenuList> | ||
</ClickAwayListener> | ||
</Paper> | ||
</Grow> | ||
)} | ||
</Popper> | ||
|
||
<Dialog | ||
open={dialogOpen} | ||
onClose={handleCloseDialog} | ||
style={{ position: "fixed" }} | ||
> | ||
<DialogTitle>Select one or more groups:</DialogTitle> | ||
<DialogContent> | ||
<form onSubmit={handleSubmit(onSubmitGroupSelectSave)}> | ||
{errors.group_ids && ( | ||
<FormValidationError message="Select at least one group." /> | ||
)} | ||
{userGroups.map((userGroup, idx) => ( | ||
<FormControlLabel | ||
key={userGroup.id} | ||
control={ | ||
<Controller | ||
as={Checkbox} | ||
name={`group_ids[${idx}]`} | ||
control={control} | ||
rules={{ validate: validateGroups }} | ||
defaultValue={false} | ||
/> | ||
} | ||
label={userGroup.name} | ||
/> | ||
))} | ||
<br /> | ||
<div style={{ textAlign: "center" }}> | ||
<Button | ||
variant="contained" | ||
type="submit" | ||
name={`finalSaveAlertButton${alert.id}`} | ||
> | ||
Save | ||
</Button> | ||
</div> | ||
</form> | ||
</DialogContent> | ||
</Dialog> | ||
</div> | ||
); | ||
}; | ||
SaveAlertButton.propTypes = { | ||
alert: PropTypes.shape({ | ||
id: PropTypes.string, | ||
group_ids: PropTypes.arrayOf(PropTypes.number), | ||
}).isRequired, | ||
userGroups: PropTypes.arrayOf( | ||
PropTypes.shape({ | ||
id: PropTypes.number, | ||
name: PropTypes.string, | ||
}) | ||
).isRequired, | ||
}; | ||
|
||
export default SaveAlertButton; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens when a user tries to save an object with only PID=3 alerts to the sitewide group?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great point, addressed here. It will now fail.