Skip to content

Commit 4403b67

Browse files
Replaced CodeEditor with the correct routes for rending YAML editor
1 parent e4d40b8 commit 4403b67

6 files changed

Lines changed: 275 additions & 144 deletions

File tree

console-extensions.json

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,23 +62,6 @@
6262
}
6363
}
6464
},
65-
{
66-
"type": "console.resource/create",
67-
"properties": {
68-
"name": "default",
69-
"model": {
70-
"group": "stable.example.com",
71-
"kind": "CronTab",
72-
"version": "v1"
73-
},
74-
"component": {
75-
"$codeRef": "CronTabForm"
76-
},
77-
"flags": {
78-
"required": ["CRONTAB"]
79-
}
80-
}
81-
},
8265
{
8366
"type": "console.resource/details-item",
8467
"properties": {
@@ -93,6 +76,16 @@
9376
"path": "spec.cronSpec"
9477
}
9578
},
79+
{
80+
"type": "console.page/route",
81+
"properties": {
82+
"path": "/k8s/ns/:ns/stable.example.com~v1~CronTab/~new/form",
83+
"component": {
84+
"$codeRef": "CronTabForm"
85+
},
86+
"exact": true
87+
}
88+
},
9689
{
9790
"type": "console.resource/details-item",
9891
"properties": {

integration-tests/e2e/crud.cy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe(`${PLUGIN_NAME}`, () => {
5151
nav.sidenav.clickNavLink(["Workloads", "CronTabs"]);
5252
common.resourceTitleShouldHaveText("CronTabs");
5353
listPage.clickCreateForm();
54-
cy.byTestID("page-heading").should("contain", "Create CronTab");
54+
cy.byTestID("page-heading").should("include.text", "Create CronTab");
5555
cy.log("Filling CronTab form");
5656
cy.byTestID("crontab-name").type(cronTabName);
5757
cy.byTestID("crontab-cronSpec").type(cronSpecValue);

locales/en/plugin__console-crontab-plugin.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"Edit annotations": "Edit annotations",
1414
"Edit CronTab": "Edit CronTab",
1515
"Edit labels": "Edit labels",
16+
"Edit YAML": "Edit YAML",
1617
"Error creating CronTab: {{err}}": "Error creating CronTab: {{err}}",
1718
"Image": "Image",
1819
"Increase replicas": "Increase replicas",

src/views/CronTabForm/CronTabForm.tsx

Lines changed: 21 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,12 @@ import {
1717
useK8sModel,
1818
k8sCreate,
1919
useActiveNamespace,
20-
CodeEditor,
2120
} from "@openshift-console/dynamic-plugin-sdk";
2221
import { useNavigate } from "react-router-dom-v5-compat";
2322
import { useCronTabTranslation } from "@crontab-utils/hooks/useCronTabTranslation";
2423
import { cronTabGroupVersionKind } from "src/utils/utils";
2524
import { CronTabKind } from "@crontab-model/types";
26-
import yaml from "js-yaml";
27-
import { defaultCronTabYamlTemplate } from "src/templates/crontab-yaml";
28-
import {
29-
PageHeader,
30-
PageHeaderLinkProps,
31-
} from "@patternfly/react-component-groups";
25+
import { PageHeader } from "@patternfly/react-component-groups";
3226

3327
export const CronTabForm: React.FC = () => {
3428
const [model] = useK8sModel(cronTabGroupVersionKind);
@@ -38,10 +32,7 @@ export const CronTabForm: React.FC = () => {
3832
const [replicas, setReplicas] = useState<number | "">(0);
3933
const [loading, setLoading] = useState(false);
4034
const [error, setError] = useState("");
41-
const [showYaml, setShowYaml] = useState(false);
42-
const [yamlContent, setYamlContent] = useState<string>(
43-
defaultCronTabYamlTemplate.trim()
44-
);
35+
4536
const navigate = useNavigate();
4637
const { t } = useCronTabTranslation();
4738
const [namespace] = useActiveNamespace();
@@ -87,105 +78,23 @@ export const CronTabForm: React.FC = () => {
8778
});
8879
};
8980

90-
const handleYamlSubmit = () => {
91-
setLoading(true);
92-
setError("");
93-
try {
94-
const loaded = yaml.load(yamlContent) as Partial<CronTabKind> | undefined;
95-
if (
96-
!loaded ||
97-
!loaded.metadata ||
98-
!(loaded.metadata.name || loaded.metadata.generateName)
99-
) {
100-
throw new Error(
101-
t("YAML must include metadata.name or metadata.generateName")
102-
);
103-
}
104-
if (!loaded.spec) {
105-
throw new Error(t("YAML must include spec"));
106-
}
107-
108-
const data: CronTabKind = {
109-
apiVersion: (loaded.apiVersion as string) || CRONTAB_APIGROUP_VERSION,
110-
kind: (loaded.kind as string) || CRONTAB_KIND,
111-
metadata: {
112-
...loaded.metadata,
113-
namespace,
114-
},
115-
spec: {
116-
cronSpec,
117-
image,
118-
replicas: replicas ? replicas : 0,
119-
},
120-
};
121-
122-
k8sCreate({ model, data })
123-
.then(() => {
124-
setLoading(false);
125-
navigate(
126-
`/k8s/ns/${namespace}/${cronTabGroupVersionKind.group}~${cronTabGroupVersionKind.version}~${cronTabGroupVersionKind.kind}`
127-
);
128-
})
129-
.catch((err) => {
130-
setLoading(false);
131-
setError(
132-
t("Error creating CronTab: {{err}}", { err: err.toString() })
133-
);
134-
});
135-
} catch (err) {
136-
setLoading(false);
137-
setError(t("Invalid YAML: {{err}}", { err: (err as Error).message }));
138-
}
139-
};
140-
14181
return (
142-
<PageSection isFilled={true} height="sizeToFit">
143-
<PageHeader
144-
title={t("Create CronTab")}
145-
linkProps={
146-
{
147-
label: showYaml ? t("Form View") : t("YAML Editor"),
148-
onClick: () => setShowYaml(!showYaml),
149-
} as PageHeaderLinkProps
150-
}
151-
/>
152-
153-
{showYaml ? (
154-
<>
155-
<CodeEditor
156-
value={yamlContent}
157-
onChange={(val) => setYamlContent(val)}
158-
// @ts-expect-error TODO: fix this
159-
height="500px"
160-
language="yaml"
161-
options={{
162-
wordWrap: "on",
163-
formatOnPaste: true,
164-
}}
165-
/>
166-
{error && <Alert variant="danger" title={error} />}
167-
<ActionGroup>
168-
<Button
169-
type="button"
170-
style={{ marginTop: "2rem", marginRight: "0.5rem" }}
171-
variant="primary"
172-
isDisabled={loading}
173-
onClick={handleYamlSubmit}
174-
isLoading={loading}
175-
>
176-
{t("Create")}
177-
</Button>
178-
<Button
179-
variant="secondary"
180-
style={{ marginTop: "2rem", marginLeft: "0.5rem" }}
181-
onClick={() => navigate(-1)}
182-
isDisabled={loading}
183-
>
184-
{t("Cancel")}
185-
</Button>
186-
</ActionGroup>
187-
</>
188-
) : (
82+
<>
83+
<div data-test="page-heading">
84+
<PageHeader
85+
title={t("Create CronTab")}
86+
linkProps={{
87+
label: t("Edit YAML"),
88+
onClick: (e) => {
89+
e.preventDefault();
90+
navigate(
91+
`/k8s/ns/${namespace}/${cronTabGroupVersionKind.group}~${cronTabGroupVersionKind.version}~${cronTabGroupVersionKind.kind}/~new`
92+
);
93+
},
94+
}}
95+
/>
96+
</div>
97+
<PageSection isFilled={true} height="sizeToFit">
18998
<Form>
19099
<FormGroup label={t("Name")} fieldId="crontab-name" isRequired>
191100
<TextInput
@@ -206,11 +115,7 @@ export const CronTabForm: React.FC = () => {
206115
</HelperText>
207116
</FormHelperText>
208117
</FormGroup>
209-
<FormGroup
210-
label={t("CronSpec")}
211-
fieldId="crontab-cronSpec"
212-
isRequired
213-
>
118+
<FormGroup label={t("CronSpec")} fieldId="crontab-cronSpec">
214119
<TextInput
215120
id="crontab-cronSpec"
216121
data-test="crontab-cronSpec"
@@ -234,7 +139,6 @@ export const CronTabForm: React.FC = () => {
234139
data-test="crontab-image"
235140
value={image || ""}
236141
onChange={(_e, value) => setImage(value)}
237-
required
238142
/>
239143
<FormHelperText>
240144
<HelperText>
@@ -294,8 +198,8 @@ export const CronTabForm: React.FC = () => {
294198
</Button>
295199
</ActionGroup>
296200
</Form>
297-
)}
298-
</PageSection>
201+
</PageSection>
202+
</>
299203
);
300204
};
301205

src/views/CronTabList/CronTabList.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,14 @@ const CronTabList: React.FC<CronTabListProps> = ({
6868
const [data, filteredData, onFilterChange] = useListPageFilter(cronTabs);
6969
const createAccessReview = {
7070
groupVersionKind: cronTabGroupVersionKind,
71-
namespace: namespace || "default",
71+
namespace: namespace || "form",
7272
};
7373

7474
const createURL = `/k8s/ns/${namespace || "default"}/${
7575
cronTabGroupVersionKind.group
76-
}~${cronTabGroupVersionKind.version}~${cronTabGroupVersionKind.kind}/~new`;
76+
}~${cronTabGroupVersionKind.version}~${
77+
cronTabGroupVersionKind.kind
78+
}/~new/form`;
7779

7880
return (
7981
<>

0 commit comments

Comments
 (0)