<commit_before>import { AfterViewInit, Component, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { MatTable } from '@angular/material/table';
import { DishTypePagedResponse, DishTypeService } from 'app/dish-type.service';
import { DishTypesDataSource } from './dish-types-datasource';
import { DishType, DishTypeItem } from "../dish-type";
import { FormControl } from '@angular/forms';
import { BehaviorSubject, Subject } from 'rxjs';
import { debounceTime, distinctUntilChanged, takeUntil } from 'rxjs/operators';
import { MatDialog } from '@angular/material/dialog';
import { AddEditDishTypeComponent } from 'app/add-edit-dish-type/add-edit-dish-type.component';

@Component({
  selector: 'dish-types',
  templateUrl: './dish-types.component.html',
  styleUrls: ['./dish-types.component.scss']
})
export class DishTypesComponent implements AfterViewInit, OnInit, OnDestroy {
  @ViewChild(MatPaginator) paginator: MatPaginator;
  @ViewChild(MatSort) sort: MatSort;
  @ViewChild(MatTable) table: MatTable<DishTypeItem>;
  dataSource: DishTypesDataSource;

  filterInput: FormControl;

  private _unsubscribeAll: Subject<any>;

  totalElements: number;
  loading = false;

  private _dishTypes$: BehaviorSubject<DishTypeItem[]>;

  private get dishTypes$() {
    return this._dishTypes$.asObservable();
  }

  constructor(private dtService: DishTypeService, private dialog: MatDialog) {
    this._dishTypes$ = new BehaviorSubject([]);
    this.filterInput = new FormControl('');

    this._unsubscribeAll = new Subject();
  }

  ngOnDestroy(): void {
    // Unsubscribe from all subscriptions
    this._unsubscribeAll.next();
    this._unsubscribeAll.complete();
  }

  /** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
  displayedColumns = ['id', 'name', 'actions'];

  ngOnInit() {
    this.loading = true;
    this.dataSource = new DishTypesDataSource(this.dishTypes$);
    this.filterInput.valueChanges.pipe(
      takeUntil(this._unsubscribeAll),
      debounceTime(300),
      distinctUntilChanged()
    ).subscribe(filter => {
      this.paginator.firstPage();
      this.fetchDishTypeList();
    });
  }

  deleteDishType(dt: DishTypeItem) {
    console.log('Delete', dt);
  }

  editDishType(dt: DishTypeItem) {
    console.log('Edit', dt);
  }

  addDishType = () => {
    const dialog = this.dialog.open<AddEditDishTypeComponent, any, DishType>(AddEditDishTypeComponent);

    dialog.afterClosed().subscribe(
      result => {
        if (result) {
          this.dtService.createDishType(result).subscribe(this.fetchDishTypeList);
        }
      }
    )
  }
<commit_msg>Call service only when a valid name is entered<commit_after>import { AfterViewInit, Component, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { MatTable } from '@angular/material/table';
import { DishTypePagedResponse, DishTypeService } from 'app/dish-type.service';
import { DishTypesDataSource } from './dish-types-datasource';
import { DishType, DishTypeItem } from "../dish-type";
import { FormControl } from '@angular/forms';
import { BehaviorSubject, Subject } from 'rxjs';
import { debounceTime, distinctUntilChanged, takeUntil } from 'rxjs/operators';
import { MatDialog } from '@angular/material/dialog';
import { AddEditDishTypeComponent } from 'app/add-edit-dish-type/add-edit-dish-type.component';

@Component({
  selector: 'dish-types',
  templateUrl: './dish-types.component.html',
  styleUrls: ['./dish-types.component.scss']
})
export class DishTypesComponent implements AfterViewInit, OnInit, OnDestroy {
  @ViewChild(MatPaginator) paginator: MatPaginator;
  @ViewChild(MatSort) sort: MatSort;
  @ViewChild(MatTable) table: MatTable<DishTypeItem>;
  dataSource: DishTypesDataSource;

  filterInput: FormControl;

  private _unsubscribeAll: Subject<any>;

  totalElements: number;
  loading = false;

  private _dishTypes$: BehaviorSubject<DishTypeItem[]>;

  private get dishTypes$() {
    return this._dishTypes$.asObservable();
  }

  constructor(private dtService: DishTypeService, private dialog: MatDialog) {
    this._dishTypes$ = new BehaviorSubject([]);
    this.filterInput = new FormControl('');

    this._unsubscribeAll = new Subject();
  }

  ngOnDestroy(): void {
    // Unsubscribe from all subscriptions
    this._unsubscribeAll.next();
    this._unsubscribeAll.complete();
  }

  /** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
  displayedColumns = ['id', 'name', 'actions'];

  ngOnInit() {
    this.loading = true;
    this.dataSource = new DishTypesDataSource(this.dishTypes$);
    this.filterInput.valueChanges.pipe(
      takeUntil(this._unsubscribeAll),
      debounceTime(300),
      distinctUntilChanged()
    ).subscribe(filter => {
      this.paginator.firstPage();
      this.fetchDishTypeList();
    });
  }

  deleteDishType(dt: DishTypeItem) {
    console.log('Delete', dt);
  }

  editDishType(dt: DishTypeItem) {
    console.log('Edit', dt);
  }

  addDishType = () => {
    const dialog = this.dialog.open<AddEditDishTypeComponent, any, DishType>(AddEditDishTypeComponent);

    dialog.afterClosed().subscribe(
      result => {
        if (result && result.name) {
          this.dtService.createDishType(result).subscribe(this.fetchDishTypeList);
        }
      }
    )
  }

{'fraction_non_alphanumeric': 0.13136971449222984, 'fraction_numerical': 0.0010842067220816769, 'mean_word_length': 4.271428571428571, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7380422', 'n_tokens_mistral': 1727, 'n_tokens_neox': 1694, 'n_words': 351}