Browse Source

Extract shared IoT Hub components code to iot-hub-components.models.ts

- New iot-hub-components.models.ts with shared ITEM_TYPE_TO_ENTITY_TYPE
  mapping and resolveEntityDetailsUrl function
- Remove duplicated ITEM_TYPE_TO_ENTITY_TYPE from install and update dialogs
- Remove duplicated resolveEntityDetailsUrl from install and update dialogs
- Both dialogs now use shared function from models file
pull/15508/head
Igor Kulikov 3 months ago
parent
commit
832b45967d
  1. 48
      ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-components.models.ts
  2. 32
      ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-install-dialog.component.ts
  3. 33
      ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-update-dialog.component.ts

48
ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-components.models.ts

@ -0,0 +1,48 @@
///
/// Copyright © 2016-2026 The Thingsboard Authors
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
import { EntityType } from '@shared/models/entity-type.models';
import { IotHubInstalledItemDescriptor } from '@shared/models/iot-hub/iot-hub-installed-item.models';
import { getEntityDetailsPageURL } from '@core/utils';
export const ITEM_TYPE_TO_ENTITY_TYPE: Record<string, EntityType> = {
'WIDGET': EntityType.WIDGET_TYPE,
'DASHBOARD': EntityType.DASHBOARD,
'CALCULATED_FIELD': EntityType.CALCULATED_FIELD,
'RULE_CHAIN': EntityType.RULE_CHAIN,
'DEVICE': EntityType.DEVICE_PROFILE
};
export function resolveEntityDetailsUrl(descriptor: IotHubInstalledItemDescriptor, itemType: string): string | null {
if (!descriptor) {
return null;
}
const entityType = ITEM_TYPE_TO_ENTITY_TYPE[itemType];
if (!entityType) {
return null;
}
let entityId: string | null = null;
switch (descriptor.type) {
case 'WIDGET': entityId = descriptor.widgetTypeId?.id; break;
case 'DASHBOARD': entityId = descriptor.dashboardId?.id; break;
case 'CALCULATED_FIELD': entityId = descriptor.calculatedFieldId?.id; break;
case 'RULE_CHAIN': entityId = descriptor.ruleChainId?.id; break;
}
if (!entityId) {
return null;
}
return getEntityDetailsPageURL(entityId, entityType) || null;
}

32
ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-install-dialog.component.ts

@ -30,7 +30,7 @@ import { IotHubApiService } from '@core/http/iot-hub-api.service';
import { TranslateService } from '@ngx-translate/core';
import { EntityType } from '@shared/models/entity-type.models';
import { EntityId } from '@shared/models/id/entity-id';
import { getEntityDetailsPageURL } from '@core/utils';
import { resolveEntityDetailsUrl } from './iot-hub-components.models';
import { SolutionInstallDialogComponent } from '@home/components/solution/solution-install-dialog.component';
export interface IotHubInstallDialogData {
@ -47,14 +47,6 @@ export type InstallState = 'select-entity' | 'confirm' | 'installing' | 'success
})
export class TbIotHubInstallDialogComponent extends DialogComponent<TbIotHubInstallDialogComponent> {
private static readonly ITEM_TYPE_TO_ENTITY_TYPE: Record<string, EntityType> = {
'WIDGET': EntityType.WIDGET_TYPE,
'DASHBOARD': EntityType.DASHBOARD,
'CALCULATED_FIELD': EntityType.CALCULATED_FIELD,
'RULE_CHAIN': EntityType.RULE_CHAIN,
'DEVICE': EntityType.DEVICE_PROFILE
};
item: MpItemVersionView;
typeTranslations = itemTypeTranslations;
state: InstallState = 'confirm';
@ -107,7 +99,7 @@ export class TbIotHubInstallDialogComponent extends DialogComponent<TbIotHubInst
});
} else {
this.state = 'success';
this.entityDetailsUrl = this.resolveEntityDetailsUrl(result.descriptor);
this.entityDetailsUrl = resolveEntityDetailsUrl(result.descriptor, this.item.type);
}
} else {
this.state = 'error';
@ -136,24 +128,4 @@ export class TbIotHubInstallDialogComponent extends DialogComponent<TbIotHubInst
this.dialogRef.close(false);
}
private resolveEntityDetailsUrl(descriptor: IotHubInstalledItemDescriptor): string | null {
if (!descriptor) {
return null;
}
const entityType = TbIotHubInstallDialogComponent.ITEM_TYPE_TO_ENTITY_TYPE[this.item.type];
if (!entityType) {
return null;
}
let entityId: string | null = null;
switch (descriptor.type) {
case 'WIDGET': entityId = descriptor.widgetTypeId?.id; break;
case 'DASHBOARD': entityId = descriptor.dashboardId?.id; break;
case 'CALCULATED_FIELD': entityId = descriptor.calculatedFieldId?.id; break;
case 'RULE_CHAIN': entityId = descriptor.ruleChainId?.id; break;
}
if (!entityId) {
return null;
}
return getEntityDetailsPageURL(entityId, entityType) || null;
}
}

33
ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-update-dialog.component.ts

@ -28,8 +28,7 @@ import {
import { IotHubApiService } from '@core/http/iot-hub-api.service';
import { DialogService } from '@core/services/dialog.service';
import { TranslateService } from '@ngx-translate/core';
import { EntityType } from '@shared/models/entity-type.models';
import { getEntityDetailsPageURL } from '@core/utils';
import { resolveEntityDetailsUrl } from './iot-hub-components.models';
import { SolutionInstallDialogComponent } from '@home/components/solution/solution-install-dialog.component';
export interface IotHubUpdateDialogData {
@ -50,14 +49,6 @@ export type UpdateState = 'confirm' | 'updating' | 'success' | 'error';
})
export class TbIotHubUpdateDialogComponent extends DialogComponent<TbIotHubUpdateDialogComponent> {
private static readonly ITEM_TYPE_TO_ENTITY_TYPE: Record<string, EntityType> = {
'WIDGET': EntityType.WIDGET_TYPE,
'DASHBOARD': EntityType.DASHBOARD,
'CALCULATED_FIELD': EntityType.CALCULATED_FIELD,
'RULE_CHAIN': EntityType.RULE_CHAIN,
'DEVICE': EntityType.DEVICE_PROFILE
};
typeTranslations = itemTypeTranslations;
state: UpdateState = 'confirm';
errorMessage = '';
@ -95,7 +86,7 @@ export class TbIotHubUpdateDialogComponent extends DialogComponent<TbIotHubUpdat
});
} else {
this.state = 'success';
this.entityDetailsUrl = this.resolveEntityDetailsUrl(result.descriptor);
this.entityDetailsUrl = resolveEntityDetailsUrl(result.descriptor, this.data.itemType);
}
} else if (result.entityModified) {
this.state = 'confirm';
@ -136,24 +127,4 @@ export class TbIotHubUpdateDialogComponent extends DialogComponent<TbIotHubUpdat
this.dialogRef.close(false);
}
private resolveEntityDetailsUrl(descriptor: IotHubInstalledItemDescriptor): string | null {
if (!descriptor) {
return null;
}
const entityType = TbIotHubUpdateDialogComponent.ITEM_TYPE_TO_ENTITY_TYPE[this.data.itemType];
if (!entityType) {
return null;
}
let entityId: string | null = null;
switch (descriptor.type) {
case 'WIDGET': entityId = descriptor.widgetTypeId?.id; break;
case 'DASHBOARD': entityId = descriptor.dashboardId?.id; break;
case 'CALCULATED_FIELD': entityId = descriptor.calculatedFieldId?.id; break;
case 'RULE_CHAIN': entityId = descriptor.ruleChainId?.id; break;
}
if (!entityId) {
return null;
}
return getEntityDetailsPageURL(entityId, entityType) || null;
}
}

Loading…
Cancel
Save