-
Notifications
You must be signed in to change notification settings - Fork 1
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
Add logic for creating new annotations #406
Open
aswallace
wants to merge
13
commits into
feature/metadata-editing/develop
Choose a base branch
from
feature/metadata-editing/new-annotation-pathway
base: feature/metadata-editing/develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e1700ce
remove commented code
aswallace b58858e
add annotation post endpoint
aswallace f2d49cc
address linting errors
aswallace f31c6d7
Merge branch 'feature/metadata-editing/develop' into feature/metadata…
aswallace 0d56f49
add temporary noop for db annotation creation
aswallace b9ea8f1
restrict field name character types
aswallace ec60d0e
add db new annotation logic
aswallace 867e0bd
store new annotation data
aswallace 73a8316
address PR comments
aswallace b3264d1
refactor create annotation responses
aswallace 3480081
rename unsaved changes prop
aswallace ceede0e
limit annotation type
aswallace 7cab70b
fix broken unit tests
aswallace 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
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
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 |
---|---|---|
@@ -1,12 +1,25 @@ | ||
import { IComboBoxOption, IconButton, Stack, StackItem, TextField } from "@fluentui/react"; | ||
import { | ||
IComboBoxOption, | ||
IconButton, | ||
Spinner, | ||
SpinnerSize, | ||
Stack, | ||
StackItem, | ||
TextField, | ||
} from "@fluentui/react"; | ||
import classNames from "classnames"; | ||
import * as React from "react"; | ||
import { useDispatch, useSelector } from "react-redux"; | ||
|
||
import MetadataDetails, { ValueCountItem } from "./MetadataDetails"; | ||
import { PrimaryButton, SecondaryButton } from "../Buttons"; | ||
import ComboBox from "../ComboBox"; | ||
import Tooltip from "../Tooltip"; | ||
import Annotation from "../../entity/Annotation"; | ||
import { AnnotationType } from "../../entity/AnnotationFormatter"; | ||
import { AnnotationValue } from "../../services/AnnotationService"; | ||
import { interaction, metadata } from "../../state"; | ||
import { ProcessStatus } from "../../state/interaction/actions"; | ||
|
||
import styles from "./EditMetadata.module.css"; | ||
|
||
|
@@ -18,21 +31,96 @@ enum EditStep { | |
|
||
interface NewAnnotationProps { | ||
onDismiss: () => void; | ||
onUnsavedChanges: () => void; | ||
setHasUnsavedChanges: (arg: boolean) => void; | ||
selectedFileCount: number; | ||
} | ||
|
||
// Simplified version of status message | ||
interface AnnotationStatus { | ||
status: ProcessStatus; | ||
message: string | undefined; | ||
} | ||
|
||
/** | ||
* Component for submitting a new annotation | ||
* and then entering values for the selected files | ||
*/ | ||
export default function NewAnnotationPathway(props: NewAnnotationProps) { | ||
const dispatch = useDispatch(); | ||
// Destructure to prevent unnecessary useEffect triggers | ||
const { onDismiss, setHasUnsavedChanges } = props; | ||
|
||
const [step, setStep] = React.useState<EditStep>(EditStep.CREATE_FIELD); | ||
const [newValues, setNewValues] = React.useState<string | undefined>(); | ||
const [newValues, setNewValues] = React.useState<AnnotationValue | undefined>(); | ||
const [newFieldName, setNewFieldName] = React.useState<string>(""); | ||
const [newFieldDataType, setNewFieldDataType] = React.useState<AnnotationType | undefined>(); | ||
const [newFieldDescription, setNewFieldDescription] = React.useState<string>(""); | ||
const [newFieldDataType, setNewFieldDataType] = React.useState<AnnotationType>( | ||
AnnotationType.STRING | ||
); | ||
const [newDropdownOption, setNewDropdownOption] = React.useState<string>(""); | ||
const [dropdownOptions, setDropdownOptions] = React.useState<IComboBoxOption[]>([]); | ||
const [submissionStatus, setSubmissionStatus] = React.useState<AnnotationStatus | undefined>(); | ||
|
||
const statuses = useSelector(interaction.selectors.getProcessStatuses); | ||
const annotationCreationStatus = React.useMemo( | ||
() => statuses.find((status) => status.processId === newFieldName), | ||
[newFieldName, statuses] | ||
); | ||
// Check for updates to the annotation submission status | ||
React.useEffect(() => { | ||
const checkForStatusUpdates = async () => { | ||
const currentStatus = annotationCreationStatus?.data?.status; | ||
switch (currentStatus) { | ||
case ProcessStatus.ERROR: | ||
case ProcessStatus.STARTED: | ||
case ProcessStatus.PROGRESS: | ||
setSubmissionStatus({ | ||
status: currentStatus, | ||
message: annotationCreationStatus?.data?.msg, | ||
}); | ||
return; | ||
case ProcessStatus.SUCCEEDED: | ||
if (newFieldName && newValues) { | ||
try { | ||
dispatch( | ||
interaction.actions.editFiles({ [newFieldName]: [newValues] }) | ||
); | ||
} catch (e) { | ||
setSubmissionStatus({ | ||
status: ProcessStatus.ERROR, | ||
message: `Failed to create annotation: ${e}`, | ||
}); | ||
} finally { | ||
setHasUnsavedChanges(false); | ||
onDismiss(); | ||
} | ||
} | ||
default: | ||
return; | ||
} | ||
}; | ||
checkForStatusUpdates(); | ||
}, [ | ||
annotationCreationStatus, | ||
dispatch, | ||
setHasUnsavedChanges, | ||
newFieldName, | ||
newValues, | ||
onDismiss, | ||
]); | ||
|
||
const onChangeAlphanumericField = ( | ||
e: React.FormEvent<HTMLInputElement | HTMLTextAreaElement>, | ||
newValue: string | undefined | ||
) => { | ||
const regex = /^[\w\-\s]+$/g; | ||
// Restricts character entry to alphanumeric | ||
if (newValue && !regex.test(newValue)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. aww, no injection attacks? But what if scientists want to mark an image ";Drop tables;":True? |
||
e.preventDefault(); | ||
} else { | ||
setNewFieldName(newValue || ""); | ||
} | ||
}; | ||
|
||
const addDropdownChip = (evt: React.FormEvent) => { | ||
evt.preventDefault(); | ||
|
@@ -53,30 +141,56 @@ export default function NewAnnotationPathway(props: NewAnnotationProps) { | |
setDropdownOptions(dropdownOptions.filter((opt) => opt !== optionToRemove)); | ||
}; | ||
|
||
function onSubmit() { | ||
// TO DO: endpoint logic is in progress on a different branch | ||
props.onDismiss(); | ||
} | ||
|
||
// TO DO: determine when to submit the new annotation post req | ||
function onCreateNewAnnotation() { | ||
props?.onUnsavedChanges(); | ||
setHasUnsavedChanges(true); | ||
setStep(EditStep.EDIT_FILES); | ||
} | ||
|
||
function onSubmit() { | ||
if (!newFieldName || !newValues) { | ||
setSubmissionStatus({ | ||
status: ProcessStatus.ERROR, | ||
message: `Missing ${!newFieldName ? "field name" : "values for file"}`, | ||
}); | ||
return; | ||
} | ||
const annotation = new Annotation({ | ||
annotationDisplayName: newFieldName, | ||
annotationName: newFieldName, | ||
description: newFieldDescription, | ||
type: newFieldDataType, | ||
}); | ||
// File editing step occurs after dispatch is processed and status is updated | ||
dispatch( | ||
metadata.actions.createAnnotation( | ||
annotation, | ||
dropdownOptions.map((opt) => opt.text) | ||
) | ||
); | ||
} | ||
|
||
return ( | ||
<> | ||
{/* TO DO: Prevent user from entering a name that collides with existing annotation */} | ||
<TextField | ||
required | ||
label="New metadata field name" | ||
className={styles.textField} | ||
onChange={(_, newValue) => setNewFieldName(newValue || "")} | ||
onChange={(ev, newValue) => onChangeAlphanumericField(ev, newValue)} | ||
placeholder="Add a new field name..." | ||
value={newFieldName} | ||
/> | ||
{step === EditStep.CREATE_FIELD && ( | ||
<> | ||
<TextField | ||
multiline | ||
rows={2} | ||
label="Description" | ||
className={styles.textField} | ||
onChange={(_, newValue) => setNewFieldDescription(newValue || "")} | ||
placeholder="Add a short description of the new field..." | ||
value={newFieldDescription} | ||
/> | ||
<ComboBox | ||
className={styles.comboBox} | ||
selectedKey={newFieldDataType || undefined} | ||
|
@@ -91,7 +205,9 @@ export default function NewAnnotationPathway(props: NewAnnotationProps) { | |
}; | ||
})} | ||
onChange={(option) => | ||
setNewFieldDataType((option?.key as AnnotationType) || "") | ||
setNewFieldDataType( | ||
(option?.key as AnnotationType) || AnnotationType.STRING | ||
) | ||
} | ||
/> | ||
{newFieldDataType === AnnotationType.DROPDOWN && ( | ||
|
@@ -132,17 +248,38 @@ export default function NewAnnotationPathway(props: NewAnnotationProps) { | |
</> | ||
)} | ||
{step === EditStep.EDIT_FILES && ( | ||
<MetadataDetails | ||
fieldType={newFieldDataType} | ||
items={[ | ||
{ | ||
value: undefined, | ||
fileCount: props.selectedFileCount, | ||
} as ValueCountItem, | ||
]} | ||
dropdownOptions={dropdownOptions} | ||
onChange={(value) => setNewValues(value)} | ||
/> | ||
<> | ||
{newFieldDescription.trim() && ( | ||
<> | ||
<b>Description: </b> {newFieldDescription} | ||
</> | ||
)} | ||
<MetadataDetails | ||
fieldType={newFieldDataType} | ||
items={[ | ||
{ | ||
value: undefined, | ||
fileCount: props.selectedFileCount, | ||
} as ValueCountItem, | ||
]} | ||
dropdownOptions={dropdownOptions} | ||
onChange={(value) => setNewValues(value)} | ||
/> | ||
{submissionStatus && ( | ||
<div | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. very nice feedback here |
||
className={classNames( | ||
submissionStatus.status === ProcessStatus.ERROR | ||
? styles.errorMessage | ||
: styles.statusMessage | ||
)} | ||
> | ||
{submissionStatus.status === ProcessStatus.STARTED && ( | ||
<Spinner className={styles.spinner} size={SpinnerSize.small} /> | ||
)} | ||
{submissionStatus?.message} | ||
</div> | ||
)} | ||
</> | ||
)} | ||
<div className={styles.footer}> | ||
<Stack horizontal> | ||
|
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
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.
We aren't able to directly chain off of the createAnnotation dispatch (we don't use thunk in this codebase) so instead we need to wait for the status to update in state and then respond accordingly when we see
SUCCEEDED