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

228 lines
8.8 KiB
TypeScript

import { Component, OnInit } from '@angular/core';
import { UntypedFormArray, UntypedFormGroup } from '@angular/forms';
import { MatDialog } from '@angular/material/dialog';
import { SnackBarNotificationLevel, UiNotificationService } from '@app/core/services/notification/ui-notification-service';
import { AppPermission } from '@app/core/common/enum/permission.enum';
import { AuthService } from '@app/core/services/auth/auth.service';
import { MatomoService } from '@app/core/services/matomo/matomo-service';
import { FormService } from '@common/forms/form-service';
import { ConfirmationDialogComponent } from '@common/modules/confirmation-dialog/confirmation-dialog.component';
import { HttpError, HttpErrorHandlingService } from '@common/modules/errors/error-handling/http-error-handling.service';
import { TranslateService } from '@ngx-translate/core';
import { map, takeUntil } from 'rxjs/operators';
import { DepositSourceEditorModel, DepositTenantConfigurationEditorModel, TenantConfigurationEditorModel } from './deposit-editor.model';
import { TenantConfiguration, TenantConfigurationPersist } from '@app/core/model/tenant-configuaration/tenant-configuration';
import { TenantConfigurationService } from '@app/core/services/tenant-configuration/tenant-configuration.service';
import { DepositEditorService } from './deposit-editor.service';
import { DepositEditorResolver } from './deposit-editor.resolver';
import { BasePendingChangesComponent } from '@common/base/base-pending-changes.component';
import { Observable } from 'rxjs';
import { TenantConfigurationType } from '@app/core/common/enum/tenant-configuration-type';
import { HttpErrorResponse } from '@angular/common/http';
import { ResponseErrorCode } from '@app/core/common/enum/respone-error-code';
import { LoggingService } from '@app/core/services/logging/logging-service';
@Component({
selector: 'app-tenant-configuration-deposit-editor',
templateUrl: 'deposit-editor.component.html',
styleUrls: ['./deposit-editor.component.scss'],
providers: [DepositEditorService]
})
export class DepositEditorComponent extends BasePendingChangesComponent implements OnInit {
isNew = true;
formGroup: UntypedFormGroup = null;
get editorModel(): TenantConfigurationEditorModel { return this._editorModel; }
set editorModel(value: TenantConfigurationEditorModel) { this._editorModel = value; }
private _editorModel: TenantConfigurationEditorModel;
protected get canDelete(): boolean {
return !this.isNew && this.hasPermission(this.authService.permissionEnum.DeleteTenantConfiguration);
}
protected get canSave(): boolean {
return this.hasPermission(this.authService.permissionEnum.EditTenantConfiguration);
}
private hasPermission(permission: AppPermission): boolean {
return this.authService.hasPermission(permission);
}
constructor(
protected dialog: MatDialog,
protected language: TranslateService,
protected formService: FormService,
protected uiNotificationService: UiNotificationService,
protected httpErrorHandlingService: HttpErrorHandlingService,
protected authService: AuthService,
private logger: LoggingService,
private tenantConfigurationService: TenantConfigurationService,
private depositEditorService: DepositEditorService,
private matomoService: MatomoService
) {
super();
}
canDeactivate(): boolean | Observable<boolean> {
return this.formGroup ? !this.formGroup.dirty : true;
}
ngOnInit(): void {
this.matomoService.trackPageView('Admin: TenantConfigurations');
this.getItem((entity) => {
this.prepareForm(entity);
if (this.formGroup && this.editorModel.belongsToCurrentTenant == false) {
this.formGroup.disable();
}
});
}
getItem(successFunction: (item: TenantConfiguration) => void) {
this.tenantConfigurationService.getCurrentTenantType(TenantConfigurationType.DepositPlugins, DepositEditorResolver.lookupFields())
.pipe(map(data => data as TenantConfiguration), takeUntil(this._destroyed))
.subscribe(
data => successFunction(data),
error => this.onCallbackError(error)
);
}
onCallbackError(errorResponse: HttpErrorResponse) {
console.log("Error:", errorResponse);
const error: HttpError = this.httpErrorHandlingService.getError(errorResponse);
if (error.statusCode === 400) {
this.editorModel.validationErrorModel.fromJSONObject(errorResponse.error);
if(errorResponse.error.code === ResponseErrorCode.TenantConfigurationTypeCanNotChange){
this.uiNotificationService.snackBarNotification(errorResponse.error.error, SnackBarNotificationLevel.Error);
}
if(errorResponse.error.code === ResponseErrorCode.MultipleTenantConfigurationTypeNotAllowed){
this.uiNotificationService.snackBarNotification(errorResponse.error.error, SnackBarNotificationLevel.Error);
}
this.formService.validateAllFormFields(this.formGroup);
} else {
this.uiNotificationService.snackBarNotification(error.getMessagesString(), SnackBarNotificationLevel.Warning);
}
}
onCallbackSuccess(data?: any): void {
console.log("Success:", data);
this.uiNotificationService.snackBarNotification(this.isNew ? this.language.instant('GENERAL.SNACK-BAR.SUCCESSFUL-CREATION') : this.language.instant('GENERAL.SNACK-BAR.SUCCESSFUL-UPDATE'), SnackBarNotificationLevel.Success);
this.refreshData();
}
prepareForm(data: TenantConfiguration) {
try {
this.editorModel = data ? new TenantConfigurationEditorModel().fromModel(data) : new TenantConfigurationEditorModel();
this.buildForm();
} catch (error) {
this.logger.error('Could not parse TenantConfiguration item: ' + data + error);
this.uiNotificationService.snackBarNotification(this.language.instant('COMMONS.ERRORS.DEFAULT'), SnackBarNotificationLevel.Error);
}
}
buildForm() {
this.formGroup = this.editorModel.buildForm(null, !this.authService.hasPermission(AppPermission.EditTenantConfiguration));
this.depositEditorService.setValidationErrorModel(this.editorModel.validationErrorModel);
}
refreshData(): void {
this.getItem((entity) => {
this.prepareForm(entity);
if (this.formGroup && this.editorModel.belongsToCurrentTenant == false) {
this.formGroup.disable();
}
});
}
persistEntity(onSuccess?: (response) => void): void {
const formData = this.formService.getValue(this.formGroup.value) as TenantConfigurationPersist;
this.tenantConfigurationService.persist(formData)
.pipe(takeUntil(this._destroyed)).subscribe(
complete => onSuccess ? onSuccess(complete) : this.onCallbackSuccess(complete),
error => this.onCallbackError(error)
);
}
formSubmit(): void {
this.clearErrorModel();
this.formService.removeAllBackEndErrors(this.formGroup);
this.formService.touchAllFormFields(this.formGroup);
if (!this.isFormValid()) {
return;
}
this.persistEntity();
}
public isFormValid() {
return this.formGroup.valid;
}
public delete() {
const value = this.formGroup.value;
if (value.id) {
const dialogRef = this.dialog.open(ConfirmationDialogComponent, {
maxWidth: '300px',
data: {
message: this.language.instant('TENANT-CONFIGURATION-EDITOR.RESET-TO-DEFAULT-DIALOG.RESET-TO-DEFAULT'),
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.tenantConfigurationService.delete(value.id).pipe(takeUntil(this._destroyed))
.subscribe(
complete => this.onCallbackDeleteSuccessConfig(),
error => this.onCallbackError(error)
);
}
});
}
}
onCallbackDeleteSuccessConfig(data?: any): void {
console.log("Success Delete:", data);
this.uiNotificationService.snackBarNotification(this.language.instant('GENERAL.SNACK-BAR.SUCCESSFUL-RESET'), SnackBarNotificationLevel.Success);
this.prepareForm(null)
}
clearErrorModel() {
this.editorModel.validationErrorModel.clear();
this.formService.validateAllFormFields(this.formGroup);
}
addSource(): void {
const formArray = this.formGroup.get('depositPlugins').get('sources') as UntypedFormArray;
const source: DepositSourceEditorModel = new DepositSourceEditorModel(this.editorModel.validationErrorModel);
formArray.push(source.buildForm({ rootPath: 'depositPlugins.sources[' + formArray.length + '].' }));
}
removeSource(sourceIndex: number): void {
(this.formGroup.get('depositPlugins').get('sources') as UntypedFormArray).removeAt(sourceIndex);
// Reapply validators
DepositTenantConfigurationEditorModel.reapplySourcesFieldsValidators(
{
formArray: this.formGroup.get('depositPlugins').get('sources') as UntypedFormArray,
validationErrorModel: this.editorModel.validationErrorModel,
rootPath: 'depositPlugins.'
}
)
this.formGroup.get('depositPlugins').get('sources').markAsDirty();
}
}