ronaldkwandy

EmployeeStatusBrowseComponent

Aug 14th, 2018
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnInit, EventEmitter, Output, OnDestroy, ViewChild } from '@angular/core';
  2.  
  3. import { Subject, of } from 'rxjs';
  4. import { takeUntil } from 'rxjs/operators';
  5.  
  6. import { DragulaService } from 'ng2-dragula';
  7.  
  8. import { MatTableDataSource } from '@angular/material/table';
  9. import { MatPaginator } from '@angular/material/paginator';
  10. import { MatSort } from '@angular/material/sort';
  11.  
  12. import { ActiveStates } from '../../../../../../../gaji-id-resources/combo-constants/active-states.constant';
  13. import { DaResponse } from '../../../../../../../gaji-id-resources/gaji-id-api-response.model';
  14. import { ComboConstant } from '../../../../../../../gaji-id-resources/combo-constants/combo-constant.interface';
  15. import { EmployeeStatus } from '../../../../../../../gaji-id-resources/resources/employee-status/employee-status.model';
  16. import { EmployeeStatusSearchParams, EmployeeStatusSorts } from '../../../../../../../gaji-id-resources/resources/employee-status/employee-status.service';
  17. import { GroupEarnersType } from '../../../../../../../gaji-id-resources/combo-constants/group-earners-type.constant';
  18. import { PkwtType } from '../../../../../../../gaji-id-resources/combo-constants/pkwt-type.constant';
  19.  
  20. import { ContentAlertService } from '../../../../../../../components/messages/alerts/content-alert/content-alert.service';
  21. import { ConfirmDialogService } from '../../../../../../../components/confirm-dialog/confirm-dialog.service';
  22. import { EmployeeStatusService } from '../../../../../../../gaji-id-resources/resources/employee-status/employee-status.service';
  23. import { SnackBarService } from '../../../../../../../components/messages/snackbar/snackbar.service';
  24.  
  25. import * as _ from 'lodash';
  26. import { DaPagingRequest } from '../../../../../../../gaji-id-resources/gaji-id-api-request.model';
  27.  
  28. interface EmployeeStatusRow {
  29.   employeeStatus: EmployeeStatus;
  30.   pkwtType: ComboConstant;
  31.   groupEarnersType: ComboConstant;
  32. }
  33.  
  34. @Component({
  35.   selector: 'app-employee-status-browse',
  36.   templateUrl: './employee-status-browse.component.html',
  37.   styleUrls: ['./employee-status-browse.component.scss']
  38. })
  39. export class EmployeeStatusBrowseComponent implements OnInit, OnDestroy {
  40.  
  41.   // @ViewChild('mainPaginator') mainPaginator: MatPaginator;
  42.   // @ViewChild(MatSort) sort: MatSort;
  43.  
  44.   @Output() public onAdd: EventEmitter<any> = new EventEmitter();
  45.   @Output() public onEdit: EventEmitter<any> = new EventEmitter();
  46.  
  47.   public searchParams: EmployeeStatusSearchParams = {};
  48.   public activeStates = ActiveStates.getValues();
  49.   public displayedColumns = ['action', 'statusName', 'pkwtType', 'groupEarners'];
  50.   public employeeStatuses: MatTableDataSource<EmployeeStatusRow>;
  51.   public employeeStatusRows: EmployeeStatusRow[] = [];
  52.  
  53.   public sorts: EmployeeStatusSorts = null;
  54.   public paging: DaPagingRequest = null;
  55.   public totalRows: number = 0;
  56.   private realTimeSearch: Subject<EmployeeStatusSearchParams> = new Subject();
  57.   private ngUnsubscribe: Subject<boolean> = new Subject();
  58.  
  59.   private isLoadingResults: boolean = false;
  60.  
  61.   constructor(
  62.     private contentAlertService: ContentAlertService,
  63.     private confirmDialogService: ConfirmDialogService,
  64.     private employeeStatusService: EmployeeStatusService,
  65.     private snackbarService: SnackBarService
  66.   ) {
  67.     this.sorts = { statusName: 'asc' };
  68.     this.paging = { page: 1, perPage: 10 };
  69.     this.initSearchTerms();
  70.   }
  71.  
  72.   ngOnInit() {
  73.     this.searchParams.isActive = true;
  74.     this.doInstantFilter();
  75.   }
  76.  
  77.   public ngOnDestroy() {
  78.     this.realTimeSearch.complete();
  79.  
  80.     this.ngUnsubscribe.next(true);
  81.     this.ngUnsubscribe.complete();
  82.   }
  83.  
  84.   public initSearchTerms() {
  85.     this.employeeStatusService
  86.       .realTimeSearch(this.realTimeSearch, this.sorts, this.paging)
  87.       .pipe(takeUntil(this.ngUnsubscribe))
  88.       .subscribe(
  89.         (results: DaResponse<EmployeeStatus[]>) => {
  90.           this.employeeStatusRows = [];
  91.           results.data.forEach(item => {
  92.             this.employeeStatusRows.push({
  93.               employeeStatus: item,
  94.               pkwtType: PkwtType.getValues().find(pkwtType => pkwtType.value === item.pkwtType),
  95.               groupEarnersType: GroupEarnersType.getValues().find(groupEarnersType => groupEarnersType.value === item.groupEarners)
  96.             });
  97.           });
  98.           this.employeeStatuses = new MatTableDataSource<EmployeeStatusRow>(this.employeeStatusRows);
  99.           // this.employeeStatuses.paginator = this.mainPaginator;
  100.           // this.employeeStatuses.sort = this.sort;
  101.           if (!_.isEmpty(results.meta)) {
  102.             this.totalRows = results.meta.pagination.dataCount;
  103.           }
  104.         },
  105.         (error) => {
  106.           this.contentAlertService.error(error);
  107.         }
  108.       );
  109.   }
  110.  
  111.   public doInstantFilter() {
  112.     this.contentAlertService.clear();
  113.     this.employeeStatusService
  114.       .search(this.searchParams, this.sorts, this.paging)
  115.       .pipe(takeUntil(this.ngUnsubscribe))
  116.       .subscribe(
  117.         (results: DaResponse<EmployeeStatus[]>) => {
  118.           this.employeeStatusRows = [];
  119.           results.data.forEach(item => {
  120.             this.employeeStatusRows.push({
  121.               employeeStatus: item,
  122.               pkwtType: PkwtType.getValues().find(pkwtType => pkwtType.value === item.pkwtType),
  123.               groupEarnersType: GroupEarnersType.getValues().find(groupEarnersType => groupEarnersType.value === item.groupEarners)
  124.             });
  125.           });
  126.           this.employeeStatuses = new MatTableDataSource<EmployeeStatusRow>(this.employeeStatusRows);
  127.           // this.employeeStatuses.paginator = this.mainPaginator;
  128.           // this.employeeStatuses.sort = this.sort;
  129.           if (!_.isEmpty(results.meta)) {
  130.             this.totalRows = results.meta.pagination.dataCount;
  131.           }
  132.         },
  133.         (error) => {
  134.           this.contentAlertService.error(error);
  135.         }
  136.       );
  137.   }
  138.  
  139.   public doFilter(filter: any) {
  140.     this.contentAlertService.clear();
  141.     this.realTimeSearch.next(Object.assign({}, this.searchParams));
  142.   }
  143.  
  144.   public add() {
  145.     this.onAdd.next();
  146.   }
  147.  
  148.   public edit(employeeStatus: EmployeeStatus) {
  149.     this.onEdit.emit(employeeStatus);
  150.   }
  151.  
  152.   public delete(employeeStatus: EmployeeStatus) {
  153.     this.confirmDialogService.confirmDelete(employeeStatus.statusName)
  154.       .pipe(takeUntil(this.ngUnsubscribe))
  155.       .subscribe(
  156.         (confirmDelete: boolean) => {
  157.           if (confirmDelete) {
  158.             this.doDelete(employeeStatus);
  159.           }
  160.         }
  161.       );
  162.   }
  163.  
  164.   public doDelete(employeeStatus: EmployeeStatus) {
  165.     this.contentAlertService.clear();
  166.     this.employeeStatusService.delete(employeeStatus).pipe(takeUntil(this.ngUnsubscribe)).subscribe(
  167.       (result) => {
  168.         this.doInstantFilter();
  169.         this.snackbarService.show('Status kepegawaian telah dihapus');
  170.       },
  171.       (error: any) => this.contentAlertService.error(error.feErrors)
  172.     );
  173.   }
  174.  
  175.   onTableScroll(e) {
  176.     const tableViewHeight = e.target.offsetHeight; // viewport: ~500px
  177.     const tableScrollHeight = e.target.scrollHeight; // length of all table
  178.     const scrollLocation = e.target.scrollTop; // how far user scrolled
  179.  
  180.     // If the user has scrolled within 200px of the bottom, add more data
  181.     const buffer = 200;
  182.     const limit = tableScrollHeight - tableViewHeight - buffer;
  183.     if (scrollLocation > limit && this.employeeStatuses.data.length < this.totalRows) {
  184.       console.log(this.totalRows);
  185.       console.log(this.employeeStatuses.data.length);
  186.  
  187.       this.isLoadingResults = true;
  188.       this.paging.page = this.paging.page + 1;
  189.       this.employeeStatusService
  190.         .search(this.searchParams, this.sorts, this.paging)
  191.         .pipe(takeUntil(this.ngUnsubscribe))
  192.         .subscribe(
  193.           (results: DaResponse<EmployeeStatus[]>) => {
  194.             this.employeeStatusRows = [];
  195.             results.data.forEach(item => {
  196.               this.employeeStatusRows.push({
  197.                 employeeStatus: item,
  198.                 pkwtType: PkwtType.getValues().find(pkwtType => pkwtType.value === item.pkwtType),
  199.                 groupEarnersType: GroupEarnersType.getValues().find(groupEarnersType => groupEarnersType.value === item.groupEarners)
  200.               });
  201.             });
  202.             this.employeeStatuses = new MatTableDataSource<EmployeeStatusRow>(this.employeeStatuses.data.concat(this.employeeStatusRows));
  203.             // this.employeeStatuses.paginator = this.mainPaginator;
  204.             // this.employeeStatuses.sort = this.sort;
  205.  
  206.             if (!_.isEmpty(results.meta)) {
  207.               this.totalRows = results.meta.pagination.dataCount;
  208.             }
  209.             this.isLoadingResults = false;
  210.           },
  211.           (error) => {
  212.             this.isLoadingResults = false;
  213.             this.contentAlertService.error(error);
  214.           },
  215.           () => { this.isLoadingResults = false; }
  216.         );
  217.     }
  218.   }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment