thingsboard/ui-ngx/src/app/modules/home/components/profile/queue/tenant-profile-queues.component.ts

204 lines
5.5 KiB
TypeScript
Raw Normal View History

2022-04-29 19:18:11 +03:00
///
2023-01-31 10:43:56 +02:00
/// Copyright © 2016-2023 The Thingsboard Authors
2022-04-29 19:18:11 +03:00
///
/// 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.
///
2022-05-05 11:02:45 +03:00
import { Component, forwardRef, Input, OnDestroy } from '@angular/core';
2022-04-29 19:18:11 +03:00
import {
AbstractControl,
ControlValueAccessor,
FormArray,
FormBuilder,
FormGroup,
NG_VALIDATORS,
NG_VALUE_ACCESSOR,
2022-05-05 11:02:45 +03:00
ValidationErrors,
2022-04-29 19:18:11 +03:00
Validator,
Validators
} from '@angular/forms';
import { Store } from '@ngrx/store';
import { AppState } from '@app/core/core.state';
import { coerceBooleanProperty } from '@angular/cdk/coercion';
import { Subscription } from 'rxjs';
import { QueueInfo } from '@shared/models/queue.models';
2022-05-05 11:02:45 +03:00
import { UtilsService } from '@core/services/utils.service';
2022-05-25 18:45:00 +03:00
import { guid } from '@core/utils';
2022-04-29 19:18:11 +03:00
@Component({
selector: 'tb-tenant-profile-queues',
templateUrl: './tenant-profile-queues.component.html',
styleUrls: ['./tenant-profile-queues.component.scss'],
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => TenantProfileQueuesComponent),
multi: true
},
{
provide: NG_VALIDATORS,
useExisting: forwardRef(() => TenantProfileQueuesComponent),
multi: true,
}
]
})
2022-05-05 11:02:45 +03:00
export class TenantProfileQueuesComponent implements ControlValueAccessor, Validator, OnDestroy {
2022-04-29 19:18:11 +03:00
tenantProfileQueuesFormGroup: FormGroup;
newQueue = false;
idMap = [];
2022-04-29 19:18:11 +03:00
private requiredValue: boolean;
get required(): boolean {
return this.requiredValue;
}
@Input()
set required(value: boolean) {
this.requiredValue = coerceBooleanProperty(value);
}
@Input()
disabled: boolean;
2022-05-05 11:02:45 +03:00
private valueChangeSubscription$: Subscription = null;
2022-04-29 19:18:11 +03:00
private propagateChange = (v: any) => { };
constructor(private store: Store<AppState>,
2022-05-05 11:02:45 +03:00
private utils: UtilsService,
2022-04-29 19:18:11 +03:00
private fb: FormBuilder) {
}
registerOnChange(fn: any): void {
this.propagateChange = fn;
}
2022-05-05 11:02:45 +03:00
ngOnDestroy() {
if (this.valueChangeSubscription$) {
this.valueChangeSubscription$.unsubscribe();
}
}
2022-04-29 19:18:11 +03:00
registerOnTouched(fn: any): void {
}
ngOnInit() {
this.tenantProfileQueuesFormGroup = this.fb.group({
queues: this.fb.array([])
});
}
2022-05-05 11:02:45 +03:00
get queuesFormArray(): FormArray {
2022-04-29 19:18:11 +03:00
return this.tenantProfileQueuesFormGroup.get('queues') as FormArray;
}
setDisabledState(isDisabled: boolean): void {
this.disabled = isDisabled;
if (this.disabled) {
this.tenantProfileQueuesFormGroup.disable({emitEvent: false});
} else {
this.tenantProfileQueuesFormGroup.enable({emitEvent: false});
}
}
writeValue(queues: Array<QueueInfo> | null): void {
2022-05-05 11:02:45 +03:00
if (this.valueChangeSubscription$) {
this.valueChangeSubscription$.unsubscribe();
2022-04-29 19:18:11 +03:00
}
const queuesControls: Array<AbstractControl> = [];
if (queues) {
queues.forEach((queue, index) => {
if (!queue.id) {
if (!this.idMap[index]) {
this.idMap.push(guid());
}
queue.id = this.idMap[index];
}
2022-04-29 19:18:11 +03:00
queuesControls.push(this.fb.control(queue, [Validators.required]));
});
}
this.tenantProfileQueuesFormGroup.setControl('queues', this.fb.array(queuesControls));
if (this.disabled) {
this.tenantProfileQueuesFormGroup.disable({emitEvent: false});
} else {
this.tenantProfileQueuesFormGroup.enable({emitEvent: false});
}
2022-06-01 19:14:09 +03:00
this.valueChangeSubscription$ = this.tenantProfileQueuesFormGroup.valueChanges.subscribe(() =>
this.updateModel()
);
2022-04-29 19:18:11 +03:00
}
2022-05-06 12:28:11 +03:00
public trackByQueue(index: number, queueControl: AbstractControl) {
2022-05-25 18:45:00 +03:00
if (queueControl) {
return queueControl.value.id;
}
return null;
2022-04-29 19:18:11 +03:00
}
public removeQueue(index: number) {
(this.tenantProfileQueuesFormGroup.get('queues') as FormArray).removeAt(index);
this.idMap.splice(index, 1);
2022-04-29 19:18:11 +03:00
}
public addQueue() {
const queue = {
2022-05-25 18:45:00 +03:00
id: guid(),
2022-04-29 19:18:11 +03:00
consumerPerPartition: false,
name: '',
packProcessingTimeout: 2000,
partitions: 10,
pollInterval: 25,
processingStrategy: {
failurePercentage: 0,
maxPauseBetweenRetries: 3,
pauseBetweenRetries: 3,
retries: 3,
type: ''
},
submitStrategy: {
batchSize: 0,
type: ''
},
2022-05-25 18:45:00 +03:00
topic: '',
additionalInfo: {
description: ''
}
2022-04-29 19:18:11 +03:00
};
this.idMap.push(queue.id);
2022-04-29 19:18:11 +03:00
this.newQueue = true;
const queuesArray = this.tenantProfileQueuesFormGroup.get('queues') as FormArray;
queuesArray.push(this.fb.control(queue, []));
this.tenantProfileQueuesFormGroup.updateValueAndValidity();
if (!this.tenantProfileQueuesFormGroup.valid) {
this.updateModel();
}
}
getTitle(value): string {
return this.utils.customTranslation(value, value);
}
2022-05-05 11:02:45 +03:00
public validate(c: AbstractControl): ValidationErrors | null {
return this.tenantProfileQueuesFormGroup.valid ? null : {
2022-04-29 19:18:11 +03:00
queues: {
valid: false,
},
};
}
private updateModel() {
const queues: Array<QueueInfo> = this.tenantProfileQueuesFormGroup.get('queues').value;
this.propagateChange(queues);
}
}