139 lines
4.1 KiB
TypeScript
139 lines
4.1 KiB
TypeScript
import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
|
|
import { AbstractControl, FormArray, FormBuilder, FormGroup } from '@angular/forms';
|
|
import { MatDialog } from '@angular/material/dialog';
|
|
import { MatSelectChange } from '@angular/material/select';
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
|
import { BehaviorSubject } from 'rxjs';
|
|
import { map } from 'rxjs/operators';
|
|
|
|
import { Printer } from '../core/printer';
|
|
import { Section } from '../core/section';
|
|
import { SectionPrinter } from '../core/section-printer';
|
|
import { ToasterService } from '../core/toaster.service';
|
|
import { ConfirmDialogComponent } from '../shared/confirm-dialog/confirm-dialog.component';
|
|
|
|
import { SectionPrinterDataSource } from './section-printer-datasource';
|
|
import { SectionPrinterService } from './section-printer.service';
|
|
|
|
@Component({
|
|
selector: 'app-section-printer',
|
|
templateUrl: './section-printer.component.html',
|
|
styleUrls: ['./section-printer.component.css'],
|
|
})
|
|
export class SectionPrinterComponent implements OnInit {
|
|
@ViewChild('section', { static: true }) sectionElement?: ElementRef;
|
|
form: FormGroup;
|
|
public listObservable = new BehaviorSubject<SectionPrinter[]>([]);
|
|
dataSource: SectionPrinterDataSource = new SectionPrinterDataSource(this.listObservable);
|
|
list: SectionPrinter[] = [];
|
|
sections: Section[] = [];
|
|
printers: Printer[] = [];
|
|
sectionId = '';
|
|
displayedColumns = ['saleCategory', 'printer', 'copies'];
|
|
|
|
constructor(
|
|
private route: ActivatedRoute,
|
|
private router: Router,
|
|
private fb: FormBuilder,
|
|
private toaster: ToasterService,
|
|
private dialog: MatDialog,
|
|
private ser: SectionPrinterService,
|
|
) {
|
|
// Create form
|
|
this.form = this.fb.group({
|
|
section: '',
|
|
saleCategories: this.fb.array([]),
|
|
});
|
|
route.params.pipe(map((p) => p.id)).subscribe((x) => {
|
|
this.sectionId = x;
|
|
});
|
|
}
|
|
|
|
ngOnInit() {
|
|
this.route.data.subscribe((value) => {
|
|
const data = value as { list: SectionPrinter[]; sections: Section[]; printers: Printer[] };
|
|
this.sections = data.sections;
|
|
this.printers = data.printers;
|
|
this.showItem(data.list);
|
|
this.dataSource = new SectionPrinterDataSource(this.listObservable);
|
|
this.listObservable.next(this.list);
|
|
});
|
|
}
|
|
|
|
showItem(list: SectionPrinter[]) {
|
|
this.list = list;
|
|
(this.form.get('section') as AbstractControl).setValue(this.sectionId);
|
|
this.form.setControl(
|
|
'saleCategories',
|
|
this.fb.array(
|
|
this.list.map((x) =>
|
|
this.fb.group({
|
|
saleCategory: x.saleCategory?.name || 'Default',
|
|
printer: x.printer?.id,
|
|
copies: `${x.copies}`,
|
|
}),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
save() {
|
|
this.ser.save(this.sectionId, this.getItem()).subscribe(
|
|
(result: SectionPrinter[]) => {
|
|
this.toaster.show('Success', '');
|
|
this.showItem(result);
|
|
},
|
|
(error) => {
|
|
this.toaster.show('Error', error);
|
|
},
|
|
);
|
|
}
|
|
|
|
delete() {
|
|
this.ser.delete(this.sectionId).subscribe(
|
|
() => {
|
|
this.toaster.show('Success', '');
|
|
},
|
|
(error) => {
|
|
this.toaster.show('Error', error);
|
|
},
|
|
);
|
|
}
|
|
|
|
confirmDelete(): void {
|
|
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
|
|
width: '250px',
|
|
data: {
|
|
title: 'Delete all printers for this section?',
|
|
content: 'Are you sure? This cannot be undone.',
|
|
},
|
|
});
|
|
|
|
dialogRef.afterClosed().subscribe((result: boolean) => {
|
|
if (result) {
|
|
this.delete();
|
|
}
|
|
});
|
|
}
|
|
|
|
getItem(): SectionPrinter[] {
|
|
const formModel = this.form.value;
|
|
this.sectionId = formModel.section;
|
|
const array = this.form.get('saleCategories') as FormArray;
|
|
this.list.forEach((item, index) => {
|
|
const cont = array.controls[index].value;
|
|
if (cont.printer === null || cont.printer === undefined) {
|
|
item.printer = null;
|
|
} else {
|
|
item.printer = { id: cont.printer };
|
|
}
|
|
item.copies = +cont.copies;
|
|
});
|
|
return this.list;
|
|
}
|
|
|
|
show(val: MatSelectChange) {
|
|
this.router.navigate(['/section-printers', val.value]);
|
|
}
|
|
}
|