1- import  React ,  {  useCallback ,  useState  }  from  'react' ; 
1+ import  React ,  {  useCallback ,  useMemo ,   useState  }  from  'react' ; 
22import  { 
33  Button , 
44  Content , 
@@ -13,6 +13,7 @@ import {
1313}  from  '@patternfly/react-core' ; 
1414import  {  useTypedNavigate  }  from  '~/app/routerHelper' ; 
1515import  useGenericObjectState  from  '~/app/hooks/useGenericObjectState' ; 
16+ import  {  useNotebookAPI  }  from  '~/app/hooks/useNotebookAPI' ; 
1617import  {  WorkspaceKindFormData  }  from  '~/app/types' ; 
1718import  {  WorkspaceKindFileUpload  }  from  './fileUpload/WorkspaceKindFileUpload' ; 
1819import  {  WorkspaceKindFormProperties  }  from  './properties/WorkspaceKindFormProperties' ; 
@@ -27,6 +28,7 @@ export type ValidationStatus = 'success' | 'error' | 'default';
2728
2829export  const  WorkspaceKindForm : React . FC  =  ( )  =>  { 
2930  const  navigate  =  useTypedNavigate ( ) ; 
31+   const  {  api }  =  useNotebookAPI ( ) ; 
3032  // TODO: Detect mode by route 
3133  const  [ mode ]  =  useState ( 'create' ) ; 
3234  const  [ yamlValue ,  setYamlValue ]  =  useState ( '' ) ; 
@@ -35,14 +37,6 @@ export const WorkspaceKindForm: React.FC = () => {
3537  const  [ validated ,  setValidated ]  =  useState < ValidationStatus > ( 'default' ) ; 
3638  const  workspaceKindFileUploadId  =  'workspace-kind-form-fileupload-view' ; 
3739
38-   const  handleViewClick  =  ( event : React . MouseEvent < unknown >  |  React . KeyboardEvent  |  MouseEvent )  =>  { 
39-     const  {  id }  =  event . currentTarget  as  HTMLElement ; 
40-     setView ( 
41-       id  ===  workspaceKindFileUploadId 
42-         ? WorkspaceKindFormView . FileUpload 
43-         : WorkspaceKindFormView . Form , 
44-     ) ; 
45-   } ; 
4640  const  [ data ,  setData ,  resetData ]  =  useGenericObjectState < WorkspaceKindFormData > ( { 
4741    properties : { 
4842      displayName : '' , 
@@ -59,16 +53,41 @@ export const WorkspaceKindForm: React.FC = () => {
5953    } , 
6054  } ) ; 
6155
62-   const  handleCreate  =  useCallback ( ( )  =>  { 
56+   const  handleViewClick  =  useCallback ( 
57+     ( event : React . MouseEvent < unknown >  |  React . KeyboardEvent  |  MouseEvent )  =>  { 
58+       const  {  id }  =  event . currentTarget  as  HTMLElement ; 
59+       setView ( 
60+         id  ===  workspaceKindFileUploadId 
61+           ? WorkspaceKindFormView . FileUpload 
62+           : WorkspaceKindFormView . Form , 
63+       ) ; 
64+     } , 
65+     [ ] , 
66+   ) ; 
67+ 
68+   const  handleSubmit  =  useCallback ( async  ( )  =>  { 
69+     setIsSubmitting ( true ) ; 
6370    // TODO: Complete handleCreate with API call to create a new WS kind 
64-     if  ( ! Object . keys ( data ) . length )  { 
65-       return ; 
71+     try  { 
72+       if  ( mode  ===  'create' )  { 
73+         const  newWorkspaceKind  =  await  api . createWorkspaceKind ( { } ,  yamlValue ) ; 
74+         console . info ( 'New workspace kind created:' ,  JSON . stringify ( newWorkspaceKind ) ) ; 
75+       } 
76+     }  catch  ( err )  { 
77+       console . error ( `Error ${ mode  ===  'edit'  ? 'editing'  : 'creating' }   workspace kind: ${ err }  ` ) ; 
78+     }  finally  { 
79+       setIsSubmitting ( false ) ; 
6680    } 
67-     setIsSubmitting ( true ) ; 
68-   } ,  [ data ] ) ; 
81+     navigate ( 'workspaceKinds' ) ; 
82+   } ,  [ navigate ,  mode ,  api ,  yamlValue ] ) ; 
83+ 
84+   const  canSubmit  =  useMemo ( 
85+     ( )  =>  ! isSubmitting  &&  yamlValue . length  >  0  &&  validated  ===  'success' , 
86+     [ yamlValue ,  isSubmitting ,  validated ] , 
87+   ) ; 
6988
7089  const  cancel  =  useCallback ( ( )  =>  { 
71-     navigate ( 'workspaceKindCreate ' ) ; 
90+     navigate ( 'workspaceKinds ' ) ; 
7291  } ,  [ navigate ] ) ; 
7392
7493  return  ( 
@@ -83,29 +102,30 @@ export const WorkspaceKindForm: React.FC = () => {
83102                </ Content > 
84103                < Content  component = { ContentVariants . p } > 
85104                  { view  ===  WorkspaceKindFormView . FileUpload 
86-                     ? `Please upload a Workspace Kind YAML file. Select 'Form View' to view 
87-                     and edit the workspace kind's information` 
105+                     ? `Please upload or drag and drop a Workspace Kind YAML file.` 
88106                    : `View and edit the Workspace Kind's information. Some fields may not be 
89107                      represented in this form` } 
90108                </ Content > 
91109              </ FlexItem > 
92-               < FlexItem > 
93-                 < ToggleGroup  className = "workspace-kind-form-header"  aria-label = "Toggle form view" > 
94-                   < ToggleGroupItem 
95-                     text = "YAML Upload" 
96-                     buttonId = { workspaceKindFileUploadId } 
97-                     isSelected = { view  ===  WorkspaceKindFormView . FileUpload } 
98-                     onChange = { handleViewClick } 
99-                   /> 
100-                   < ToggleGroupItem 
101-                     text = "Form View" 
102-                     buttonId = "workspace-kind-form-form-view" 
103-                     isSelected = { view  ===  WorkspaceKindFormView . Form } 
104-                     onChange = { handleViewClick } 
105-                     isDisabled = { yamlValue  ===  ''  ||  validated  ===  'error' } 
106-                   /> 
107-                 </ ToggleGroup > 
108-               </ FlexItem > 
110+               { mode  ===  'edit'  &&  ( 
111+                 < FlexItem > 
112+                   < ToggleGroup  className = "workspace-kind-form-header"  aria-label = "Toggle form view" > 
113+                     < ToggleGroupItem 
114+                       text = "YAML Upload" 
115+                       buttonId = { workspaceKindFileUploadId } 
116+                       isSelected = { view  ===  WorkspaceKindFormView . FileUpload } 
117+                       onChange = { handleViewClick } 
118+                     /> 
119+                     < ToggleGroupItem 
120+                       text = "Form View" 
121+                       buttonId = "workspace-kind-form-form-view" 
122+                       isSelected = { view  ===  WorkspaceKindFormView . Form } 
123+                       onChange = { handleViewClick } 
124+                       isDisabled = { yamlValue  ===  ''  ||  validated  ===  'error' } 
125+                     /> 
126+                   </ ToggleGroup > 
127+                 </ FlexItem > 
128+               ) } 
109129            </ Flex > 
110130          </ Stack > 
111131        </ PageSection > 
@@ -144,8 +164,8 @@ export const WorkspaceKindForm: React.FC = () => {
144164            < Button 
145165              variant = "primary" 
146166              ouiaId = "Primary" 
147-               onClick = { handleCreate } 
148-               isDisabled = { ! isSubmitting } 
167+               onClick = { handleSubmit } 
168+               isDisabled = { ! canSubmit } 
149169            > 
150170              { mode  ===  'create'  ? 'Create'  : 'Edit' } 
151171            </ Button > 
0 commit comments