Skip to content

Commit

Permalink
revert some earlier changes and use permissions to avoid sending set …
Browse files Browse the repository at this point in the history
…rq API req
  • Loading branch information
salonig23 committed Aug 15, 2024
1 parent 627e42b commit 4b0d1cd
Showing 1 changed file with 51 additions and 75 deletions.
126 changes: 51 additions & 75 deletions webui/react/src/components/WorkspaceCreateModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ const WorkspaceCreateModalComponent: React.FC<Props> = ({ onClose, workspaceId }
[resourceManagers, workspaceId],
);

const loadedResourceQuotaList = Loadable.getOrElse({}, resourceQuotasList);

const initFields = useCallback(
(ws?: Workspace) => {
if (ws) {
Expand All @@ -147,16 +145,18 @@ const WorkspaceCreateModalComponent: React.FC<Props> = ({ onClose, workspaceId }
namespaceBindingsList.forEach((bindingsList) => {
form.setFieldValue('bindings', { ...bindingsList });
});
for (const key in loadedResourceQuotaList) {
form.setFieldValue(['resourceQuotas', key], loadedResourceQuotaList[key]);
}
resourceQuotasList.forEach((quotasList) => {
form.setFieldValue('resourceQuotas', { ...quotasList });
});
}
},
[form, loadedResourceQuotaList, namespaceBindingsList],
[form, namespaceBindingsList, resourceQuotasList],
);

const loadableWorkspace = useObservable(workspaceStore.getWorkspace(workspaceId || 0));
const workspace = Loadable.getOrElse(undefined, loadableWorkspace);
const isViewing = !!workspace && !canModifyWorkspace({ workspace });
const isEditing = workspaceId !== undefined;

useEffect(() => {
initFields(workspace || undefined);
Expand All @@ -171,7 +171,7 @@ const WorkspaceCreateModalComponent: React.FC<Props> = ({ onClose, workspaceId }
}, [canModifyWorkspaceAgentUserGroup, canModifyWorkspaceCheckpointStorage, workspaceId]);

const modalContent = useMemo(() => {
if (workspaceId && loadableWorkspace === NotLoaded) return <Spinner spinning />;
if (isEditing && loadableWorkspace === NotLoaded) return <Spinner spinning />;
return (
<Form
autoComplete="off"
Expand All @@ -189,10 +189,7 @@ const WorkspaceCreateModalComponent: React.FC<Props> = ({ onClose, workspaceId }
required: true,
},
]}>
<Input
disabled={(workspace && !canModifyWorkspace({ workspace })) ?? false}
maxLength={80}
/>
<Input disabled={isViewing} maxLength={80} />
</Form.Item>
{canSetWorkspaceNamespaceBindings && resourceManagers.length > 0 && (
<>
Expand All @@ -218,12 +215,7 @@ const WorkspaceCreateModalComponent: React.FC<Props> = ({ onClose, workspaceId }
name={['bindings', name, 'autoCreateNamespace']}
valuePropName="checked">
<Toggle
onChange={() => {
form.setFieldValue(['resourceQuotas', name], undefined);
if (loadedResourceQuotaList[name]) {
delete loadedResourceQuotaList[name];
}
}}
onChange={() => form.setFieldValue(['resourceQuotas', name], undefined)}
/>
</Form.Item>
{canSetResourceQuotas && (
Expand Down Expand Up @@ -252,7 +244,7 @@ const WorkspaceCreateModalComponent: React.FC<Props> = ({ onClose, workspaceId }
)}
{!canSetWorkspaceNamespaceBindings &&
canViewResourceQuotas &&
workspaceId &&
isEditing &&
info.branding === BrandingType.HPE &&
resourceManagers.length > 0 && (
<>
Expand All @@ -261,9 +253,7 @@ const WorkspaceCreateModalComponent: React.FC<Props> = ({ onClose, workspaceId }
<>
{resourceManagers.map((name) => (
<Fragment key={name}>
<Form.Item label={`Cluster Name: ${name}`} name={['resourceQuotas', name]}>
<Input disabled={true} />
</Form.Item>
<Form.Item label={`Cluster Name: ${name}`} name={['resourceQuotas', name]} />
</Fragment>
))}
</>
Expand Down Expand Up @@ -369,12 +359,11 @@ const WorkspaceCreateModalComponent: React.FC<Props> = ({ onClose, workspaceId }
</Form>
);
}, [
workspaceId,
loadableWorkspace,
form,
idPrefix,
workspace,
canModifyWorkspace,
isEditing,
isViewing,
canSetWorkspaceNamespaceBindings,
resourceManagers,
canViewResourceQuotas,
Expand All @@ -386,7 +375,6 @@ const WorkspaceCreateModalComponent: React.FC<Props> = ({ onClose, workspaceId }
useCheckpointStorage,
watchBindings,
canSetResourceQuotas,
loadedResourceQuotaList,
]);

const handleSubmit = useCallback(async () => {
Expand Down Expand Up @@ -455,7 +443,7 @@ const WorkspaceCreateModalComponent: React.FC<Props> = ({ onClose, workspaceId }
}
}

if (workspaceId) {
if (isEditing) {
await patchWorkspace({ id: workspaceId, ...body });
workspaceStore.fetch(undefined, true);
resourceQuotaBody['id'] = workspaceId;
Expand All @@ -465,22 +453,12 @@ const WorkspaceCreateModalComponent: React.FC<Props> = ({ onClose, workspaceId }
resourceQuotaBody['id'] = response.id;
}

let submittedRQ = values.resourceQuotas;
if (loadedResourceQuotaList) {
submittedRQ = Object.keys(values.resourceQuotas ?? {}).reduce(
(memo: Record<string, number>, name) => {
if (values.resourceQuotas) {
if (values.resourceQuotas[name] !== loadedResourceQuotaList[name]) {
memo[name] = values.resourceQuotas[name];
}
}
return memo;
},
{},
);
}
if (submittedRQ && Object.keys(submittedRQ).length !== 0) {
resourceQuotaBody['clusterQuotaPairs'] = pick(submittedRQ, resourceManagers);
if (
canSetResourceQuotas &&
values.resourceQuotas &&
Object.keys(values.resourceQuotas).length !== 0
) {
resourceQuotaBody['clusterQuotaPairs'] = pick(values.resourceQuotas, resourceManagers);
await setResourceQuotas(resourceQuotaBody);
}
form.resetFields();
Expand All @@ -504,40 +482,38 @@ const WorkspaceCreateModalComponent: React.FC<Props> = ({ onClose, workspaceId }
});
}
}
}, [form, canModifyAUG, canModifyCPS, workspaceId, loadedResourceQuotaList, resourceManagers]);
}, [
form,
canModifyAUG,
canModifyCPS,
canSetResourceQuotas,
isEditing,
workspaceId,
resourceManagers,
]);

if ((workspace && !canModifyWorkspace({ workspace })) ?? false) {
return (
<Modal
size="medium"
title="Workspace Config"
onClose={() => {
initFields(undefined);
onClose?.();
}}>
{modalContent}
</Modal>
);
} else {
return (
<Modal
cancel
size="medium"
submit={{
form: idPrefix + FORM_ID,
handleError,
handler: handleSubmit,
text: 'Save Workspace',
}}
title={`${workspaceId ? 'Edit' : 'New'} Workspace`}
onClose={() => {
initFields(undefined);
onClose?.();
}}>
{modalContent}
</Modal>
);
}
return (
<Modal
cancel
size="medium"
submit={
isViewing
? undefined
: {
form: idPrefix + FORM_ID,
handleError,
handler: handleSubmit,
text: 'Save Workspace',
}
}
title={isViewing ? 'Workspace Config' : `${isEditing ? 'Edit' : 'New'} Workspace`}
onClose={() => {
initFields(undefined);
onClose?.();
}}>
{modalContent}
</Modal>
);
};

export default WorkspaceCreateModalComponent;

0 comments on commit 4b0d1cd

Please sign in to comment.