edit-report.component.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { Component } from "@angular/core";
  2. import { MatDialog } from "@angular/material";
  3. import { Location } from '@angular/common';
  4. import { ActivatedRoute } from "@angular/router";
  5. import { ReportDataService } from "./services/reportData.service";
  6. import { BaseReportComponent } from "./base-report.component";
  7. import { Report } from "./types";
  8. @Component({
  9. selector: 'ps-report',
  10. templateUrl: './report.component.html',
  11. styleUrls: ['./report.component.css']
  12. })
  13. export class EditReportComponent extends BaseReportComponent {
  14. title = 'Edit expense report';
  15. private report: Report;
  16. constructor(location: Location,
  17. dialog: MatDialog,
  18. reportDataService: ReportDataService,
  19. route: ActivatedRoute) {
  20. super(location, dialog, reportDataService);
  21. route
  22. .paramMap
  23. .switchMap(params => reportDataService.getReport(+params.get('id')!))
  24. .subscribe(report => {
  25. if (report) {
  26. this.description = report.description;
  27. this.itemsDataBase.addRange(report.items);
  28. this.report = report;
  29. } else {
  30. console.error('No report found for the specified id');
  31. }
  32. });
  33. }
  34. save(): void {
  35. const modifiedReport = {
  36. ...this.report,
  37. description: this.description,
  38. items: this.itemsDataBase.data
  39. };
  40. this.reportDataService.save(modifiedReport);
  41. this.location.back();
  42. }
  43. }