-
Notifications
You must be signed in to change notification settings - Fork 115
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 #1101 from PintoGideon/Update-Pipelines
Clear alerts on both success and error states
- Loading branch information
Showing
7 changed files
with
134 additions
and
208 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,119 +1,140 @@ | ||
import React from "react"; | ||
import { Dispatch } from "redux"; | ||
import { useDispatch } from "react-redux"; | ||
import { ErrorBoundary } from "react-error-boundary"; | ||
import { PluginInstance } from "@fnndsc/chrisapi"; | ||
import { Button, Modal, ModalVariant } from "@patternfly/react-core"; | ||
import { connect } from "react-redux"; | ||
import { ApplicationState } from "../../store/root/applicationState"; | ||
import { Feed, PluginInstance } from "@fnndsc/chrisapi"; | ||
import { | ||
clearDeleteState, | ||
deleteNode, | ||
deleteNodeError, | ||
} from "../../store/pluginInstance/actions"; | ||
import TrashIcon from "@patternfly/react-icons/dist/esm/icons/trash-icon"; | ||
|
||
import { useMutation } from "@tanstack/react-query"; | ||
import { Alert } from "antd"; | ||
import { useEffect } from "react"; | ||
import { useDispatch } from "react-redux"; | ||
import { fetchResource } from "../../api/common"; | ||
import { useTypedSelector } from "../../store/hooks"; | ||
import { getNodeOperations } from "../../store/plugin/actions"; | ||
import { | ||
getPluginInstancesSuccess, | ||
getSelectedPlugin, | ||
} from "../../store/pluginInstance/actions"; | ||
import { getPluginInstanceStatusRequest } from "../../store/resources/actions"; | ||
import { SpinContainer } from "../Common"; | ||
|
||
interface DeleteNodeProps { | ||
selectedPlugin?: PluginInstance; | ||
deleteNode: (instance: PluginInstance, feed: Feed) => void; | ||
deleteNodeState: { | ||
error: string; | ||
success: boolean; | ||
}; | ||
} | ||
|
||
const DeleteNode: React.FC<DeleteNodeProps> = ({ | ||
selectedPlugin, | ||
deleteNode, | ||
deleteNodeState, | ||
}: DeleteNodeProps) => { | ||
const DeleteNode = () => { | ||
const dispatch = useDispatch(); | ||
const feed = useTypedSelector((state) => state.feed.currentFeed); | ||
const { deleteNode: isModalOpen } = useTypedSelector( | ||
(state) => state.plugin.nodeOperations, | ||
); | ||
const { data } = feed; | ||
const handleModalToggle = React.useCallback(() => { | ||
dispatch(getNodeOperations("deleteNode")); | ||
if (isModalOpen) { | ||
dispatch(clearDeleteState()); | ||
} | ||
}, [dispatch, isModalOpen]); | ||
const { selectedPlugin } = useTypedSelector((state) => state.instance); | ||
const currentFeed = useTypedSelector((state) => state.feed.currentFeed.data); | ||
|
||
const handleDelete = async () => { | ||
if (selectedPlugin && data) { | ||
deleteNode(selectedPlugin, data); | ||
handleModalToggle(); | ||
if (selectedPlugin) { | ||
try { | ||
const statuses = [ | ||
"finishedSuccessfully", | ||
"finishedWithError", | ||
"cancelled", | ||
]; | ||
const status = selectedPlugin.data.status; | ||
|
||
if (!statuses.includes(status)) { | ||
// Always cancel an active node first to avoid side effects | ||
await selectedPlugin.put({ | ||
status: "cancelled", | ||
}); | ||
} | ||
|
||
await selectedPlugin.delete(); | ||
//Fetch Resources again because I don't understand how delete works in cube. It's highly inconsistent | ||
if (currentFeed) { | ||
const params = { limit: 15, offset: 0 }; | ||
|
||
const fn = currentFeed.getPluginInstances; | ||
const boundFn = fn.bind(currentFeed); | ||
const { resource: pluginInstances } = | ||
await fetchResource<PluginInstance>(params, boundFn); | ||
|
||
const selected = pluginInstances[pluginInstances.length - 1]; | ||
const pluginInstanceObj = { | ||
selected, | ||
pluginInstances, | ||
}; | ||
|
||
dispatch(getSelectedPlugin(selected)); | ||
dispatch(getPluginInstancesSuccess(pluginInstanceObj)); | ||
dispatch(getPluginInstanceStatusRequest(pluginInstanceObj)); | ||
} | ||
} catch (e) { | ||
throw e; | ||
} | ||
} else { | ||
dispatch( | ||
deleteNodeError({ | ||
error_message: "Please wait for the plugin to finish running", | ||
}), | ||
); | ||
throw new Error("Please select a node to delete"); | ||
} | ||
}; | ||
|
||
const mutation = useMutation({ | ||
mutationFn: () => handleDelete(), | ||
}); | ||
|
||
const handleModalToggle = () => { | ||
dispatch(getNodeOperations("deleteNode")); | ||
}; | ||
|
||
useEffect(() => { | ||
if (mutation.isSuccess) { | ||
setTimeout(() => { | ||
mutation.reset(); | ||
dispatch(getNodeOperations("deleteNode")); | ||
}, 1000); | ||
} | ||
}, [mutation.isSuccess]); | ||
|
||
return ( | ||
<React.Fragment> | ||
<ErrorBoundary fallback={<FallbackComponent />}> | ||
<Button | ||
disabled={!selectedPlugin} | ||
onClick={handleModalToggle} | ||
icon={<TrashIcon />} | ||
type="button" | ||
> | ||
Delete Node{" "} | ||
<span style={{ padding: "2px", color: "#F5F5DC" }}>( D )</span> | ||
</Button> | ||
<Modal | ||
variant={ModalVariant.small} | ||
title="Delete Node Confirmation" | ||
isOpen={isModalOpen} | ||
onClose={handleModalToggle} | ||
actions={[ | ||
<React.Fragment key="modal-action"> | ||
<Button key="confirm" variant="primary" onClick={handleDelete}> | ||
Confirm | ||
</Button> | ||
<Button | ||
key="cancel" | ||
variant="primary" | ||
onClick={handleModalToggle} | ||
> | ||
Cancel | ||
</Button> | ||
</React.Fragment>, | ||
]} | ||
> | ||
<> | ||
<Button | ||
isDisabled={ | ||
selectedPlugin.data.plugin_type === "fs" && | ||
selectedPlugin.data.plugin_name === "pl-dircopy" | ||
} | ||
onClick={handleModalToggle} | ||
icon={<TrashIcon />} | ||
variant="primary" | ||
type="button" | ||
> | ||
Delete Node{" "} | ||
</Button> | ||
<Modal | ||
variant={ModalVariant.small} | ||
title="Delete Selected Node" | ||
isOpen={isModalOpen} | ||
onClose={handleModalToggle} | ||
actions={[ | ||
<> | ||
<Button | ||
key="confirm" | ||
variant="primary" | ||
onClick={() => mutation.mutate()} | ||
> | ||
Confirm | ||
</Button> | ||
<Button key="cancel" variant="primary" onClick={handleModalToggle}> | ||
Cancel | ||
</Button> | ||
</>, | ||
]} | ||
> | ||
<span> | ||
{" "} | ||
Deleting a node will delete all it's descendants as well. Please | ||
confirm if you are sure | ||
{deleteNodeState.error && <span>{deleteNodeState.error}</span>} | ||
</Modal> | ||
</ErrorBoundary> | ||
</React.Fragment> | ||
</span> | ||
|
||
{mutation.isPending && <SpinContainer title="Deleting..." />} | ||
{mutation.isError && ( | ||
<Alert type="error" description={mutation.error.message} /> | ||
)} | ||
{mutation.isSuccess && ( | ||
<Alert type="success" description="Deleted Successfully" /> | ||
)} | ||
</Modal> | ||
</> | ||
); | ||
}; | ||
|
||
const mapStateToProps = (state: ApplicationState) => ({ | ||
selectedPlugin: state.instance.selectedPlugin, | ||
deleteNodeState: state.instance.deleteNode, | ||
}); | ||
|
||
const mapDispatchToProps = (dispatch: Dispatch) => ({ | ||
deleteNode: (instance: PluginInstance, feed: Feed) => | ||
dispatch(deleteNode(instance, feed)), | ||
}); | ||
|
||
const DeleteNodeConnect = connect( | ||
mapStateToProps, | ||
mapDispatchToProps, | ||
)(DeleteNode); | ||
|
||
export default DeleteNodeConnect; | ||
|
||
function FallbackComponent() { | ||
return <div>Oops ! Delete was not successful. Please try again.</div>; | ||
} | ||
export default DeleteNode; |
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
Oops, something went wrong.