From 5849e093f7f7ad1f67886cd2391892dcee1e0cf4 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Thu, 22 Dec 2022 10:45:02 -0500 Subject: [PATCH] Remove usages of legacy global resourceQuotaApi Signed-off-by: Sebastian Malton --- .../k8s-api/endpoints/legacy-globals.ts | 6 - .../add-dialog/close.injectable.ts | 18 +++ .../add-dialog/is-open.injectable.ts | 18 +++ .../add-dialog/open-state.injectable.ts | 13 ++ .../add-dialog/open.injectable.ts | 18 +++ .../view.scss} | 0 .../view.tsx} | 114 ++++++++++-------- .../resource-quotas.tsx | 7 +- 8 files changed, 133 insertions(+), 61 deletions(-) create mode 100644 src/renderer/components/+config-resource-quotas/add-dialog/close.injectable.ts create mode 100644 src/renderer/components/+config-resource-quotas/add-dialog/is-open.injectable.ts create mode 100644 src/renderer/components/+config-resource-quotas/add-dialog/open-state.injectable.ts create mode 100644 src/renderer/components/+config-resource-quotas/add-dialog/open.injectable.ts rename src/renderer/components/+config-resource-quotas/{add-quota-dialog.scss => add-dialog/view.scss} (100%) rename src/renderer/components/+config-resource-quotas/{add-quota-dialog.tsx => add-dialog/view.tsx} (67%) diff --git a/src/common/k8s-api/endpoints/legacy-globals.ts b/src/common/k8s-api/endpoints/legacy-globals.ts index ebc17d1099..a008316a96 100644 --- a/src/common/k8s-api/endpoints/legacy-globals.ts +++ b/src/common/k8s-api/endpoints/legacy-globals.ts @@ -11,7 +11,6 @@ import networkPolicyApiInjectable from "./network-policy.api.injectable"; import nodeApiInjectable from "./node.api.injectable"; import persistentVolumeClaimApiInjectable from "./persistent-volume-claim.api.injectable"; import podApiInjectable from "./pod.api.injectable"; -import resourceQuotaApiInjectable from "./resource-quota.api.injectable"; import roleApiInjectable from "./role.api.injectable"; /** @@ -53,8 +52,3 @@ export const nodeApi = asLegacyGlobalForExtensionApi(nodeApiInjectable); * @deprecated use `di.inject(persistentVolumeClaimApiInjectable)` instead */ export const persistentVolumeClaimApi = asLegacyGlobalForExtensionApi(persistentVolumeClaimApiInjectable); - -/** - * @deprecated use `di.inject(resourceQuotaApiInjectable)` instead - */ -export const resourceQuotaApi = asLegacyGlobalForExtensionApi(resourceQuotaApiInjectable); diff --git a/src/renderer/components/+config-resource-quotas/add-dialog/close.injectable.ts b/src/renderer/components/+config-resource-quotas/add-dialog/close.injectable.ts new file mode 100644 index 0000000000..76af08fc88 --- /dev/null +++ b/src/renderer/components/+config-resource-quotas/add-dialog/close.injectable.ts @@ -0,0 +1,18 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import { getInjectable } from "@ogre-tools/injectable"; +import { action } from "mobx"; +import addQuotaDialogOpenStateInjectable from "./open-state.injectable"; + +const closeAddQuotaDialogInjectable = getInjectable({ + id: "close-add-quota-dialog", + instantiate: (di) => { + const state = di.inject(addQuotaDialogOpenStateInjectable); + + return action(() => state.set(false)); + }, +}); + +export default closeAddQuotaDialogInjectable; diff --git a/src/renderer/components/+config-resource-quotas/add-dialog/is-open.injectable.ts b/src/renderer/components/+config-resource-quotas/add-dialog/is-open.injectable.ts new file mode 100644 index 0000000000..3f91737e72 --- /dev/null +++ b/src/renderer/components/+config-resource-quotas/add-dialog/is-open.injectable.ts @@ -0,0 +1,18 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import { getInjectable } from "@ogre-tools/injectable"; +import { computed } from "mobx"; +import addQuotaDialogOpenStateInjectable from "./open-state.injectable"; + +const isAddQuotaDialogOpenInjectable = getInjectable({ + id: "is-add-quota-dialog-open", + instantiate: (di) => { + const state = di.inject(addQuotaDialogOpenStateInjectable); + + return computed(() => state.get()); + }, +}); + +export default isAddQuotaDialogOpenInjectable; diff --git a/src/renderer/components/+config-resource-quotas/add-dialog/open-state.injectable.ts b/src/renderer/components/+config-resource-quotas/add-dialog/open-state.injectable.ts new file mode 100644 index 0000000000..11af7e8d38 --- /dev/null +++ b/src/renderer/components/+config-resource-quotas/add-dialog/open-state.injectable.ts @@ -0,0 +1,13 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import { getInjectable } from "@ogre-tools/injectable"; +import { observable } from "mobx"; + +const addQuotaDialogOpenStateInjectable = getInjectable({ + id: "add-quota-dialog-open-state", + instantiate: () => observable.box(false), +}); + +export default addQuotaDialogOpenStateInjectable; diff --git a/src/renderer/components/+config-resource-quotas/add-dialog/open.injectable.ts b/src/renderer/components/+config-resource-quotas/add-dialog/open.injectable.ts new file mode 100644 index 0000000000..243f6b0fd9 --- /dev/null +++ b/src/renderer/components/+config-resource-quotas/add-dialog/open.injectable.ts @@ -0,0 +1,18 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import { getInjectable } from "@ogre-tools/injectable"; +import { action } from "mobx"; +import addQuotaDialogOpenStateInjectable from "./open-state.injectable"; + +const openAddQuotaDialogInjectable = getInjectable({ + id: "open-add-quota-dialog", + instantiate: (di) => { + const state = di.inject(addQuotaDialogOpenStateInjectable); + + return action(() => state.set(true)); + }, +}); + +export default openAddQuotaDialogInjectable; diff --git a/src/renderer/components/+config-resource-quotas/add-quota-dialog.scss b/src/renderer/components/+config-resource-quotas/add-dialog/view.scss similarity index 100% rename from src/renderer/components/+config-resource-quotas/add-quota-dialog.scss rename to src/renderer/components/+config-resource-quotas/add-dialog/view.scss diff --git a/src/renderer/components/+config-resource-quotas/add-quota-dialog.tsx b/src/renderer/components/+config-resource-quotas/add-dialog/view.tsx similarity index 67% rename from src/renderer/components/+config-resource-quotas/add-quota-dialog.tsx rename to src/renderer/components/+config-resource-quotas/add-dialog/view.tsx index 70e0dcba1f..b0197e9c1b 100644 --- a/src/renderer/components/+config-resource-quotas/add-quota-dialog.tsx +++ b/src/renderer/components/+config-resource-quotas/add-dialog/view.tsx @@ -3,54 +3,61 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ -import "./add-quota-dialog.scss"; +import "./view.scss"; import React from "react"; +import type { IComputedValue } from "mobx"; import { computed, observable, makeObservable } from "mobx"; import { observer } from "mobx-react"; -import type { DialogProps } from "../dialog"; -import { Dialog } from "../dialog"; -import { Wizard, WizardStep } from "../wizard"; -import { Input } from "../input"; -import { systemName } from "../input/input_validators"; -import type { IResourceQuotaValues } from "../../../common/k8s-api/endpoints"; -import { resourceQuotaApi } from "../../../common/k8s-api/endpoints"; -import { Select } from "../select"; -import { Icon } from "../icon"; -import { Button } from "../button"; -import { Notifications } from "../notifications"; -import { NamespaceSelect } from "../+namespaces/namespace-select"; -import { SubTitle } from "../layout/sub-title"; +import type { DialogProps } from "../../dialog"; +import { Dialog } from "../../dialog"; +import { Wizard, WizardStep } from "../../wizard"; +import { Input } from "../../input"; +import { systemName } from "../../input/input_validators"; +import type { IResourceQuotaValues, ResourceQuotaApi } from "../../../../common/k8s-api/endpoints"; +import { Select } from "../../select"; +import { Icon } from "../../icon"; +import { Button } from "../../button"; +import { Notifications } from "../../notifications"; +import { NamespaceSelect } from "../../+namespaces/namespace-select"; +import { SubTitle } from "../../layout/sub-title"; +import { withInjectables } from "@ogre-tools/injectable-react"; +import closeAddQuotaDialogInjectable from "./close.injectable"; +import isAddQuotaDialogOpenInjectable from "./is-open.injectable"; +import resourceQuotaApiInjectable from "../../../../common/k8s-api/endpoints/resource-quota.api.injectable"; export interface AddQuotaDialogProps extends DialogProps { } -const dialogState = observable.object({ - isOpen: false, -}); +interface Dependencies { + resourceQuotaApi: ResourceQuotaApi; + isAddQuotaDialogOpen: IComputedValue; + closeAddQuotaDialog: () => void; +} + +const defaultQuotas = JSON.stringify({ + "limits.cpu": "", + "limits.memory": "", + "requests.cpu": "", + "requests.memory": "", + "requests.storage": "", + "persistentvolumeclaims": "", + "count/pods": "", + "count/persistentvolumeclaims": "", + "count/services": "", + "count/secrets": "", + "count/configmaps": "", + "count/replicationcontrollers": "", + "count/deployments.apps": "", + "count/replicasets.apps": "", + "count/statefulsets.apps": "", + "count/jobs.batch": "", + "count/cronjobs.batch": "", + "count/deployments.extensions": "", +} as IResourceQuotaValues); @observer -export class AddQuotaDialog extends React.Component { - static defaultQuotas: IResourceQuotaValues = { - "limits.cpu": "", - "limits.memory": "", - "requests.cpu": "", - "requests.memory": "", - "requests.storage": "", - "persistentvolumeclaims": "", - "count/pods": "", - "count/persistentvolumeclaims": "", - "count/services": "", - "count/secrets": "", - "count/configmaps": "", - "count/replicationcontrollers": "", - "count/deployments.apps": "", - "count/replicasets.apps": "", - "count/statefulsets.apps": "", - "count/jobs.batch": "", - "count/cronjobs.batch": "", - "count/deployments.extensions": "", - }; +class NonInjectedAddQuotaDialog extends React.Component { public defaultNamespace = "default"; @@ -58,21 +65,13 @@ export class AddQuotaDialog extends React.Component { @observable quotaSelectValue: string | null = null; @observable quotaInputValue = ""; @observable namespace: string | null = this.defaultNamespace; - @observable quotas = AddQuotaDialog.defaultQuotas; + @observable quotas: IResourceQuotaValues = JSON.parse(defaultQuotas); - constructor(props: AddQuotaDialogProps) { + constructor(props: AddQuotaDialogProps & Dependencies) { super(props); makeObservable(this); } - static open() { - dialogState.isOpen = true; - } - - static close() { - dialogState.isOpen = false; - } - @computed get quotaEntries() { return Object.entries(this.quotas) .filter(([, value]) => !!value?.trim()); @@ -101,7 +100,7 @@ export class AddQuotaDialog extends React.Component { }; close = () => { - AddQuotaDialog.close(); + this.props.closeAddQuotaDialog(); }; reset = () => { @@ -109,7 +108,7 @@ export class AddQuotaDialog extends React.Component { this.quotaSelectValue = ""; this.quotaInputValue = ""; this.namespace = this.defaultNamespace; - this.quotas = AddQuotaDialog.defaultQuotas; + this.quotas = JSON.parse(defaultQuotas); }; addQuota = async () => { @@ -122,7 +121,7 @@ export class AddQuotaDialog extends React.Component { try { const quotas = Object.fromEntries(this.quotaEntries); - await resourceQuotaApi.create({ namespace, name: quotaName }, { + await this.props.resourceQuotaApi.create({ namespace, name: quotaName }, { spec: { hard: quotas, }, @@ -143,14 +142,14 @@ export class AddQuotaDialog extends React.Component { }; render() { - const { ...dialogProps } = this.props; + const { closeAddQuotaDialog, isAddQuotaDialogOpen, resourceQuotaApi, ...dialogProps } = this.props; const header =
Create ResourceQuota
; return ( @@ -244,3 +243,12 @@ export class AddQuotaDialog extends React.Component { ); } } + +export const AddQuotaDialog = withInjectables(NonInjectedAddQuotaDialog, { + getProps: (di, props) => ({ + ...props, + closeAddQuotaDialog: di.inject(closeAddQuotaDialogInjectable), + isAddQuotaDialogOpen: di.inject(isAddQuotaDialogOpenInjectable), + resourceQuotaApi: di.inject(resourceQuotaApiInjectable), + }), +}); diff --git a/src/renderer/components/+config-resource-quotas/resource-quotas.tsx b/src/renderer/components/+config-resource-quotas/resource-quotas.tsx index 06cba945ad..28c3355b43 100644 --- a/src/renderer/components/+config-resource-quotas/resource-quotas.tsx +++ b/src/renderer/components/+config-resource-quotas/resource-quotas.tsx @@ -8,7 +8,7 @@ import "./resource-quotas.scss"; import React from "react"; import { observer } from "mobx-react"; import { KubeObjectListLayout } from "../kube-object-list-layout"; -import { AddQuotaDialog } from "./add-quota-dialog"; +import { AddQuotaDialog } from "./add-dialog/view"; import { KubeObjectStatusIcon } from "../kube-object-status-icon"; import { SiblingsInTabLayout } from "../layout/siblings-in-tab-layout"; import { KubeObjectAge } from "../kube-object/age"; @@ -18,6 +18,7 @@ import type { FilterByNamespace } from "../+namespaces/namespace-select-filter-m import { withInjectables } from "@ogre-tools/injectable-react"; import filterByNamespaceInjectable from "../+namespaces/namespace-select-filter-model/filter-by-namespace.injectable"; import resourceQuotaStoreInjectable from "./store.injectable"; +import openAddQuotaDialogInjectable from "./add-dialog/open.injectable"; enum columnId { name = "name", @@ -28,6 +29,7 @@ enum columnId { interface Dependencies { resourceQuotaStore: ResourceQuotaStore; filterByNamespace: FilterByNamespace; + openAddQuotaDialog: () => void; } @observer @@ -69,7 +71,7 @@ class NonInjectedResourceQuotas extends React.Component { , ]} addRemoveButtons={{ - onAdd: () => AddQuotaDialog.open(), + onAdd: this.props.openAddQuotaDialog, addTooltip: "Create new ResourceQuota", }} /> @@ -84,5 +86,6 @@ export const ResourceQuotas = withInjectables(NonInjectedResourceQ ...props, filterByNamespace: di.inject(filterByNamespaceInjectable), resourceQuotaStore: di.inject(resourceQuotaStoreInjectable), + openAddQuotaDialog: di.inject(openAddQuotaDialogInjectable), }), });