@@ -17,18 +17,12 @@ import {
1717 useK8sModel ,
1818 k8sCreate ,
1919 useActiveNamespace ,
20- CodeEditor ,
2120} from "@openshift-console/dynamic-plugin-sdk" ;
2221import { useNavigate } from "react-router-dom-v5-compat" ;
2322import { useCronTabTranslation } from "@crontab-utils/hooks/useCronTabTranslation" ;
2423import { cronTabGroupVersionKind } from "src/utils/utils" ;
2524import { 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
3327export 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
0 commit comments