25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

139 lines
5.2 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 {CommentJsonld, ContactJsonld, PartnerJsonld, 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 {ModalComponent} from "@app/_components/modal/modal.component";
  12. import {NgbModal, NgbModalOptions} from "@ng-bootstrap/ng-bootstrap";
  13. import {NewProductComponent} from "@app/products/new-product/new-product.component";
  14. import {NewCommentComponent} from "@app/postings/new-comment/new-comment.component";
  15. import {ModalStatus} from "@app/_helpers/modal.states";
  16. @Component({
  17. selector: 'app-products',
  18. templateUrl: './products.component.html',
  19. styleUrl: './products.component.scss',
  20. standalone: true,
  21. imports: [MatTableModule, MatSortModule, MatPaginatorModule, RouterLink, RouterLinkActive, NgIf],
  22. })
  23. export class ProductsComponent implements OnInit, AfterViewInit {
  24. @ViewChild(MatSort) sort;
  25. @ViewChild(MatPaginator) productsPaginator: MatPaginator;
  26. protected displayedColumns: string[];
  27. protected productsSub: Subscription;
  28. protected products: Array<ProductJsonld>;
  29. protected productsDataSource;
  30. protected productsLength: number;
  31. protected productsPageEvent: PageEvent;
  32. protected productsPageSize: number;
  33. protected productPageIndex: number;
  34. protected modalOptions: NgbModalOptions = {
  35. centered: true
  36. };
  37. constructor(
  38. private router: Router,
  39. private modalService: NgbModal,
  40. private productService: ProductService
  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.productPageIndex = 0;
  52. }
  53. ngOnInit() {
  54. this.getProductData();
  55. }
  56. ngAfterViewInit() {
  57. this.productsDataSource.sort = this.sort;
  58. this.productsDataSource.paginator = this.productsPaginator;
  59. }
  60. getProductData() {
  61. this.productsSub = this.productService.productsGetCollection(
  62. this.productPageIndex + 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.productPageIndex = 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.getProductData();
  98. }
  99. handlePageEvent(e: PageEvent) {
  100. this.productsPageEvent = e;
  101. this.productsLength = e.length;
  102. this.productPageIndex = e.pageIndex.valueOf();
  103. this.productsPageSize = e.pageSize.valueOf();
  104. this.getProductData();
  105. }
  106. navigateToProductDetails(element: any) {
  107. const product: ProductJsonld = element as ProductJsonld;
  108. this.router.navigate(['/products', 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.getProductData();
  120. }
  121. });
  122. }
  123. }