2022-05-25 20:05:59 +03:00
|
|
|
///
|
2023-01-31 10:43:56 +02:00
|
|
|
/// Copyright © 2016-2023 The Thingsboard Authors
|
2022-05-25 20:05:59 +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-06-20 19:48:29 +03:00
|
|
|
import { ChangeDetectorRef, Component, Input, OnDestroy, OnInit } from '@angular/core';
|
2022-05-25 20:05:59 +03:00
|
|
|
import { PageComponent } from '@shared/components/page.component';
|
2023-02-02 15:55:06 +02:00
|
|
|
import { UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms';
|
2022-05-25 20:05:59 +03:00
|
|
|
import {
|
|
|
|
|
SingleEntityVersionCreateRequest,
|
|
|
|
|
VersionCreateRequestType,
|
|
|
|
|
VersionCreationResult
|
|
|
|
|
} from '@shared/models/vc.models';
|
|
|
|
|
import { Store } from '@ngrx/store';
|
|
|
|
|
import { AppState } from '@core/core.state';
|
|
|
|
|
import { EntitiesVersionControlService } from '@core/http/entities-version-control.service';
|
|
|
|
|
import { EntityId } from '@shared/models/id/entity-id';
|
2022-05-26 13:50:50 +03:00
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
2022-06-20 19:48:29 +03:00
|
|
|
import { Observable, of, Subscription } from 'rxjs';
|
2022-06-02 12:32:10 +03:00
|
|
|
import { EntityType } from '@shared/models/entity-type.models';
|
|
|
|
|
import { TbPopoverComponent } from '@shared/components/popover.component';
|
2022-06-20 19:48:29 +03:00
|
|
|
import { share } from 'rxjs/operators';
|
2022-06-21 13:10:20 +03:00
|
|
|
import { parseHttpErrorMessage } from '@core/utils';
|
2022-05-25 20:05:59 +03:00
|
|
|
|
|
|
|
|
@Component({
|
2022-05-30 19:05:17 +03:00
|
|
|
selector: 'tb-entity-version-create',
|
|
|
|
|
templateUrl: './entity-version-create.component.html',
|
2022-06-02 12:32:10 +03:00
|
|
|
styleUrls: ['./version-control.scss']
|
2022-05-25 20:05:59 +03:00
|
|
|
})
|
2022-06-20 19:48:29 +03:00
|
|
|
export class EntityVersionCreateComponent extends PageComponent implements OnInit, OnDestroy {
|
2022-05-25 20:05:59 +03:00
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
branch: string;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
entityId: EntityId;
|
|
|
|
|
|
2022-06-01 13:10:32 +03:00
|
|
|
@Input()
|
|
|
|
|
entityName: string;
|
|
|
|
|
|
2022-05-25 20:05:59 +03:00
|
|
|
@Input()
|
|
|
|
|
onClose: (result: VersionCreationResult | null, branch: string | null) => void;
|
|
|
|
|
|
2022-05-26 13:50:50 +03:00
|
|
|
@Input()
|
2022-06-02 12:32:10 +03:00
|
|
|
onBeforeCreateVersion: () => Observable<any>;
|
2022-05-26 13:50:50 +03:00
|
|
|
|
2022-06-01 18:02:52 +03:00
|
|
|
@Input()
|
2022-06-02 12:32:10 +03:00
|
|
|
popoverComponent: TbPopoverComponent;
|
2022-06-01 18:02:52 +03:00
|
|
|
|
2023-02-02 15:55:06 +02:00
|
|
|
createVersionFormGroup: UntypedFormGroup;
|
2022-05-25 20:05:59 +03:00
|
|
|
|
2022-06-02 12:32:10 +03:00
|
|
|
entityTypes = EntityType;
|
|
|
|
|
|
2022-05-26 13:50:50 +03:00
|
|
|
resultMessage: string;
|
|
|
|
|
|
2022-06-20 19:48:29 +03:00
|
|
|
versionCreateResult$: Observable<VersionCreationResult>;
|
|
|
|
|
|
|
|
|
|
private versionCreateResultSubscription: Subscription;
|
|
|
|
|
|
2022-05-25 20:05:59 +03:00
|
|
|
constructor(protected store: Store<AppState>,
|
|
|
|
|
private entitiesVersionControlService: EntitiesVersionControlService,
|
2022-06-02 12:32:10 +03:00
|
|
|
private cd: ChangeDetectorRef,
|
2022-05-26 13:50:50 +03:00
|
|
|
private translate: TranslateService,
|
2023-02-02 15:55:06 +02:00
|
|
|
private fb: UntypedFormBuilder) {
|
2022-05-25 20:05:59 +03:00
|
|
|
super(store);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
2022-05-30 19:05:17 +03:00
|
|
|
this.createVersionFormGroup = this.fb.group({
|
2022-05-25 20:05:59 +03:00
|
|
|
branch: [this.branch, [Validators.required]],
|
2022-06-01 13:10:32 +03:00
|
|
|
versionName: [this.translate.instant('version-control.default-create-entity-version-name',
|
2022-06-27 14:29:57 +03:00
|
|
|
{entityName: this.entityName}), [Validators.required, Validators.pattern(/(?:.|\s)*\S(&:.|\s)*/)]],
|
2022-05-31 16:16:24 +03:00
|
|
|
saveRelations: [false, []],
|
2022-06-02 12:32:10 +03:00
|
|
|
saveAttributes: [true, []],
|
|
|
|
|
saveCredentials: [true, []]
|
2022-05-25 20:05:59 +03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-20 19:48:29 +03:00
|
|
|
ngOnDestroy() {
|
|
|
|
|
super.ngOnDestroy();
|
|
|
|
|
if (this.versionCreateResultSubscription) {
|
|
|
|
|
this.versionCreateResultSubscription.unsubscribe();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-25 20:05:59 +03:00
|
|
|
cancel(): void {
|
|
|
|
|
if (this.onClose) {
|
|
|
|
|
this.onClose(null, null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export(): void {
|
2022-06-01 18:02:52 +03:00
|
|
|
const before = this.onBeforeCreateVersion ? this.onBeforeCreateVersion() : of(null);
|
|
|
|
|
before.subscribe(() => {
|
|
|
|
|
const request: SingleEntityVersionCreateRequest = {
|
|
|
|
|
entityId: this.entityId,
|
|
|
|
|
branch: this.createVersionFormGroup.get('branch').value,
|
|
|
|
|
versionName: this.createVersionFormGroup.get('versionName').value,
|
|
|
|
|
config: {
|
|
|
|
|
saveRelations: this.createVersionFormGroup.get('saveRelations').value,
|
|
|
|
|
saveAttributes: this.createVersionFormGroup.get('saveAttributes').value,
|
2022-06-02 12:32:10 +03:00
|
|
|
saveCredentials: this.entityId.entityType === EntityType.DEVICE ? this.createVersionFormGroup.get('saveCredentials').value : false
|
2022-06-01 18:02:52 +03:00
|
|
|
},
|
|
|
|
|
type: VersionCreateRequestType.SINGLE_ENTITY
|
|
|
|
|
};
|
2022-06-21 13:10:20 +03:00
|
|
|
this.versionCreateResult$ = this.entitiesVersionControlService.saveEntitiesVersion(request, {ignoreErrors: true}).pipe(
|
2022-06-20 19:48:29 +03:00
|
|
|
share()
|
|
|
|
|
);
|
|
|
|
|
this.cd.detectChanges();
|
|
|
|
|
if (this.popoverComponent) {
|
|
|
|
|
this.popoverComponent.updatePosition();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.versionCreateResultSubscription = this.versionCreateResult$.subscribe((result) => {
|
|
|
|
|
if (result.done) {
|
|
|
|
|
if (!result.added && !result.modified || result.error) {
|
|
|
|
|
this.resultMessage = result.error ? result.error : this.translate.instant('version-control.nothing-to-commit');
|
|
|
|
|
this.cd.detectChanges();
|
|
|
|
|
if (this.popoverComponent) {
|
|
|
|
|
this.popoverComponent.updatePosition();
|
|
|
|
|
}
|
|
|
|
|
} else if (this.onClose) {
|
|
|
|
|
this.onClose(result, request.branch);
|
2022-06-01 18:02:52 +03:00
|
|
|
}
|
2022-05-26 13:50:50 +03:00
|
|
|
}
|
2022-06-21 13:10:20 +03:00
|
|
|
},
|
|
|
|
|
(error) => {
|
|
|
|
|
this.resultMessage = parseHttpErrorMessage(error, this.translate).message;
|
|
|
|
|
this.cd.detectChanges();
|
|
|
|
|
if (this.popoverComponent) {
|
|
|
|
|
this.popoverComponent.updatePosition();
|
|
|
|
|
}
|
2022-06-01 18:02:52 +03:00
|
|
|
});
|
2022-05-25 20:05:59 +03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|