82 lines
2.2 KiB
TypeScript
82 lines
2.2 KiB
TypeScript
import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
|
|
import { FormBuilder, FormGroup } from '@angular/forms';
|
|
import { MatDialog } from '@angular/material/dialog';
|
|
import { MatSelectChange } from '@angular/material/select';
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
|
import { map } from 'rxjs/operators';
|
|
|
|
import { ToasterService } from '../core/toaster.service';
|
|
|
|
import { HeaderFooter } from './header-footer';
|
|
import { HeaderFooterService } from './header-footer.service';
|
|
|
|
@Component({
|
|
selector: 'app-section-printer',
|
|
templateUrl: './header-footer.component.html',
|
|
styleUrls: ['./header-footer.component.css'],
|
|
})
|
|
export class HeaderFooterComponent implements OnInit {
|
|
@ViewChild('section', { static: true }) sectionElement?: ElementRef;
|
|
form: FormGroup;
|
|
list: HeaderFooter[] = [];
|
|
id = '';
|
|
|
|
constructor(
|
|
private route: ActivatedRoute,
|
|
private router: Router,
|
|
private fb: FormBuilder,
|
|
private toaster: ToasterService,
|
|
private dialog: MatDialog,
|
|
private ser: HeaderFooterService,
|
|
) {
|
|
// Create form
|
|
this.form = this.fb.group({
|
|
id: '',
|
|
text: '',
|
|
});
|
|
route.params.pipe(map((p) => p.id)).subscribe((x) => {
|
|
this.id = x;
|
|
});
|
|
}
|
|
|
|
ngOnInit() {
|
|
this.route.data.subscribe((value) => {
|
|
const data = value as { list: HeaderFooter[] };
|
|
this.showItem(data.list);
|
|
});
|
|
}
|
|
|
|
showItem(list: HeaderFooter[]) {
|
|
this.list = list;
|
|
const val = this.list.find((v) => v.id === this.id) as HeaderFooter;
|
|
this.form.setValue({
|
|
id: this.id,
|
|
text: val.text,
|
|
});
|
|
}
|
|
|
|
save() {
|
|
this.ser.save(this.getItem()).subscribe(
|
|
(result: HeaderFooter[]) => {
|
|
this.toaster.show('Success', '');
|
|
this.showItem(result);
|
|
},
|
|
(error) => {
|
|
this.toaster.show('Danger', error.error);
|
|
},
|
|
);
|
|
}
|
|
|
|
getItem(): HeaderFooter {
|
|
const formModel = this.form.value;
|
|
const item = this.list.find((v) => v.id === this.id);
|
|
if (item === undefined) return new HeaderFooter();
|
|
item.text = formModel.text;
|
|
return item;
|
|
}
|
|
|
|
show(val: MatSelectChange) {
|
|
this.router.navigate(['/header-footer', val.value]);
|
|
}
|
|
}
|