You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

139 regels
5.1 KiB

  1. import {AfterViewInit, ChangeDetectorRef, Component, OnInit, ViewChild} from '@angular/core';
  2. import {MatSort, MatSortModule, Sort} from "@angular/material/sort";
  3. import {MatPaginator, MatPaginatorIntl, MatPaginatorModule, PageEvent} from "@angular/material/paginator";
  4. import {MatTableDataSource, MatTableModule} from "@angular/material/table";
  5. import {ProductJsonld, ProductService} from "@app/core/api/v1";
  6. import {OrderFilter} from "@app/_models/orderFilter";
  7. import {ApiConverter} from "@app/_helpers/api.converter";
  8. import {Router, RouterLink, RouterLinkActive} from "@angular/router";
  9. import {NgIf} from "@angular/common";
  10. import {Subscription} from "rxjs";
  11. import {NgbModal, NgbModalOptions} from "@ng-bootstrap/ng-bootstrap";
  12. import {NewProductComponent} from "@app/products/new-product/new-product.component";
  13. import {ModalStatus} from "@app/_helpers/modal.states";
  14. import {TranslateModule} from "@ngx-translate/core";
  15. @Component({
  16. selector: 'app-products',
  17. templateUrl: './products.component.html',
  18. styleUrl: './products.component.scss',
  19. standalone: true,
  20. imports: [MatTableModule, MatSortModule, MatPaginatorModule, RouterLink, RouterLinkActive, NgIf, TranslateModule],
  21. })
  22. export class ProductsComponent implements OnInit, AfterViewInit {
  23. @ViewChild(MatSort) sort;
  24. @ViewChild(MatPaginator) productsPaginator: MatPaginator;
  25. protected displayedColumns: string[];
  26. protected productsSub: Subscription;
  27. protected products: Array<ProductJsonld>;
  28. protected productsDataSource;
  29. protected productsLength: number;
  30. protected productsPageEvent: PageEvent;
  31. protected productsPageSize: number;
  32. protected productsPageIndex: number;
  33. protected modalOptions: NgbModalOptions = {
  34. centered: true
  35. };
  36. constructor(
  37. private router: Router,
  38. private modalService: NgbModal,
  39. private productService: ProductService,
  40. protected apiConverter: ApiConverter,
  41. ) {
  42. this.sort = new MatSort();
  43. this.displayedColumns = ['pos', 'image', 'name', 'storage', 'number'];
  44. this.productsSub = new Subscription();
  45. this.products = [];
  46. this.productsPaginator = new MatPaginator(new MatPaginatorIntl(), ChangeDetectorRef.prototype);
  47. this.productsDataSource = new MatTableDataSource<ProductJsonld>(this.products);
  48. this.productsLength = 0;
  49. this.productsPageEvent = new PageEvent();
  50. this.productsPageSize = 10;
  51. this.productsPageIndex = 0;
  52. }
  53. ngOnInit() {
  54. this.getProductsData();
  55. }
  56. ngAfterViewInit() {
  57. this.productsDataSource.sort = this.sort;
  58. this.productsDataSource.paginator = this.productsPaginator;
  59. }
  60. getProductsData() {
  61. this.productsSub = this.productService.productsGetCollection(
  62. this.productsPageIndex + 1,
  63. this.productsPageSize
  64. ).subscribe(
  65. data => {
  66. this.products = data["hydra:member"];
  67. this.productsDataSource = new MatTableDataSource<ProductJsonld>(this.products);
  68. this.productsLength = Number(data["hydra:totalItems"]);
  69. this.productsPaginator.length = this.productsLength;
  70. }
  71. );
  72. }
  73. /** Announce the change in sort state for assistive technology. */
  74. onSortChange(sortState: Sort) {
  75. // Reset page index to first page
  76. this.productsPageIndex = 0;
  77. let order: OrderFilter;
  78. if (sortState.direction === "") {
  79. order = OrderFilter.Undefined;
  80. } else {
  81. order = sortState.direction;
  82. }
  83. // this.nameOrderAsc = OrderFilter.Undefined;
  84. // this.cityOrderAsc = OrderFilter.Undefined;
  85. // this.websiteOrderAsc = OrderFilter.Undefined;
  86. // switch (sortState.active) {
  87. // case "name":
  88. // this.nameOrderAsc = order;
  89. // break;
  90. // case "address":
  91. // this.cityOrderAsc = order;
  92. // break;
  93. // case "website":
  94. // this.websiteOrderAsc = order;
  95. // break;
  96. // }
  97. this.getProductsData();
  98. }
  99. handlePageEvent(e: PageEvent) {
  100. this.productsPageEvent = e;
  101. this.productsLength = e.length;
  102. this.productsPageIndex = e.pageIndex.valueOf();
  103. this.productsPageSize = e.pageSize.valueOf();
  104. this.getProductsData();
  105. }
  106. navigateToProductDetails(element: any) {
  107. const product: ProductJsonld = element as ProductJsonld;
  108. this.router.navigate(['/products', this.apiConverter.extractId(product.id)]);
  109. }
  110. openModalNewProduct() {
  111. const modalRefProduct = this.modalService.open(NewProductComponent, this.modalOptions);
  112. // TODO: Warum muss ich einen leeren String übergeben, damit es funktioniert?
  113. let product: ProductJsonld = {} as ProductJsonld;
  114. product.name = "";
  115. modalRefProduct.componentInstance.product = product;
  116. modalRefProduct.componentInstance.submit.subscribe((modalStatus: ModalStatus) => {
  117. if (modalStatus === ModalStatus.Submitted) {
  118. modalRefProduct.dismiss();
  119. this.getProductsData();
  120. }
  121. });
  122. }
  123. }