Commit ec84c9cf5159bbcca00beacb5488a6da3788a87f

Authored by Vladyslav_Prykhodko
1 parent d5c7f7fc

UI: Fixed json attribute widget - showed two errors at the same time.

@@ -22,7 +22,7 @@ @@ -22,7 +22,7 @@
22 class="tb-json-input__form" 22 class="tb-json-input__form"
23 [formGroup]="attributeUpdateFormGroup" 23 [formGroup]="attributeUpdateFormGroup"
24 (ngSubmit)="save()"> 24 (ngSubmit)="save()">
25 - <div fxLayout="column" fxLayoutGap="10px" fxFlex *ngIf="entityDetected && isValidParameter && dataKeyDetected"> 25 + <div fxLayout="column" fxLayoutGap="10px" fxFlex *ngIf="datasourceDetected && !errorMessage; else errorContainer">
26 <fieldset fxFlex> 26 <fieldset fxFlex>
27 <tb-json-object-edit 27 <tb-json-object-edit
28 [editorStyle]="{minHeight: '100px'}" 28 [editorStyle]="{minHeight: '100px'}"
@@ -51,19 +51,10 @@ @@ -51,19 +51,10 @@
51 </div> 51 </div>
52 </div> 52 </div>
53 53
54 - <div fxLayout="column" fxLayoutAlign="center center" fxFlex *ngIf="!entityDetected || !dataKeyDetected || !isValidParameter">  
55 - <div class="tb-json-input__error"  
56 - *ngIf="!entityDetected">  
57 - {{ 'widgets.input-widgets.no-entity-selected' | translate }}  
58 - </div>  
59 - <div class="tb-json-input__error"  
60 - *ngIf="entityDetected && !dataKeyDetected">  
61 - {{ 'widgets.input-widgets.no-datakey-selected' | translate }}  
62 - </div>  
63 - <div class="tb-json-input__error"  
64 - *ngIf="dataKeyDetected && !isValidParameter"> 54 + <ng-template #errorContainer>
  55 + <div class="tb-json-input__error" fxLayout="column" fxLayoutAlign="center center" fxFlex>
65 {{ errorMessage | translate }} 56 {{ errorMessage | translate }}
66 </div> 57 </div>
67 - </div> 58 + </ng-template>
68 </form> 59 </form>
69 </div> 60 </div>
@@ -25,7 +25,6 @@ @@ -25,7 +25,6 @@
25 } 25 }
26 26
27 &__error { 27 &__error {
28 - text-align: center;  
29 font-size: 18px; 28 font-size: 18px;
30 color: #a0a0a0; 29 color: #a0a0a0;
31 } 30 }
@@ -28,7 +28,7 @@ import { AttributeService } from '@core/http/attribute.service'; @@ -28,7 +28,7 @@ import { AttributeService } from '@core/http/attribute.service';
28 import { AttributeData, AttributeScope, DataKeyType, LatestTelemetry } from '@shared/models/telemetry/telemetry.models'; 28 import { AttributeData, AttributeScope, DataKeyType, LatestTelemetry } from '@shared/models/telemetry/telemetry.models';
29 import { EntityId } from '@shared/models/id/entity-id'; 29 import { EntityId } from '@shared/models/id/entity-id';
30 import { EntityType } from '@shared/models/entity-type.models'; 30 import { EntityType } from '@shared/models/entity-type.models';
31 -import { createLabelFromDatasource } from '@core/utils'; 31 +import { createLabelFromDatasource, isDefinedAndNotNull } from '@core/utils';
32 import { Observable } from 'rxjs'; 32 import { Observable } from 'rxjs';
33 33
34 enum JsonInputWidgetMode { 34 enum JsonInputWidgetMode {
@@ -63,9 +63,7 @@ export class JsonInputWidgetComponent extends PageComponent implements OnInit { @@ -63,9 +63,7 @@ export class JsonInputWidgetComponent extends PageComponent implements OnInit {
63 63
64 labelValue: string; 64 labelValue: string;
65 65
66 - entityDetected = false;  
67 - dataKeyDetected = false;  
68 - isValidParameter = false; 66 + datasourceDetected = false;
69 errorMessage: string; 67 errorMessage: string;
70 68
71 isFocused: boolean; 69 isFocused: boolean;
@@ -111,30 +109,26 @@ export class JsonInputWidgetComponent extends PageComponent implements OnInit { @@ -111,30 +109,26 @@ export class JsonInputWidgetComponent extends PageComponent implements OnInit {
111 } 109 }
112 110
113 private validateDatasources() { 111 private validateDatasources() {
114 - if (this.datasource?.type === DatasourceType.entity) {  
115 - this.entityDetected = true;  
116 - if (this.datasource.dataKeys.length) {  
117 - this.dataKeyDetected = true;  
118 -  
119 - if (this.settings.widgetMode === JsonInputWidgetMode.ATTRIBUTE) {  
120 - if (this.datasource.dataKeys[0].type === DataKeyType.attribute) {  
121 - if (this.settings.attributeScope === AttributeScope.SERVER_SCOPE || this.datasource.entityType === EntityType.DEVICE) {  
122 - this.isValidParameter = true;  
123 - } else {  
124 - this.errorMessage = 'widgets.input-widgets.not-allowed-entity';  
125 - }  
126 - } else {  
127 - this.errorMessage = 'widgets.input-widgets.no-attribute-selected'; 112 + this.datasourceDetected = isDefinedAndNotNull(this.datasource);
  113 + if (!this.datasourceDetected) {
  114 + return;
  115 + }
  116 + if (this.datasource.type === DatasourceType.entity) {
  117 + if (this.settings.widgetMode === JsonInputWidgetMode.ATTRIBUTE) {
  118 + if (this.datasource.dataKeys[0].type === DataKeyType.attribute) {
  119 + if (this.settings.attributeScope !== AttributeScope.SERVER_SCOPE && this.datasource.entityType !== EntityType.DEVICE) {
  120 + this.errorMessage = 'widgets.input-widgets.not-allowed-entity';
128 } 121 }
129 } else { 122 } else {
130 - if (this.datasource.dataKeys[0].type === DataKeyType.timeseries) {  
131 - this.isValidParameter = true;  
132 - } else {  
133 - this.errorMessage = 'widgets.input-widgets.no-timeseries-selected';  
134 - } 123 + this.errorMessage = 'widgets.input-widgets.no-attribute-selected';
  124 + }
  125 + } else {
  126 + if (this.datasource.dataKeys[0].type !== DataKeyType.timeseries) {
  127 + this.errorMessage = 'widgets.input-widgets.no-timeseries-selected';
135 } 128 }
136 -  
137 } 129 }
  130 + } else {
  131 + this.errorMessage = 'widgets.input-widgets.no-entity-selected';
138 } 132 }
139 } 133 }
140 134
@@ -152,7 +146,7 @@ export class JsonInputWidgetComponent extends PageComponent implements OnInit { @@ -152,7 +146,7 @@ export class JsonInputWidgetComponent extends PageComponent implements OnInit {
152 } 146 }
153 147
154 private updateWidgetData(data: Array<DatasourceData>) { 148 private updateWidgetData(data: Array<DatasourceData>) {
155 - if (this.isValidParameter) { 149 + if (!this.errorMessage) {
156 let value = {}; 150 let value = {};
157 if (data[0].data[0][1] !== '') { 151 if (data[0].data[0][1] !== '') {
158 try { 152 try {