mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Try extracting dependencies so separate files
Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com> Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
parent
1e257c42cf
commit
9cef2f27aa
@ -19,7 +19,7 @@
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
import { Injectable, lifecycleEnum } from "@ogre-tools/injectable";
|
||||
import kubeObjectMenuRegistryInjectable from "../kubeObjectMenuRegistry.injectable";
|
||||
import kubeObjectMenuRegistryInjectable from "./kube-object-menu-registry.injectable";
|
||||
|
||||
import {
|
||||
InstantiationParameter,
|
||||
|
||||
@ -18,7 +18,7 @@
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
import { KubeObjectMenuRegistry } from "../../../../extensions/registries";
|
||||
import { KubeObjectMenuRegistry } from "../../../../../extensions/registries";
|
||||
import type { Injectable } from "@ogre-tools/injectable";
|
||||
|
||||
const kubeObjectMenuRegistryInjectable: Injectable<KubeObjectMenuRegistry> = {
|
||||
@ -0,0 +1,51 @@
|
||||
/**
|
||||
* Copyright (c) 2021 OpenLens Authors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
import type { Injectable } from "@ogre-tools/injectable";
|
||||
import { lifecycleEnum } from "@ogre-tools/injectable";
|
||||
import apiManagerInjectable from "./api-manager.injectable";
|
||||
import type { ApiManager } from "../../../../../../common/k8s-api/api-manager";
|
||||
import type { KubeObject } from "../../../../../../common/k8s-api/kube-object";
|
||||
import type { KubeObjectStore } from "../../../../../../common/k8s-api/kube-object.store";
|
||||
|
||||
interface Dependencies {
|
||||
apiManager: ApiManager;
|
||||
}
|
||||
|
||||
interface InstantiationParameter {
|
||||
kubeObject: KubeObject | null;
|
||||
}
|
||||
|
||||
const apiManagerStoreInjectable: Injectable<
|
||||
KubeObjectStore<KubeObject> | null,
|
||||
Dependencies,
|
||||
InstantiationParameter
|
||||
> = {
|
||||
getDependencies: di => ({
|
||||
apiManager: di.inject(apiManagerInjectable),
|
||||
}),
|
||||
|
||||
instantiate: ({ apiManager }, { kubeObject }) =>
|
||||
kubeObject ? apiManager.getStore(kubeObject.selfLink) : null,
|
||||
|
||||
lifecycle: lifecycleEnum.transient,
|
||||
};
|
||||
|
||||
export default apiManagerStoreInjectable;
|
||||
@ -18,8 +18,8 @@
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
import { apiManager } from "../../../../common/k8s-api/api-manager";
|
||||
import type { ApiManager } from "../../../../common/k8s-api/api-manager";
|
||||
import { apiManager } from "../../../../../../common/k8s-api/api-manager";
|
||||
import type { ApiManager } from "../../../../../../common/k8s-api/api-manager";
|
||||
import type { Injectable } from "@ogre-tools/injectable";
|
||||
|
||||
const apiManagerInjectable: Injectable<ApiManager> = {
|
||||
@ -0,0 +1,43 @@
|
||||
/**
|
||||
* Copyright (c) 2021 OpenLens Authors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
import type { Injectable } from "@ogre-tools/injectable";
|
||||
import { lifecycleEnum } from "@ogre-tools/injectable";
|
||||
import apiManagerStoreInjectable from "./api-manager-store/api-manager-store.injectable";
|
||||
import type { InstantiationParameter } from "./remove-action";
|
||||
import type { Dependencies } from "./remove-action";
|
||||
import { removeAction } from "./remove-action";
|
||||
import hideDetailsInjectable from "../hide-details.injectable";
|
||||
|
||||
const removeActionInjectable: Injectable<
|
||||
ReturnType<typeof removeAction>,
|
||||
Dependencies,
|
||||
InstantiationParameter
|
||||
> = {
|
||||
getDependencies: (di, { kubeObject }) => ({
|
||||
apiManagerStore: di.inject(apiManagerStoreInjectable, { kubeObject }),
|
||||
hideDetails: di.inject(hideDetailsInjectable),
|
||||
}),
|
||||
|
||||
instantiate: removeAction,
|
||||
lifecycle: lifecycleEnum.transient,
|
||||
};
|
||||
|
||||
export default removeActionInjectable;
|
||||
@ -0,0 +1,46 @@
|
||||
/**
|
||||
* Copyright (c) 2021 OpenLens Authors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
import type { KubeObjectStore } from "../../../../../common/k8s-api/kube-object.store";
|
||||
import type { KubeObject } from "../../../../../common/k8s-api/kube-object";
|
||||
import type { hideDetails as hideDetailsType } from "../../../kube-detail-params";
|
||||
|
||||
export interface Dependencies {
|
||||
apiManagerStore: KubeObjectStore<KubeObject> | null;
|
||||
hideDetails: typeof hideDetailsType;
|
||||
}
|
||||
|
||||
export interface InstantiationParameter {
|
||||
kubeObject?: KubeObject;
|
||||
customAction?: (kubeObject: KubeObject) => Promise<void>;
|
||||
}
|
||||
|
||||
export const removeAction =
|
||||
(
|
||||
{ apiManagerStore, hideDetails }: Dependencies,
|
||||
{ customAction, kubeObject }: InstantiationParameter,
|
||||
) =>
|
||||
async () => {
|
||||
hideDetails();
|
||||
|
||||
const action = customAction || apiManagerStore.remove;
|
||||
|
||||
await action(kubeObject);
|
||||
};
|
||||
@ -18,7 +18,7 @@
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
import { editResourceTab } from "../../dock/edit-resource.store";
|
||||
import { editResourceTab } from "../../../dock/edit-resource.store";
|
||||
import type { Injectable } from "@ogre-tools/injectable";
|
||||
|
||||
const editResourceTabInjectable: Injectable<typeof editResourceTab> = {
|
||||
@ -0,0 +1,49 @@
|
||||
/**
|
||||
* Copyright (c) 2021 OpenLens Authors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
import type { Injectable } from "@ogre-tools/injectable";
|
||||
import { lifecycleEnum } from "@ogre-tools/injectable";
|
||||
import apiManagerStoreInjectable from "../remove-action/api-manager-store/api-manager-store.injectable";
|
||||
import hideDetailsInjectable from "../hide-details.injectable";
|
||||
import editResourceTabInjectable from "./edit-resource-tab.injectable";
|
||||
|
||||
import {
|
||||
Dependencies,
|
||||
InstantiationParameter,
|
||||
updateAction,
|
||||
} from "./update-action";
|
||||
|
||||
const updateActionInjectable: Injectable<
|
||||
ReturnType<typeof updateAction>,
|
||||
Dependencies,
|
||||
InstantiationParameter
|
||||
> = {
|
||||
getDependencies: (di, { kubeObject }) => ({
|
||||
apiManagerStore: di.inject(apiManagerStoreInjectable, { kubeObject }),
|
||||
hideDetails: di.inject(hideDetailsInjectable),
|
||||
editResourceTab: di.inject(editResourceTabInjectable),
|
||||
}),
|
||||
|
||||
instantiate: updateAction,
|
||||
|
||||
lifecycle: lifecycleEnum.transient,
|
||||
};
|
||||
|
||||
export default updateActionInjectable;
|
||||
@ -0,0 +1,45 @@
|
||||
/**
|
||||
* Copyright (c) 2021 OpenLens Authors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
import type { KubeObjectStore } from "../../../../../common/k8s-api/kube-object.store";
|
||||
import type { KubeObject } from "../../../../../common/k8s-api/kube-object";
|
||||
import type { hideDetails as hideDetailsType } from "../../../kube-detail-params";
|
||||
import type { editResourceTab } from "../../../dock/edit-resource.store";
|
||||
|
||||
export interface Dependencies {
|
||||
apiManagerStore: KubeObjectStore<KubeObject> | null;
|
||||
hideDetails: typeof hideDetailsType;
|
||||
editResourceTab: typeof editResourceTab
|
||||
}
|
||||
|
||||
export interface InstantiationParameter {
|
||||
kubeObject?: KubeObject;
|
||||
}
|
||||
|
||||
export const updateAction =
|
||||
(
|
||||
{ editResourceTab, hideDetails }: Dependencies,
|
||||
{ kubeObject }: InstantiationParameter,
|
||||
) =>
|
||||
() => {
|
||||
hideDetails();
|
||||
|
||||
editResourceTab(kubeObject);
|
||||
};
|
||||
@ -28,22 +28,27 @@ import {
|
||||
|
||||
import type { KubeObject } from "../../../common/k8s-api/kube-object";
|
||||
import { lifecycleEnum, Injectable } from "@ogre-tools/injectable";
|
||||
import apiManagerInjectable from "./dependencies/apiManager.injectable";
|
||||
import clusterNameInjectable from "./dependencies/clusterName.injectable";
|
||||
import editResourceTabInjectable from "./dependencies/editResourceTab.injectable";
|
||||
import hideDetailsInjectable from "./dependencies/hideDetails.injectable";
|
||||
import clusterNameInjectable from "./dependencies/cluster-name.injectable";
|
||||
import kubeObjectMenuItemsInjectable from "./dependencies/kube-object-menu-items/kube-object-menu-items.injectable";
|
||||
import removeActionInjectable from "./dependencies/remove-action/remove-action.injectable";
|
||||
import updateActionInjectable from "./dependencies/update-action/update-action.injectable";
|
||||
|
||||
const KubeObjectMenuInjectable: Injectable<
|
||||
JSX.Element,
|
||||
KubeObjectMenuDependencies<KubeObject>,
|
||||
KubeObjectMenuDependencies,
|
||||
KubeObjectMenuProps<KubeObject>
|
||||
> = {
|
||||
getDependencies: (di, props) => ({
|
||||
clusterName: di.inject(clusterNameInjectable),
|
||||
apiManager: di.inject(apiManagerInjectable),
|
||||
editResourceTab: di.inject(editResourceTabInjectable),
|
||||
hideDetails: di.inject(hideDetailsInjectable),
|
||||
|
||||
removeObject: di.inject(removeActionInjectable, {
|
||||
customAction: props.removeAction,
|
||||
kubeObject: props.object,
|
||||
}),
|
||||
|
||||
updateObject: di.inject(updateActionInjectable, {
|
||||
kubeObject: props.object,
|
||||
}),
|
||||
|
||||
kubeObjectMenuItems: di.inject(kubeObjectMenuItemsInjectable, {
|
||||
kubeObject: props.object,
|
||||
|
||||
@ -33,15 +33,15 @@ import { getDiForUnitTesting } from "../getDiForUnitTesting";
|
||||
|
||||
import { Inject } from "@ogre-tools/injectable-react";
|
||||
import clusterInjectable from "./dependencies/cluster.injectable";
|
||||
import hideDetailsInjectable from "./dependencies/hideDetails.injectable";
|
||||
import editResourceTabInjectable from "./dependencies/editResourceTab.injectable";
|
||||
import hideDetailsInjectable from "./dependencies/hide-details.injectable";
|
||||
import editResourceTabInjectable from "./dependencies/update-action/edit-resource-tab.injectable";
|
||||
import { TabKind } from "../dock/dock.store";
|
||||
import KubeObjectMenuInjectable from "./kube-object-menu.injectable";
|
||||
import kubeObjectMenuRegistryInjectable from "./dependencies/kubeObjectMenuRegistry.injectable";
|
||||
import kubeObjectMenuRegistryInjectable from "./dependencies/kube-object-menu-items/kube-object-menu-registry.injectable";
|
||||
import { renderFor, DiRender } from "../test-utils/renderFor";
|
||||
import type { Cluster } from "../../../main/cluster";
|
||||
import type { ApiManager } from "../../../common/k8s-api/api-manager";
|
||||
import apiManagerInjectable from "./dependencies/apiManager.injectable";
|
||||
import apiManagerInjectable from "./dependencies/remove-action/api-manager-store/api-manager.injectable";
|
||||
|
||||
describe("kube-object-menu", () => {
|
||||
let objectStub: KubeObject | null;
|
||||
|
||||
@ -24,14 +24,13 @@ import { boundMethod, cssNames } from "../../utils";
|
||||
import type { KubeObject } from "../../../common/k8s-api/kube-object";
|
||||
import { MenuActions, MenuActionsProps } from "../menu";
|
||||
import identity from "lodash/identity";
|
||||
import type { ApiManager } from "../../../common/k8s-api/api-manager";
|
||||
|
||||
export interface KubeObjectMenuDependencies<TKubeObject> {
|
||||
apiManager: ApiManager;
|
||||
export interface KubeObjectMenuDependencies {
|
||||
kubeObjectMenuItems: React.ElementType[];
|
||||
clusterName: string;
|
||||
hideDetails: () => void;
|
||||
editResourceTab: (kubeObject: TKubeObject) => void;
|
||||
|
||||
removeObject: () => Promise<void>
|
||||
updateObject: () => void
|
||||
}
|
||||
|
||||
export interface KubeObjectMenuProps<TKubeObject> extends MenuActionsProps {
|
||||
@ -42,41 +41,17 @@ export interface KubeObjectMenuProps<TKubeObject> extends MenuActionsProps {
|
||||
|
||||
export interface KubeObjectMenuPropsAndDependencies<TKubeObject>
|
||||
extends KubeObjectMenuProps<TKubeObject>,
|
||||
KubeObjectMenuDependencies<TKubeObject> {}
|
||||
KubeObjectMenuDependencies {}
|
||||
|
||||
export class KubeObjectMenu<
|
||||
TKubeObject extends KubeObject,
|
||||
> extends React.Component<KubeObjectMenuPropsAndDependencies<TKubeObject>> {
|
||||
get store() {
|
||||
const { object } = this.props;
|
||||
|
||||
if (!object) return null;
|
||||
|
||||
return this.props.apiManager.getStore(object.selfLink);
|
||||
}
|
||||
|
||||
get isEditable() {
|
||||
return this.props.editable ?? Boolean(this.store?.patch);
|
||||
return this.props.editable; // ?? Boolean(this.store?.patch);
|
||||
}
|
||||
|
||||
get isRemovable() {
|
||||
return this.props.removable ?? Boolean(this.store?.remove);
|
||||
}
|
||||
|
||||
@boundMethod
|
||||
async update() {
|
||||
this.props.hideDetails();
|
||||
this.props.editResourceTab(this.props.object);
|
||||
}
|
||||
|
||||
@boundMethod
|
||||
async remove() {
|
||||
this.props.hideDetails();
|
||||
const { object, removeAction } = this.props;
|
||||
|
||||
// TODO: currently only branch for removeAction() is unit tested, and store.remove() is not.
|
||||
if (removeAction) await removeAction();
|
||||
else await this.store.remove(object);
|
||||
return this.props.removable; // ?? Boolean(this.store?.remove);
|
||||
}
|
||||
|
||||
@boundMethod
|
||||
@ -112,14 +87,16 @@ export class KubeObjectMenu<
|
||||
}
|
||||
|
||||
render() {
|
||||
const { remove, update, getRemoveMessage, isEditable, isRemovable } = this;
|
||||
const { className, editable, removable, ...menuProps } = this.props;
|
||||
const { getRemoveMessage, isEditable, isRemovable } = this;
|
||||
const { className, editable, updateObject, removable, removeObject, ...menuProps } = this.props;
|
||||
|
||||
return (
|
||||
<MenuActions
|
||||
className={cssNames("KubeObjectMenu", className)}
|
||||
updateAction={isEditable ? update : undefined}
|
||||
removeAction={isRemovable ? remove : undefined}
|
||||
|
||||
{...(isEditable ? { updateAction: updateObject } : {})}
|
||||
{...(isRemovable ? { removeAction: removeObject } : {})}
|
||||
|
||||
removeConfirmationMessage={getRemoveMessage}
|
||||
{...menuProps}
|
||||
>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user