|
| 1 | +import { Modal, Stack, TextInput } from "@carbon/react" |
| 2 | +import { InlineLoadingStatus } from "@carbon/react/lib/components/InlineLoading/InlineLoading" |
| 3 | +import { useState } from "react" |
| 4 | +import { createPortal } from "react-dom" |
| 5 | +import { getEipFlow } from "../../../singletons/store/storeViews" |
| 6 | +import { keipClient } from "../../endpoints/deploy/keipClient" |
| 7 | +import { fetchXmlTranslation } from "../../endpoints/translation/translateFlowToXml" |
| 8 | + |
| 9 | +interface DeployRouteModalProps { |
| 10 | + open: boolean |
| 11 | + setOpen: (open: boolean) => void |
| 12 | +} |
| 13 | + |
| 14 | +const getLoadingDescription = (status: InlineLoadingStatus) => { |
| 15 | + switch (status) { |
| 16 | + case "active": |
| 17 | + return "Deploying Route" |
| 18 | + case "error": |
| 19 | + return "Deploy Failed" |
| 20 | + case "finished": |
| 21 | + return "Deployed" |
| 22 | + case "inactive": |
| 23 | + return "" |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +// TODO: Display status for deployed routes once Keip controller endpoint adds support for status checks. |
| 28 | +// TODO: Store deployed route name and namespace in the AppStore to survive refreshes. |
| 29 | +export const DeployRouteModal = ({ open, setOpen }: DeployRouteModalProps) => { |
| 30 | + const [loadingStatus, setLoadingStatus] = |
| 31 | + useState<InlineLoadingStatus>("inactive") |
| 32 | + |
| 33 | + const [name, setName] = useState("") |
| 34 | + const [namespace, setNamespace] = useState("default") |
| 35 | + |
| 36 | + const onClose = () => { |
| 37 | + setOpen(false) |
| 38 | + loadingStatus !== "active" && setLoadingStatus("inactive") |
| 39 | + } |
| 40 | + |
| 41 | + const handleDeploy = () => { |
| 42 | + setLoadingStatus("active") |
| 43 | + deployDiagram() |
| 44 | + .then(() => setLoadingStatus("finished")) |
| 45 | + .catch((err) => { |
| 46 | + console.error(err) |
| 47 | + setLoadingStatus("error") |
| 48 | + }) |
| 49 | + } |
| 50 | + |
| 51 | + const deployDiagram = async () => { |
| 52 | + const flow = getEipFlow() |
| 53 | + |
| 54 | + if (flow.nodes?.length === 0) { |
| 55 | + throw new Error("Failed to deploy - diagram is empty") |
| 56 | + } |
| 57 | + |
| 58 | + const xml = await fetchXmlTranslation(flow, new AbortController()) |
| 59 | + |
| 60 | + if (!xml) { |
| 61 | + throw new Error("Failed to deploy - translated XML is empty") |
| 62 | + } |
| 63 | + |
| 64 | + const request = { |
| 65 | + routes: [ |
| 66 | + { |
| 67 | + name, |
| 68 | + namespace, |
| 69 | + xml, |
| 70 | + }, |
| 71 | + ], |
| 72 | + } |
| 73 | + |
| 74 | + await keipClient.deploy(request) |
| 75 | + } |
| 76 | + |
| 77 | + return createPortal( |
| 78 | + <Modal |
| 79 | + loadingDescription={getLoadingDescription(loadingStatus)} |
| 80 | + loadingStatus={loadingStatus} |
| 81 | + modalHeading="Deploy Routes" |
| 82 | + onRequestClose={onClose} |
| 83 | + onRequestSubmit={handleDeploy} |
| 84 | + open={open} |
| 85 | + primaryButtonText="Deploy" |
| 86 | + secondaryButtonText="Cancel" |
| 87 | + > |
| 88 | + <Stack gap={6}> |
| 89 | + <TextInput |
| 90 | + id="name-input" |
| 91 | + labelText="Name" |
| 92 | + placeholder="Route name" |
| 93 | + value={name} |
| 94 | + onChange={(e) => setName(e.target.value)} |
| 95 | + /> |
| 96 | + <TextInput |
| 97 | + id="namespace-input" |
| 98 | + labelText="Namespace" |
| 99 | + value={namespace} |
| 100 | + onChange={(e) => setNamespace(e.target.value)} |
| 101 | + /> |
| 102 | + </Stack> |
| 103 | + </Modal>, |
| 104 | + document.body |
| 105 | + ) |
| 106 | +} |
0 commit comments