<commit_before>import {Page, ViewController, NavParams} from 'ionic-angular';
import {AnalyticsService} from '../../services/analytics/analytics.service';
import {ProductsService} from '../../services/products/common.service';
import {categoryEnum} from '../../config/constants';

@Page({
  templateUrl: 'build/modals/widget-add/widget-add.html'
})
export class WidgetAddModal {
  categoryKeys: Array<string> = Object.keys(categoryEnum);
  subCategoryKeys: Array<string> = Object.keys(categoryEnum).filter((cat) => cat !== 'PROJECT');
  CategoryEnum: any = categoryEnum;
  category: any = this.CategoryEnum.DOMAIN;
  subCategory: any = this.CategoryEnum.DOMAIN;
  loading: boolean = true;
  projectName: string;
  errors: any;
  products: Array<any> = [];
  project: Array<any> = [];

  constructor(private viewCtrl: ViewController, private productService: ProductsService, private params: NavParams, private analytics: AnalyticsService) {
    this.fetchProducts(this.category.url);
    this.analytics.trackView('Widget-add');
  }

  close(): void {
    this.viewCtrl.dismiss({});
  }

  addWidget(serviceName: string): void {
    this.viewCtrl.dismiss({category: this.category, serviceName});
  }


  addSubProduct(serviceName: string, category: any): void {
    this.filterCategories(category);
    this.fetchProducts(this.subCategory.url);
    this.project = this.project.concat([{ serviceName, category }]);
  }

  addProject(serviceName: string): void {
    if (this.project.length === 2) {
      this.viewCtrl.dismiss({ category: this.category, project: this.project, serviceName });
    }
  }

  filterCategories(category: any): void{
    if (!category.hardware) {
      this.subCategoryKeys = this.subCategoryKeys.filter((cat) => this.CategoryEnum[cat].hardware);
      this.subCategory = this.CategoryEnum.WEB;
    } else {
      this.subCategoryKeys = this.subCategoryKeys.filter((cat) => !this.CategoryEnum[cat].hardware);
      this.subCategory = this.CategoryEnum.DOMAIN;
    }
  }

  selectCategory(category: any): void {
    this.analytics.trackEvent('Widget-add', 'addWidgetCategory', 'Add', category);
    if (category !== this.CategoryEnum.PROJECT) {
      this.project = [];
      this.projectName = null;
      this.fetchProducts(category.url);
      this.subCategoryKeys = Object.keys(categoryEnum).filter((cat) => cat !== 'PROJECT');
    }
  }

  selectProduct(category: any): void {
    this.analytics.trackEvent('Widget-add', 'addWidgetProduct', 'Add', category);
    this.fetchProducts(category.url);
  }

  fetchProducts(category: string): void {
    this.loading = true;
    this.productService.getAll(category)
      .then(products => {
        if (this.category !== this.CategoryEnum.PROJECT) {
          this.products = products.filter(product => {
            return !(this.params.get('widgets').find(widget => widget.serviceName === product && this.category === widget.category));
          });
        } else {
          this.products = products;
        }

        this.loading = false;
      })<commit_msg>fix(widget-add): don't give the ability to add 2 same widgets (issue: #33)<commit_after>import {Page, ViewController, NavParams} from 'ionic-angular';
import {AnalyticsService} from '../../services/analytics/analytics.service';
import {ProductsService} from '../../services/products/common.service';
import {categoryEnum} from '../../config/constants';

@Page({
  templateUrl: 'build/modals/widget-add/widget-add.html'
})
export class WidgetAddModal {
  categoryKeys: Array<string> = Object.keys(categoryEnum);
  subCategoryKeys: Array<string> = Object.keys(categoryEnum).filter((cat) => cat !== 'PROJECT');
  CategoryEnum: any = categoryEnum;
  category: any = this.CategoryEnum.DOMAIN;
  subCategory: any = this.CategoryEnum.DOMAIN;
  loading: boolean = true;
  projectName: string;
  errors: any;
  products: Array<any> = [];
  project: Array<any> = [];

  constructor(private viewCtrl: ViewController, private productService: ProductsService, private params: NavParams, private analytics: AnalyticsService) {
    this.fetchProducts(this.category.url);
    this.analytics.trackView('Widget-add');
  }

  close(): void {
    this.viewCtrl.dismiss({});
  }

  addWidget(serviceName: string): void {
    this.viewCtrl.dismiss({category: this.category, serviceName});
  }


  addSubProduct(serviceName: string, category: any): void {
    this.filterCategories(category);
    this.fetchProducts(this.subCategory.url);
    this.project = this.project.concat([{ serviceName, category }]);
  }

  addProject(serviceName: string): void {
    if (this.project.length === 2) {
      this.viewCtrl.dismiss({ category: this.category, project: this.project, serviceName });
    }
  }

  filterCategories(category: any): void{
    if (!category.hardware) {
      this.subCategoryKeys = this.subCategoryKeys.filter((cat) => this.CategoryEnum[cat].hardware);
      this.subCategory = this.CategoryEnum.WEB;
    } else {
      this.subCategoryKeys = this.subCategoryKeys.filter((cat) => !this.CategoryEnum[cat].hardware);
      this.subCategory = this.CategoryEnum.DOMAIN;
    }
  }

  selectCategory(category: any): void {
    this.analytics.trackEvent('Widget-add', 'addWidgetCategory', 'Add', category);
    if (category !== this.CategoryEnum.PROJECT) {
      this.project = [];
      this.projectName = null;
      this.fetchProducts(category.url);
      this.subCategoryKeys = Object.keys(categoryEnum).filter((cat) => cat !== 'PROJECT');
    }
  }

  selectProduct(category: any): void {
    this.analytics.trackEvent('Widget-add', 'addWidgetProduct', 'Add', category);
    this.fetchProducts(category.url);
  }

  fetchProducts(category: string): void {
    this.loading = true;
    this.productService.getAll(category)
      .then(products => {
        if (this.category !== this.CategoryEnum.PROJECT) {
          this.products = products.filter(product => {
            return !(this.params.get('widgets').find(widget => widget.serviceName === product && this.category.name === widget.category.name));
          });
        } else {
          this.products = products;
        }

        this.loading = false;
      })
{'fraction_non_alphanumeric': 0.14935803673005038, 'fraction_numerical': 0.0008126117341134406, 'mean_word_length': 3.7965705378020265, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 25, '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': '5507014', 'n_tokens_mistral': 1824, 'n_tokens_neox': 1779, 'n_words': 337}