Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 51 additions & 27 deletions superset-frontend/src/views/CRUD/annotation/AnnotationModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,24 @@ const AnnotationModal: FunctionComponent<AnnotationModalProps> = ({
addDangerToast,
);

const resetAnnotation = () => {
// Reset annotation
setCurrentAnnotation({
short_descr: '',
start_dttm: '',
end_dttm: '',
json_metadata: '',
long_descr: '',
});
};

// Functions
const hide = () => {
setIsHidden(true);

// Reset annotation
resetAnnotation();

onHide();
};

Expand All @@ -127,7 +142,12 @@ const AnnotationModal: FunctionComponent<AnnotationModalProps> = ({
delete currentAnnotation.changed_by;
delete currentAnnotation.changed_on_delta_humanized;
delete currentAnnotation.layer;
updateResource(update_id, currentAnnotation).then(() => {
updateResource(update_id, currentAnnotation).then(response => {
// No response on error
if (!response) {
return;
}

if (onAnnotationAdd) {
onAnnotationAdd();
}
Expand All @@ -137,7 +157,11 @@ const AnnotationModal: FunctionComponent<AnnotationModalProps> = ({
}
} else if (currentAnnotation) {
// Create
createResource(currentAnnotation).then(() => {
createResource(currentAnnotation).then(response => {
if (!response) {
return;
}

if (onAnnotationAdd) {
onAnnotationAdd();
}
Expand Down Expand Up @@ -206,32 +230,32 @@ const AnnotationModal: FunctionComponent<AnnotationModalProps> = ({
};

// Initialize
if (
isEditMode &&
(!currentAnnotation ||
!currentAnnotation.id ||
(annotation && annotation.id !== currentAnnotation.id) ||
(isHidden && show))
) {
if (annotation && annotation.id !== null && !loading) {
const id = annotation.id || 0;

fetchResource(id).then(() => {
setCurrentAnnotation(resource);
});
useEffect(() => {
if (
isEditMode &&
(!currentAnnotation ||
!currentAnnotation.id ||
(annotation && annotation.id !== currentAnnotation.id) ||
(isHidden && show))
) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can split these if conditions into multiple early returns. Currently it's kind of hard to reason with.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can also use optional chaining to help if you want.

if (annotation && annotation.id !== null && !loading) {
const id = annotation.id || 0;

fetchResource(id);
}
} else if (
!isEditMode &&
(!currentAnnotation || currentAnnotation.id || (isHidden && show))
) {
resetAnnotation();
}
} else if (
!isEditMode &&
(!currentAnnotation || currentAnnotation.id || (isHidden && show))
) {
setCurrentAnnotation({
short_descr: '',
start_dttm: '',
end_dttm: '',
json_metadata: '',
long_descr: '',
});
}
}, [annotation]);

useEffect(() => {
if (resource) {
setCurrentAnnotation(resource);
}
}, [resource]);

// Validation
useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,21 @@ const AnnotationLayerModal: FunctionComponent<AnnotationLayerModalProps> = ({
addDangerToast,
);

const resetLayer = () => {
// Reset layer
setCurrentLayer({
name: '',
descr: '',
});
};

// Functions
const hide = () => {
setIsHidden(true);

// Reset layer
resetLayer();

onHide();
};

Expand All @@ -121,13 +133,21 @@ const AnnotationLayerModal: FunctionComponent<AnnotationLayerModalProps> = ({
const update_id = currentLayer.id;
delete currentLayer.id;
delete currentLayer.created_by;
updateResource(update_id, currentLayer).then(() => {
updateResource(update_id, currentLayer).then(response => {
if (!response) {
return;
}

hide();
});
}
} else if (currentLayer) {
// Create
createResource(currentLayer).then(response => {
if (!response) {
return;
}

if (onLayerAdd) {
onLayerAdd(response);
}
Expand Down Expand Up @@ -162,29 +182,33 @@ const AnnotationLayerModal: FunctionComponent<AnnotationLayerModalProps> = ({
};

// Initialize
if (
isEditMode &&
(!currentLayer ||
!currentLayer.id ||
(layer && layer.id !== currentLayer.id) ||
(isHidden && show))
) {
if (layer && layer.id !== null && !loading) {
const id = layer.id || 0;

fetchResource(id).then(() => {
setCurrentLayer(resource);
});
useEffect(() => {
if (
isEditMode &&
(!currentLayer ||
!currentLayer.id ||
(layer && layer.id !== currentLayer.id) ||
(isHidden && show))
) {
if (show && layer && layer.id !== null && !loading) {
const id = layer.id || 0;

fetchResource(id);
}
} else if (
!isEditMode &&
(!currentLayer || currentLayer.id || (isHidden && show))
) {
// Reset layer
resetLayer();
}
} else if (
!isEditMode &&
(!currentLayer || currentLayer.id || (isHidden && show))
) {
setCurrentLayer({
name: '',
descr: '',
});
}
}, [layer, show]);

useEffect(() => {
if (resource) {
setCurrentLayer(resource);
}
}, [resource]);

// Validation
useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,14 +333,19 @@ function AnnotationLayersList({
window.location.href = `/annotationmodelview/${id}/annotation`;
};

const onModalHide = () => {
refreshData();
setAnnotationLayerModalOpen(false);
};

return (
<>
<SubMenu name={t('Annotation layers')} buttons={subMenuButtons} />
<AnnotationLayerModal
addDangerToast={addDangerToast}
layer={currentAnnotationLayer}
onLayerAdd={onLayerAdd}
onHide={() => setAnnotationLayerModalOpen(false)}
onHide={onModalHide}
show={annotationLayerModalOpen}
/>
{layerCurrentlyDeleting && (
Expand Down