ronaldkwandy

ts

Jul 18th, 2019
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnInit, EventEmitter, Output, OnDestroy } from '@angular/core';
  2. import { FormGroup, FormBuilder } from '@angular/forms';
  3.  
  4. import { Subject } from 'rxjs';
  5. import { takeUntil } from 'rxjs/operators';
  6.  
  7. import { GaResponse } from '../../../../../gaji-id-resources/gaji-id-api-response.model';
  8. import { DataClass } from '../../../../../gaji-id-resources/data/data.model';
  9.  
  10. import { ContentAlertService } from '../../../../../components/messages/alerts/content-alert/content-alert.service';
  11. import { DataService } from '../../../../../gaji-id-resources/data/data.servuce';
  12. import { UiBlockService } from '../../../../../components/ui-block/ui-block.service';
  13.  
  14. import * as _ from 'lodash';
  15.  
  16. @Component({
  17.   selector: 'app-data-browse',
  18.   templateUrl: './data-browse.component.html'
  19. })
  20. export class DataBrowseComponent implements OnInit, OnDestroy {
  21.  
  22.   @Output() public onAdd: EventEmitter<DataClass> = new EventEmitter();
  23.   @Output() public onEdit: EventEmitter<DataClass> = new EventEmitter();
  24.  
  25.   public filterForm: FormGroup;
  26.   public data: DataClass[] = [];
  27.   public sorts: DataSortInterface = {};
  28.  
  29.   private ngUnsubscribe: Subject<boolean> = new Subject();
  30.  
  31.   constructor(
  32.     private fb: FormBuilder,
  33.     private contentAlertService: ContentAlertService,
  34.     private dataService: DataService,
  35.     private uiBlockService: UiBlockService,
  36.   ) {
  37.     this.initForm();
  38.   }
  39.  
  40.   ngOnInit() { }
  41.  
  42.   public ngOnDestroy() {
  43.     this.ngUnsubscribe.next(true);
  44.     this.ngUnsubscribe.complete();
  45.   }
  46.  
  47.   public doInstantFilter() {
  48.     this.contentAlertService.clear();
  49.  
  50.     this.uiBlockService.showUiBlock();
  51.     this.dataService
  52.       .search(this.filterForm.value, this.sorts)
  53.       .pipe(takeUntil(this.ngUnsubscribe))
  54.       .subscribe(
  55.         (result: GaResponse<DataClass[]>) => {
  56.           this.uiBlockService.hideUiBlock();
  57.           this.data = result.data;
  58.         },
  59.         (error) => {
  60.           this.uiBlockService.hideUiBlock();
  61.           this.contentAlertService.error(error);
  62.         },
  63.         () => { this.uiBlockService.hideUiBlock(); }
  64.       );
  65.   }
  66.  
  67.   private initForm() {
  68.     this.filterForm = this.fb.group({
  69.       // Form rules here
  70.     });
  71.   }
  72.  
  73.   public add() {
  74.     this.onAdd.emit(DataClass);
  75.   }
  76.  
  77.   public edit(data: DataClass) {
  78.     this.onEdit.emit(DataClass);
  79.   }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment