|
| 1 | +import React from "react"; |
| 2 | +import { useTranslation } from "react-i18next"; |
| 3 | +import { useDispatch, useSelector } from "react-redux"; |
| 4 | + |
| 5 | +import { disableToolTipsHighlight, enableToolTipsHighlight, loadProcessState } from "../../../../actions/nk"; |
| 6 | +import Icon from "../../../../assets/img/toolbarButtons/redeploy.svg"; |
| 7 | +import type { NodesDeploymentData } from "../../../../http/HttpService"; |
| 8 | +import HttpService from "../../../../http/HttpService"; |
| 9 | +import { |
| 10 | + getProcessName, |
| 11 | + getProcessVersionId, |
| 12 | + hasError, |
| 13 | + isRedeployPossible, |
| 14 | + isRedeployVisible, |
| 15 | + isSaveDisabled, |
| 16 | + isValidationResultPresent, |
| 17 | +} from "../../../../reducers/selectors/graph"; |
| 18 | +import { getCapabilities } from "../../../../reducers/selectors/other"; |
| 19 | +import { ACTION_DIALOG_WIDTH } from "../../../../stylesheets/variables"; |
| 20 | +import { useWindows } from "../../../../windowManager"; |
| 21 | +import { WindowKind } from "../../../../windowManager"; |
| 22 | +import type { ToggleProcessActionModalData } from "../../../modals/DeployProcessDialog"; |
| 23 | +import type { ProcessName, ProcessVersionId } from "../../../Process/types"; |
| 24 | +import { ToolbarButton } from "../../../toolbarComponents/toolbarButtons"; |
| 25 | +import type { ToolbarButtonProps } from "../../types"; |
| 26 | + |
| 27 | +export default function RedeployButton(props: ToolbarButtonProps) { |
| 28 | + const dispatch = useDispatch(); |
| 29 | + const isVisible = useSelector(isRedeployVisible); |
| 30 | + const isPossible = useSelector(isRedeployPossible); |
| 31 | + const saveDisabled = useSelector(isSaveDisabled); |
| 32 | + const hasErrors = useSelector(hasError); |
| 33 | + const validationResultPresent = useSelector(isValidationResultPresent); |
| 34 | + const processName = useSelector(getProcessName); |
| 35 | + const processVersionId = useSelector(getProcessVersionId); |
| 36 | + const capabilities = useSelector(getCapabilities); |
| 37 | + const { disabled, type } = props; |
| 38 | + |
| 39 | + const available = validationResultPresent && !disabled && isPossible && capabilities.deploy; |
| 40 | + const { t } = useTranslation(); |
| 41 | + const deployToolTip = !capabilities.deploy |
| 42 | + ? t("panels.actions.redeploy.tooltips.forbidden", "Redeploy forbidden for current scenario.") |
| 43 | + : hasErrors |
| 44 | + ? t("panels.actions.redeploy.tooltips.error", "Cannot redeploy due to errors. Please look at the left panel for more details.") |
| 45 | + : !saveDisabled |
| 46 | + ? t("panels.actions.redeploy.tooltips.unsaved", "You have unsaved changes.") |
| 47 | + : null; |
| 48 | + const deployMouseOver = hasErrors ? () => dispatch(enableToolTipsHighlight()) : null; |
| 49 | + const deployMouseOut = hasErrors ? () => dispatch(disableToolTipsHighlight()) : null; |
| 50 | + |
| 51 | + const { open, confirm } = useWindows(); |
| 52 | + |
| 53 | + const message = t("panels.actions.redeploy.dialog", "Redeploy scenario {{name}}", { name: processName }); |
| 54 | + const action = (name: ProcessName, versionId: ProcessVersionId, comment: string, nodesDeploymentData?: NodesDeploymentData) => |
| 55 | + HttpService.deploy(name, comment, nodesDeploymentData).finally(() => dispatch(loadProcessState(name, versionId))); |
| 56 | + |
| 57 | + const handleOnClick = async () => { |
| 58 | + await HttpService.validateProcessVersion(processName, processVersionId).then((res) => { |
| 59 | + if (!res.data.isLatest) { |
| 60 | + confirm({ |
| 61 | + text: t( |
| 62 | + "panels.actions.confirm-unsafe-deployment.message", |
| 63 | + `There is newer version #${res.data.latestVersion} created by ${res.data.modifiedBy} available. Scenario will be deployed using the newest version. |
| 64 | + You're currently checked out on version #${res.data.localVersion}. |
| 65 | + Are you sure you want to perform this action?`, |
| 66 | + ), |
| 67 | + confirmText: t("panels.actions.confirm-unsafe-deployment.confirmButton", "Confirm"), |
| 68 | + denyText: t("panels.actions.confirm-unsafe-deployment.cancelButton", "Cancel"), |
| 69 | + onConfirmCallback: (confirmed) => { |
| 70 | + if (confirmed) { |
| 71 | + open<ToggleProcessActionModalData>({ |
| 72 | + title: message, |
| 73 | + kind: WindowKind.deployWithParameters, |
| 74 | + width: ACTION_DIALOG_WIDTH, |
| 75 | + meta: { action, displayWarnings: true, actionName: "REDEPLOY" }, |
| 76 | + }); |
| 77 | + } |
| 78 | + }, |
| 79 | + width: window.innerWidth / 3, |
| 80 | + }); |
| 81 | + } else { |
| 82 | + open<ToggleProcessActionModalData>({ |
| 83 | + title: message, |
| 84 | + kind: WindowKind.deployWithParameters, |
| 85 | + width: ACTION_DIALOG_WIDTH, |
| 86 | + meta: { action, displayWarnings: true, actionName: "REDEPLOY" }, |
| 87 | + }); |
| 88 | + } |
| 89 | + }); |
| 90 | + }; |
| 91 | + |
| 92 | + if (isVisible) { |
| 93 | + return ( |
| 94 | + <ToolbarButton |
| 95 | + name={t("panels.actions.redeploy.button", "redeploy")} |
| 96 | + disabled={!available} |
| 97 | + icon={<Icon />} |
| 98 | + title={deployToolTip} |
| 99 | + onClick={handleOnClick} |
| 100 | + onMouseOver={deployMouseOver} |
| 101 | + onMouseOut={deployMouseOut} |
| 102 | + type={type} |
| 103 | + /> |
| 104 | + ); |
| 105 | + } else return <></>; |
| 106 | +} |
0 commit comments