paperless-ngx/src-ui/src/app/components/common/edit-dialog/edit-dialog.component.ts

88 lines
2.1 KiB
TypeScript
Raw Normal View History

2020-10-27 01:10:18 +01:00
import { Directive, EventEmitter, Input, OnInit, Output } from '@angular/core';
2020-10-30 22:46:43 +01:00
import { FormGroup } from '@angular/forms';
2020-10-27 01:10:18 +01:00
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
import { Observable } from 'rxjs';
2020-10-30 22:46:43 +01:00
import { MATCHING_ALGORITHMS } from 'src/app/data/matching-model';
2020-10-27 01:10:18 +01:00
import { ObjectWithId } from 'src/app/data/object-with-id';
import { AbstractPaperlessService } from 'src/app/services/rest/abstract-paperless-service';
2020-12-28 21:52:09 +01:00
import { ToastService } from 'src/app/services/toast.service';
2020-10-27 01:10:18 +01:00
@Directive()
export abstract class EditDialogComponent<T extends ObjectWithId> implements OnInit {
constructor(
private service: AbstractPaperlessService<T>,
private activeModal: NgbActiveModal,
2020-12-28 22:54:49 +01:00
private toastService: ToastService) { }
2020-10-27 01:10:18 +01:00
@Input()
dialogMode: string = 'create'
@Input()
object: T
@Output()
success = new EventEmitter()
abstract getForm(): FormGroup
objectForm: FormGroup = this.getForm()
ngOnInit(): void {
if (this.object != null) {
this.objectForm.patchValue(this.object)
}
}
2020-12-28 22:54:49 +01:00
getCreateTitle() {
return $localize`Create new item`
}
getEditTitle() {
return $localize`Edit item`
}
getSaveErrorMessage(error: string) {
return $localize`Could not save element: ${error}`
}
2020-10-27 01:10:18 +01:00
getTitle() {
switch (this.dialogMode) {
case 'create':
2020-12-28 22:54:49 +01:00
return this.getCreateTitle()
2020-10-27 01:10:18 +01:00
case 'edit':
2020-12-28 22:54:49 +01:00
return this.getEditTitle()
2020-10-27 01:10:18 +01:00
default:
break;
}
}
2020-10-28 18:04:50 +01:00
getMatchingAlgorithms() {
2020-10-30 22:46:43 +01:00
return MATCHING_ALGORITHMS
2020-10-28 18:04:50 +01:00
}
2020-10-27 01:10:18 +01:00
save() {
var newObject = Object.assign(Object.assign({}, this.object), this.objectForm.value)
var serverResponse: Observable<T>
switch (this.dialogMode) {
case 'create':
serverResponse = this.service.create(newObject)
break;
case 'edit':
serverResponse = this.service.update(newObject)
default:
break;
}
serverResponse.subscribe(result => {
this.activeModal.close()
this.success.emit(result)
}, error => {
2020-12-28 22:54:49 +01:00
this.toastService.showError(this.getSaveErrorMessage(error.error.name))
2020-10-27 01:10:18 +01:00
})
}
cancel() {
this.activeModal.close()
}
}