|
| 1 | +import React, { useState } from "react"; |
| 2 | +import { |
| 3 | + Form, |
| 4 | + FormGroup, |
| 5 | + TextInput, |
| 6 | + ActionGroup, |
| 7 | + Button, |
| 8 | + PageSection, |
| 9 | + Title, |
| 10 | + Alert, |
| 11 | + NumberInput, |
| 12 | + FormHelperText, |
| 13 | + HelperText, |
| 14 | + HelperTextItem, |
| 15 | +} from "@patternfly/react-core"; |
| 16 | +import { CRONTAB_APIGROUP_VERSION, CRONTAB_KIND } from "src/const"; |
| 17 | +import { |
| 18 | + useK8sModel, |
| 19 | + k8sCreate, |
| 20 | + useActiveNamespace, |
| 21 | +} from "@openshift-console/dynamic-plugin-sdk"; |
| 22 | +import { useNavigate } from "react-router-dom-v5-compat"; |
| 23 | +import { useCronTabTranslation } from "@crontab-utils/hooks/useCronTabTranslation"; |
| 24 | +import { cronTabGroupVersionKind } from "src/utils/utils"; |
| 25 | +import { CronTabKind } from "@crontab-model/types"; |
| 26 | + |
| 27 | +export const CronTabForm: React.FC = () => { |
| 28 | + const [model] = useK8sModel(cronTabGroupVersionKind); |
| 29 | + const [name, setName] = useState(""); |
| 30 | + const [cronSpec, setCronSpec] = useState(""); |
| 31 | + const [image, setImage] = useState(""); |
| 32 | + const [replicas, setReplicas] = useState<number | "">(0); |
| 33 | + const [loading, setLoading] = useState(false); |
| 34 | + const [error, setError] = useState(""); |
| 35 | + const navigate = useNavigate(); |
| 36 | + const { t } = useCronTabTranslation(); |
| 37 | + const [namespace] = useActiveNamespace(); |
| 38 | + |
| 39 | + const onChangeReplicas = (event: React.FormEvent<HTMLInputElement>) => { |
| 40 | + const value = (event.target as HTMLInputElement).value; |
| 41 | + setReplicas(value === "" ? value : +value); |
| 42 | + }; |
| 43 | + const onReplicasMinus = () => { |
| 44 | + setReplicas((currentReplicas) => (currentReplicas || 0) - 1); |
| 45 | + }; |
| 46 | + const onReplicasPlus = () => { |
| 47 | + setReplicas((currentReplicas) => (currentReplicas || 0) + 1); |
| 48 | + }; |
| 49 | + |
| 50 | + const handleSubmit = (e: React.FormEvent) => { |
| 51 | + e.preventDefault(); |
| 52 | + setLoading(true); |
| 53 | + setError(""); |
| 54 | + |
| 55 | + const data: CronTabKind = { |
| 56 | + apiVersion: CRONTAB_APIGROUP_VERSION, |
| 57 | + kind: CRONTAB_KIND, |
| 58 | + metadata: { |
| 59 | + name, |
| 60 | + namespace, |
| 61 | + }, |
| 62 | + spec: { |
| 63 | + cronSpec, |
| 64 | + image, |
| 65 | + replicas: replicas ? replicas : 0, |
| 66 | + }, |
| 67 | + }; |
| 68 | + |
| 69 | + k8sCreate({ model, data }) |
| 70 | + .then(() => { |
| 71 | + setLoading(false); |
| 72 | + navigate(`/k8s/ns/${namespace}/stable.example.com~v1~CronTab/${name}`); |
| 73 | + }) |
| 74 | + .catch((err) => { |
| 75 | + setLoading(false); |
| 76 | + setError(t("Error creating CronTab: {{err}}", { err: err.toString() })); |
| 77 | + }); |
| 78 | + }; |
| 79 | + return ( |
| 80 | + <PageSection> |
| 81 | + <Title headingLevel="h1" data-test="page-heading"> |
| 82 | + {t("Create CronTab")} |
| 83 | + </Title> |
| 84 | + <Form> |
| 85 | + <FormGroup label={t("Name")} fieldId="crontab-name" isRequired> |
| 86 | + <TextInput |
| 87 | + id="crontab-name" |
| 88 | + data-test="crontab-name" |
| 89 | + name="name" |
| 90 | + onChange={(_e, value) => setName(value)} |
| 91 | + value={name} |
| 92 | + required |
| 93 | + /> |
| 94 | + <FormHelperText> |
| 95 | + <HelperText> |
| 96 | + <HelperTextItem> |
| 97 | + {t("A unique identifier for this CronTab within the project.")} |
| 98 | + </HelperTextItem> |
| 99 | + </HelperText> |
| 100 | + </FormHelperText> |
| 101 | + </FormGroup> |
| 102 | + <FormGroup label={t("CronSpec")} fieldId="crontab-cronSpec" isRequired> |
| 103 | + <TextInput |
| 104 | + id="crontab-cronSpec" |
| 105 | + data-test="crontab-cronSpec" |
| 106 | + value={cronSpec || ""} |
| 107 | + onChange={(_e, value) => setCronSpec(value)} |
| 108 | + required |
| 109 | + /> |
| 110 | + <FormHelperText> |
| 111 | + <HelperText> |
| 112 | + <HelperTextItem> |
| 113 | + {t( |
| 114 | + "Defines the schedule on which the job will run (e.g., */5 * * * *)." |
| 115 | + )} |
| 116 | + </HelperTextItem> |
| 117 | + </HelperText> |
| 118 | + </FormHelperText> |
| 119 | + </FormGroup> |
| 120 | + <FormGroup label={t("Image")} fieldId="crontab-image" isRequired> |
| 121 | + <TextInput |
| 122 | + id="crontab-image" |
| 123 | + data-test="crontab-image" |
| 124 | + value={image || ""} |
| 125 | + onChange={(_e, value) => setImage(value)} |
| 126 | + required |
| 127 | + /> |
| 128 | + <FormHelperText> |
| 129 | + <HelperText> |
| 130 | + <HelperTextItem> |
| 131 | + {t( |
| 132 | + "Specifies the container image to be executed by the CronTab." |
| 133 | + )} |
| 134 | + </HelperTextItem> |
| 135 | + </HelperText> |
| 136 | + </FormHelperText> |
| 137 | + </FormGroup> |
| 138 | + <FormGroup label={t("Replicas")} fieldId="crontab-replicas" isRequired> |
| 139 | + <NumberInput |
| 140 | + id="crontab-replicas" |
| 141 | + data-test="crontab-replicas" |
| 142 | + value={replicas} |
| 143 | + onChange={onChangeReplicas} |
| 144 | + onMinus={onReplicasMinus} |
| 145 | + onPlus={onReplicasPlus} |
| 146 | + inputName={t("replicas")} |
| 147 | + inputAriaLabel={t("Number of replicas")} |
| 148 | + minusBtnAriaLabel={t("Decrease replicas")} |
| 149 | + plusBtnAriaLabel={t("Increase replicas")} |
| 150 | + required |
| 151 | + min={0} |
| 152 | + /> |
| 153 | + <FormHelperText> |
| 154 | + <HelperText> |
| 155 | + <HelperTextItem> |
| 156 | + {t("The desired number of instances of this CronTab to run.")} |
| 157 | + </HelperTextItem> |
| 158 | + </HelperText> |
| 159 | + </FormHelperText> |
| 160 | + </FormGroup> |
| 161 | + {error && <Alert variant="danger" title={error} />} |
| 162 | + <ActionGroup> |
| 163 | + <Button |
| 164 | + type="button" |
| 165 | + variant="primary" |
| 166 | + isDisabled={loading || !name || !cronSpec || !image} |
| 167 | + onClick={handleSubmit} |
| 168 | + isLoading={loading} |
| 169 | + data-test="save-changes" |
| 170 | + > |
| 171 | + {t("Create")} |
| 172 | + </Button> |
| 173 | + <Button |
| 174 | + variant="secondary" |
| 175 | + isDisabled={loading} |
| 176 | + onClick={() => navigate(-1)} |
| 177 | + > |
| 178 | + {t("Cancel")} |
| 179 | + </Button> |
| 180 | + </ActionGroup> |
| 181 | + </Form> |
| 182 | + </PageSection> |
| 183 | + ); |
| 184 | +}; |
| 185 | + |
| 186 | +export default CronTabForm; |
0 commit comments