argos/dmp-frontend/src/app/ui/admin/tenant/editor/tenant-editor.component.ts

289 lines
10 KiB
TypeScript

import { Component, OnInit } from '@angular/core';
import { FormArray, UntypedFormGroup } from '@angular/forms';
import { MatDialog } from '@angular/material/dialog';
import { ActivatedRoute, Router } from '@angular/router';
import { TenantService } from '@app/core/services/tenant/tenant.service';
import { SnackBarNotificationLevel, UiNotificationService } from '@app/core/services/notification/ui-notification-service';
import { EnumUtils } from '@app/core/services/utilities/enum-utils.service';
// import { BreadcrumbItem } from '@app/ui/misc/breadcrumb/definition/breadcrumb-item';
import { DatePipe } from '@angular/common';
import { IsActive } from '@app/core/common/enum/is-active.enum';
import { AppPermission } from '@app/core/common/enum/permission.enum';
import { Tenant, TenantPersist } from '@app/core/model/tenant/tenant';
import { AuthService } from '@app/core/services/auth/auth.service';
import { LoggingService } from '@app/core/services/logging/logging-service';
import { MatomoService } from '@app/core/services/matomo/matomo-service';
import { FileUtils } from '@app/core/services/utilities/file-utils.service';
import { QueryParamsService } from '@app/core/services/utilities/query-params.service';
import { MultipleAutoCompleteConfiguration } from '@app/library/auto-complete/multiple/multiple-auto-complete-configuration';
import { BaseEditor } from '@common/base/base-editor';
import { FormService } from '@common/forms/form-service';
import { ConfirmationDialogComponent } from '@common/modules/confirmation-dialog/confirmation-dialog.component';
import { HttpErrorHandlingService } from '@common/modules/errors/error-handling/http-error-handling.service';
import { FilterService } from '@common/modules/text-filter/filter-service';
import { Guid } from '@common/types/guid';
import { TranslateService } from '@ngx-translate/core';
import { map, takeUntil } from 'rxjs/operators';
import { TenantEditorResolver } from './tenant-editor.resolver';
import { TenantEditorService } from './tenant-editor.service';
import { TenantEditorModel, TenantSourceEditorModel } from './tenant-editor.model';
import { MatChipEditedEvent, MatChipInputEvent } from '@angular/material/chips';
@Component({
selector: 'app-tenant-editor-component',
templateUrl: 'tenant-editor.component.html',
styleUrls: ['./tenant-editor.component.scss'],
providers: [TenantEditorService]
})
export class TenantEditorComponent extends BaseEditor<TenantEditorModel, Tenant> implements OnInit {
isNew = true;
isDeleted = false;
formGroup: UntypedFormGroup = null;
showInactiveDetails = false;
depositCodes: string[] = [];
fileTransformersCodes: string[] = [];
protected get canDelete(): boolean {
return !this.isDeleted && !this.isNew && this.hasPermission(this.authService.permissionEnum.DeleteTenant);
}
protected get canSave(): boolean {
return !this.isDeleted && this.hasPermission(this.authService.permissionEnum.EditTenant);
}
protected get canFinalize(): boolean {
return !this.isDeleted && this.hasPermission(this.authService.permissionEnum.EditTenant);
}
private hasPermission(permission: AppPermission): boolean {
return this.authService.hasPermission(permission) || this.editorModel?.permissions?.includes(permission);
}
constructor(
// BaseFormEditor injected dependencies
protected dialog: MatDialog,
protected language: TranslateService,
protected formService: FormService,
protected router: Router,
protected uiNotificationService: UiNotificationService,
protected httpErrorHandlingService: HttpErrorHandlingService,
protected filterService: FilterService,
protected datePipe: DatePipe,
protected route: ActivatedRoute,
protected queryParamsService: QueryParamsService,
// Rest dependencies. Inject any other needed deps here:
public authService: AuthService,
public enumUtils: EnumUtils,
private tenantService: TenantService,
private logger: LoggingService,
private tenantEditorService: TenantEditorService,
private fileUtils: FileUtils,
private matomoService: MatomoService
) {
super(dialog, language, formService, router, uiNotificationService, httpErrorHandlingService, filterService, datePipe, route, queryParamsService);
}
ngOnInit(): void {
this.matomoService.trackPageView('Admin: Tenants');
super.ngOnInit();
}
getItem(itemId: Guid, successFunction: (item: Tenant) => void) {
this.tenantService.getSingle(itemId, TenantEditorResolver.lookupFields())
.pipe(map(data => data as Tenant), takeUntil(this._destroyed))
.subscribe(
data => successFunction(data),
error => this.onCallbackError(error)
);
}
prepareForm(data: Tenant) {
try {
this.editorModel = data ? new TenantEditorModel().fromModel(data) : new TenantEditorModel();
if(data && data.config){
if(data.config.deposit && data.config.deposit.sources) data.config.deposit.sources.forEach(source => this.depositCodes = source.codes)
if(data.config.fileTransformers && data.config.fileTransformers.sources) data.config.fileTransformers.sources.forEach(source => this.fileTransformersCodes = source.codes)
}
this.isDeleted = data ? data.isActive === IsActive.Inactive : false;
this.buildForm();
} catch (error) {
this.logger.error('Could not parse Tenant item: ' + data + error);
this.uiNotificationService.snackBarNotification(this.language.instant('COMMONS.ERRORS.DEFAULT'), SnackBarNotificationLevel.Error);
}
}
buildForm() {
this.formGroup = this.editorModel.buildForm(null, this.isDeleted || !this.authService.hasPermission(AppPermission.EditTenant));
this.tenantEditorService.setValidationErrorModel(this.editorModel.validationErrorModel);
}
refreshData(): void {
this.getItem(this.editorModel.id, (data: Tenant) => this.prepareForm(data));
}
refreshOnNavigateToData(id?: Guid): void {
this.formGroup.markAsPristine();
let route = [];
if (id === null) {
route.push('../..');
} else if (this.isNew) {
route.push('../' + id);
} else {
route.push('..');
}
this.router.navigate(route, { queryParams: { 'lookup': this.queryParamsService.serializeLookup(this.lookupParams), 'lv': ++this.lv }, replaceUrl: true, relativeTo: this.route });
}
persistEntity(onSuccess?: (response) => void): void {
const formData = this.formService.getValue(this.formGroup.value) as TenantPersist;
this.tenantService.persist(formData)
.pipe(takeUntil(this._destroyed)).subscribe(
complete => onSuccess ? onSuccess(complete) : this.onCallbackSuccess(complete),
error => this.onCallbackError(error)
);
}
formSubmit(): void {
this.formService.touchAllFormFields(this.formGroup);
if (!this.isFormValid()) {
return;
}
this.persistEntity();
}
public delete() {
const value = this.formGroup.value;
if (value.id) {
const dialogRef = this.dialog.open(ConfirmationDialogComponent, {
maxWidth: '300px',
data: {
message: this.language.instant('GENERAL.CONFIRMATION-DIALOG.DELETE-ITEM'),
confirmButton: this.language.instant('GENERAL.CONFIRMATION-DIALOG.ACTIONS.CONFIRM'),
cancelButton: this.language.instant('GENERAL.CONFIRMATION-DIALOG.ACTIONS.CANCEL')
}
});
dialogRef.afterClosed().pipe(takeUntil(this._destroyed)).subscribe(result => {
if (result) {
this.tenantService.delete(value.id).pipe(takeUntil(this._destroyed))
.subscribe(
complete => this.onCallbackSuccess(),
error => this.onCallbackError(error)
);
}
});
}
}
clearErrorModel() {
this.editorModel.validationErrorModel.clear();
this.formService.validateAllFormFields(this.formGroup);
}
//
// deposit source
//
addDepositSource(): void {
(this.formGroup.get('config').get('deposit').get('sources') as FormArray).push(this.editorModel.createChildDeposit((this.formGroup.get('config').get('deposit').get('sources') as FormArray).length));
}
removeDepositSource(sourceIndex: number): void {
(this.formGroup.get('config').get('deposit').get('sources') as FormArray).removeAt(sourceIndex);
//Reapply validators
TenantEditorModel.reApplyDepositSourcesValidators(
{
formGroup: this.formGroup,
validationErrorModel: this.editorModel.validationErrorModel
}
)
this.formGroup.get('config').get('deposit').get('sources').markAsDirty();
}
// deposit source codes
addDepositCode(event: MatChipInputEvent): void {
const value = (event.value || '').trim();
if (value) this.depositCodes.push(value)
event.chipInput!.clear();
}
removeDepositCode(code: string): void {
const index = this.depositCodes.indexOf(code);
if (index >= 0) this.depositCodes.splice(index, 1);
}
editDepositCode(code: string, event: MatChipEditedEvent) {
const value = event.value.trim();
// Remove code if it no longer has a value
if (!value) {
this.removeDepositCode(code);
return;
}
const index = this.depositCodes.indexOf(code);
if (index >= 0) this.depositCodes[index] = value
}
//
// fileTransformers source
//
addFileSource(): void {
(this.formGroup.get('config').get('fileTransformers').get('sources') as FormArray).push(this.editorModel.createChildFileTransformer((this.formGroup.get('config').get('fileTransformers').get('sources') as FormArray).length));
}
removeFileSource(sourceIndex: number): void {
(this.formGroup.get('config').get('fileTransformers').get('sources') as FormArray).removeAt(sourceIndex);
//Reapply validators
TenantEditorModel.reApplyFileTransformerSourcesValidators(
{
formGroup: this.formGroup,
validationErrorModel: this.editorModel.validationErrorModel
}
)
this.formGroup.get('config').get('fileTransformers').get('sources').markAsDirty();
}
// fileTransformers source codes
addFileCode(event: MatChipInputEvent): void {
const value = (event.value || '').trim();
if (value) this.fileTransformersCodes.push(value)
event.chipInput!.clear();
}
removeFileCode(code: string): void {
const index = this.fileTransformersCodes.indexOf(code);
if (index >= 0) this.fileTransformersCodes.splice(index, 1);
}
editFileCode(code: string, event: MatChipEditedEvent) {
const value = event.value.trim();
// Remove code if it no longer has a value
if (!value) {
this.removeFileCode(code);
return;
}
const index = this.fileTransformersCodes.indexOf(code);
if (index >= 0) this.fileTransformersCodes[index] = value
}
}