1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Revert "Try extracting dependencies to separate files"

This reverts commit 8af0e748

Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com>
Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen 2021-11-30 15:24:36 +02:00
parent 5b21c99b9d
commit 724a571eb0
14 changed files with 53 additions and 269 deletions

View File

@ -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 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 { apiManager } from "../../../../common/k8s-api/api-manager";
import type { Injectable } from "@ogre-tools/injectable";
import { lifecycleEnum } from "@ogre-tools/injectable";

View File

@ -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";
import { lifecycleEnum } from "@ogre-tools/injectable";

View File

@ -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 "./kube-object-menu-registry.injectable";
import kubeObjectMenuRegistryInjectable from "../kubeObjectMenuRegistry.injectable";
import {
InstantiationParameter,

View File

@ -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";
import { lifecycleEnum } from "@ogre-tools/injectable";

View File

@ -1,51 +0,0 @@
/**
* 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;

View File

@ -1,43 +0,0 @@
/**
* 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;

View File

@ -1,46 +0,0 @@
/**
* 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) => void;
}
export const removeAction =
(
{ apiManagerStore, hideDetails }: Dependencies,
{ customAction, kubeObject }: InstantiationParameter,
) =>
async () => {
hideDetails();
const action = customAction || apiManagerStore.remove;
await action(kubeObject);
};

View File

@ -1,49 +0,0 @@
/**
* 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;

View File

@ -1,45 +0,0 @@
/**
* 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);
};

View File

@ -28,27 +28,22 @@ import {
import type { KubeObject } from "../../../common/k8s-api/kube-object";
import { lifecycleEnum, Injectable } from "@ogre-tools/injectable";
import clusterNameInjectable from "./dependencies/cluster-name.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 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,
KubeObjectMenuDependencies<KubeObject>,
KubeObjectMenuProps<KubeObject>
> = {
getDependencies: (di, props) => ({
clusterName: di.inject(clusterNameInjectable),
removeObject: di.inject(removeActionInjectable, {
customAction: props.removeAction,
kubeObject: props.object,
}),
updateObject: di.inject(updateActionInjectable, {
kubeObject: props.object,
}),
apiManager: di.inject(apiManagerInjectable),
editResourceTab: di.inject(editResourceTabInjectable),
hideDetails: di.inject(hideDetailsInjectable),
kubeObjectMenuItems: di.inject(kubeObjectMenuItemsInjectable, {
kubeObject: props.object,

View File

@ -32,14 +32,14 @@ import asyncFn, { AsyncFnMock } from "@async-fn/jest";
import { getDiForUnitTesting } from "../getDiForUnitTesting";
import clusterInjectable from "./dependencies/cluster.injectable";
import hideDetailsInjectable from "./dependencies/hide-details.injectable";
import editResourceTabInjectable from "./dependencies/update-action/edit-resource-tab.injectable";
import hideDetailsInjectable from "./dependencies/hideDetails.injectable";
import editResourceTabInjectable from "./dependencies/editResourceTab.injectable";
import { TabKind } from "../dock/dock.store";
import kubeObjectMenuRegistryInjectable from "./dependencies/kube-object-menu-items/kube-object-menu-registry.injectable";
import kubeObjectMenuRegistryInjectable from "./dependencies/kubeObjectMenuRegistry.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/remove-action/api-manager-store/api-manager.injectable";
import apiManagerInjectable from "./dependencies/apiManager.injectable";
import { KubeObjectMenu } from "./index";
describe("kube-object-menu", () => {

View File

@ -24,13 +24,14 @@ 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 {
export interface KubeObjectMenuDependencies<TKubeObject> {
apiManager: ApiManager;
kubeObjectMenuItems: React.ElementType[];
clusterName: string;
removeObject: () => Promise<void>
updateObject: () => void
hideDetails: () => void;
editResourceTab: (kubeObject: TKubeObject) => void;
}
export interface KubeObjectMenuProps<TKubeObject> extends MenuActionsProps {
@ -41,17 +42,41 @@ export interface KubeObjectMenuProps<TKubeObject> extends MenuActionsProps {
export interface KubeObjectMenuPropsAndDependencies<TKubeObject>
extends KubeObjectMenuProps<TKubeObject>,
KubeObjectMenuDependencies {}
KubeObjectMenuDependencies<TKubeObject> {}
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);
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);
}
@boundMethod
@ -87,16 +112,14 @@ export class KubeObjectMenu<
}
render() {
const { getRemoveMessage, isEditable, isRemovable } = this;
const { className, editable, updateObject, removable, removeObject, ...menuProps } = this.props;
const { remove, update, getRemoveMessage, isEditable, isRemovable } = this;
const { className, editable, removable, ...menuProps } = this.props;
return (
<MenuActions
className={cssNames("KubeObjectMenu", className)}
{...(isEditable ? { updateAction: updateObject } : {})}
{...(isRemovable ? { removeAction: removeObject } : {})}
updateAction={isEditable ? update : undefined}
removeAction={isRemovable ? remove : undefined}
removeConfirmationMessage={getRemoveMessage}
{...menuProps}
>