uoa-repository-manager-service/app/features/administration/templates-management/create-template-dialog/create-template-dialog.comp...

93 lines
3.6 KiB
TypeScript

import { TemplateFormComponent } from './../../forms/template-form/template-form.component';
import { CategoriesService } from 'src/app/shared/services/administration/categories.service';
import { IpowerClientsService } from './../../../../shared/services/administration/ipower-clients.service';
import { Component, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core';
import { FormBuilder, Validators } from '@angular/forms';
import { Category } from 'src/app/shared/models/category.interface';
import { DocumentClassification } from 'src/app/shared/models/document-classification.interface';
import { DocumentSubclassification } from 'src/app/shared/models/document-subclassification.interface';
import { IPowerClient } from 'src/app/shared/models/ipower-client.interface';
import { Template } from 'src/app/shared/models/template.interface';
import { DocumentClassificationsService } from 'src/app/shared/services/administration/document-classifications.service';
import { DocumentSubclassificationsService } from 'src/app/shared/services/administration/document-subclassifications.service';
import { TemplatesService } from 'src/app/shared/services/administration/templates.service';
import { NotificationsHandlingService } from 'src/app/shared/services/notifications-handling/notifications-handling.service';
@Component({
selector: 'app-create-template-dialog',
templateUrl: './create-template-dialog.component.html',
styleUrls: ['./create-template-dialog.component.scss']
})
export class CreateTemplateDialogComponent implements OnInit {
@ViewChild(TemplateFormComponent) templateForm: TemplateFormComponent;
@Input() displayDialog: boolean;
@Input() header: string;
@Input() templateToEdit: Template;
@Output() cancelled = new EventEmitter<boolean>();
@Output() valueChange = new EventEmitter<any>();
displayValidationMessagesEvenIfPristine: boolean;
constructor(private templatesService: TemplatesService, private notificationService: NotificationsHandlingService) { }
ngOnInit(): void {
this.reset(); // Make sure everything is cleanly initialised.
}
reset() {
this.displayValidationMessagesEvenIfPristine = false;
if (this.templateForm) {
this.templateForm.resetForm();
}
}
onShow() {
// If a categoryToEdit was provided.
if (this.templateToEdit) {
this.templateForm.setValue(this.templateToEdit);
}
}
cancel() {
this.cancelled.emit(true);
}
addTemplate() {
// If the form is not valid, make sure validator messages are displayed and then return without doing anything.
if (!this.templateForm.isValid()) {
this.displayValidationMessagesEvenIfPristine = true;
return;
}
// TODO: What is returned from backend on create()??
this.templatesService.createNewTemplate(this.templateForm.formValue()).subscribe(result => {
this.valueChange.emit(result);
this.cancel();
this.notificationService.showCreateNewTemplateSuccess();
},
error => {
});
}
editTemplate() {
// If the form is not valid, make sure validator messages are displayed and then return without doing anything.
if (!this.templateForm.isValid()) {
this.displayValidationMessagesEvenIfPristine = true;
return;
}
// TODO: What is returned from backend on create()??
this.templatesService.updateTemplate(this.templateForm.formValue()).subscribe(result => {
this.valueChange.emit(result);
this.cancel();
this.notificationService.showUpdateTemplateSuccess();
},
error => {
});
}
}