Skip to content

Commit 1c74bc7

Browse files
committed
Refactor Form View to Edit only
Signed-off-by: Charles Thao <[email protected]>
1 parent 28f2471 commit 1c74bc7

File tree

1 file changed

+55
-36
lines changed

1 file changed

+55
-36
lines changed

workspaces/frontend/src/app/pages/WorkspaceKinds/Form/WorkspaceKindForm.tsx

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useCallback, useState } from 'react';
1+
import React, { useCallback, useMemo, useState } from 'react';
22
import {
33
Button,
44
Content,
@@ -13,6 +13,7 @@ import {
1313
} from '@patternfly/react-core';
1414
import { useTypedNavigate } from '~/app/routerHelper';
1515
import useGenericObjectState from '~/app/hooks/useGenericObjectState';
16+
import { useNotebookAPI } from '~/app/hooks/useNotebookAPI';
1617
import { WorkspaceKindFormData } from '~/app/types';
1718
import { WorkspaceKindFileUpload } from './fileUpload/WorkspaceKindFileUpload';
1819
import { WorkspaceKindFormProperties } from './properties/WorkspaceKindFormProperties';
@@ -27,6 +28,7 @@ export type ValidationStatus = 'success' | 'error' | 'default';
2728

2829
export 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,40 @@ 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({}, { data: 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+
}, [yamlValue, api, mode]);
82+
83+
const canSubmit = useMemo(
84+
() => !isSubmitting && yamlValue.length > 0 && validated === 'success',
85+
[yamlValue, isSubmitting, validated],
86+
);
6987

7088
const cancel = useCallback(() => {
71-
navigate('workspaceKindCreate');
89+
navigate('workspaceKinds');
7290
}, [navigate]);
7391

7492
return (
@@ -83,29 +101,30 @@ export const WorkspaceKindForm: React.FC = () => {
83101
</Content>
84102
<Content component={ContentVariants.p}>
85103
{view === WorkspaceKindFormView.FileUpload
86-
? `Please upload a Workspace Kind YAML file. Select 'Form View' to view
87-
and edit the workspace kind's information`
104+
? `Please upload or drag and drop a Workspace Kind YAML file.`
88105
: `View and edit the Workspace Kind's information. Some fields may not be
89106
represented in this form`}
90107
</Content>
91108
</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>
109+
{mode === 'edit' && (
110+
<FlexItem>
111+
<ToggleGroup className="workspace-kind-form-header" aria-label="Toggle form view">
112+
<ToggleGroupItem
113+
text="YAML Upload"
114+
buttonId={workspaceKindFileUploadId}
115+
isSelected={view === WorkspaceKindFormView.FileUpload}
116+
onChange={handleViewClick}
117+
/>
118+
<ToggleGroupItem
119+
text="Form View"
120+
buttonId="workspace-kind-form-form-view"
121+
isSelected={view === WorkspaceKindFormView.Form}
122+
onChange={handleViewClick}
123+
isDisabled={yamlValue === '' || validated === 'error'}
124+
/>
125+
</ToggleGroup>
126+
</FlexItem>
127+
)}
109128
</Flex>
110129
</Stack>
111130
</PageSection>
@@ -144,8 +163,8 @@ export const WorkspaceKindForm: React.FC = () => {
144163
<Button
145164
variant="primary"
146165
ouiaId="Primary"
147-
onClick={handleCreate}
148-
isDisabled={!isSubmitting}
166+
onClick={handleSubmit}
167+
isDisabled={!canSubmit}
149168
>
150169
{mode === 'create' ? 'Create' : 'Edit'}
151170
</Button>

0 commit comments

Comments
 (0)