<commit_before>import { Component, Input, ElementRef, ViewChild} from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';

import { Subscription } from 'rxjs';

import { MindsTitle } from '../../services/ux/title';
import { Client } from '../../services/api/client';
import { Session } from '../../services/session';
import { NotificationService } from './notification.service';

@Component({
  moduleId: module.id,
  selector: 'minds-notifications',
  templateUrl: 'notifications.component.html'
})

export class NotificationsComponent {

  @Input() visible: boolean = true;
  @Input() params: any;
  @Input() count: number;
  @Input() loadOnDemand: boolean;
  @ViewChild('notificationGrid') notificationList: ElementRef;
  notifications: Array<Object> = [];
  entity;
  moreData: boolean = true;
  offset: string = '';
  inProgress: boolean = false;
  _filter: string = 'all';

  minds: any = window.Minds;
  paramsSubscription: Subscription;

  constructor(
    public session: Session,
    public client: Client,
    public router: Router,
    public title: MindsTitle,
    public notificationService: NotificationService,
    public route: ActivatedRoute,
    public el: ElementRef,
  ) { }

  ngOnInit() {
    if (!this.session.isLoggedIn()) {
      if (!this.loadOnDemand)
        this.router.navigate(['/login']);
      return;
    }

    this.paramsSubscription = this.route.params.subscribe(params => {
      if (params['filter']) {
        this._filter = params['filter'];
        this.notifications = [];
        this.load(true);
      }
      if (params['ts']) {
        this.notifications = [];
        this.load(true);
        this.notificationService.clear();
      }
    });

    this.notificationService.clear();
    if (!this.loadOnDemand) {
      this.title.setTitle('Notifications');
      this.load(true);
    }

  }

  onVisible() {
    if (this.notifications.length === 0 ) {
      this.load(true);
    } else {
      setTimeout(() => {
        if (this.minds.notifications_count > 0 && this.notificationList.nativeElement.scrollTop <= 100) {
          this.load(true);
        }
      }, 200);
    }
  }
  
  ngOnDestroy() {
    if (this.paramsSubscription)
      this.paramsSubscription.unsubscribe();
  }

  load(refresh: boolean = false) {
    if (this.inProgress) return false;

    if (refresh)
      this.offset = '';

    this.inProgress = true;

    this.client.get(`api/v1/notifications/${this._filter}`, { limit: 24, offset: this.offset })
      .then((data: any) => {

        if (!data.notifications) {
          this.moreData = false;
          this.inProgress = false;
          return false;
        }

        if (refresh) {
          this.notifications = data.notifications;
        } else {
          for (let entity of data.notifications)
            this.notifications.push(entity);
        }

        if (!data['load-next'])
          this.moreData = false;
        this.offset = data['load-next'];
        this.inProgress = false;
        this.minds.notifications_count = 0;
        this.notificationService.clear();
      });
  }

  loadEntity(entity) {
    if (entity.type === 'comment') {
      this.client.get('api/v1/entities/entity/' + entity.parent_guid)
        .then((response: any) => {
          this.entity = response.entity;
        });
    } else {
      this.entity = entity;
    }
  }

  changeFilter(filter) {
      this._filter = filter;
      this.notifications = [];
      this.load(true);
  }
}<commit_msg>[Sprint/Zero](bug): Stopped user changing tab mid-load #389<commit_after>import { Component, Input, ElementRef, ViewChild} from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';

import { Subscription } from 'rxjs';

import { MindsTitle } from '../../services/ux/title';
import { Client } from '../../services/api/client';
import { Session } from '../../services/session';
import { NotificationService } from './notification.service';

@Component({
  moduleId: module.id,
  selector: 'minds-notifications',
  templateUrl: 'notifications.component.html'
})

export class NotificationsComponent {

  @Input() visible: boolean = true;
  @Input() params: any;
  @Input() count: number;
  @Input() loadOnDemand: boolean;
  @ViewChild('notificationGrid') notificationList: ElementRef;
  notifications: Array<Object> = [];
  entity;
  moreData: boolean = true;
  offset: string = '';
  inProgress: boolean = false;
  _filter: string = 'all';

  minds: any = window.Minds;
  paramsSubscription: Subscription;

  constructor(
    public session: Session,
    public client: Client,
    public router: Router,
    public title: MindsTitle,
    public notificationService: NotificationService,
    public route: ActivatedRoute,
    public el: ElementRef,
  ) { }

  ngOnInit() {
    if (!this.session.isLoggedIn()) {
      if (!this.loadOnDemand)
        this.router.navigate(['/login']);
      return;
    }

    this.paramsSubscription = this.route.params.subscribe(params => {
      if (params['filter']) {
        this._filter = params['filter'];
        this.notifications = [];
        this.load(true);
      }
      if (params['ts']) {
        this.notifications = [];
        this.load(true);
        this.notificationService.clear();
      }
    });

    this.notificationService.clear();
    if (!this.loadOnDemand) {
      this.title.setTitle('Notifications');
      this.load(true);
    }

  }

  onVisible() {
    if (this.notifications.length === 0 ) {
      this.load(true);
    } else {
      setTimeout(() => {
        if (this.minds.notifications_count > 0 && this.notificationList.nativeElement.scrollTop <= 100) {
          this.load(true);
        }
      }, 200);
    }
  }
  
  ngOnDestroy() {
    if (this.paramsSubscription)
      this.paramsSubscription.unsubscribe();
  }

  load(refresh: boolean = false) {
    if (this.inProgress) return false;

    if (refresh)
      this.offset = '';

    this.inProgress = true;

    this.client.get(`api/v1/notifications/${this._filter}`, { limit: 24, offset: this.offset })
      .then((data: any) => {

        if (!data.notifications) {
          this.moreData = false;
          this.inProgress = false;
          return false;
        }

        if (refresh) {
          this.notifications = data.notifications;
        } else {
          for (let entity of data.notifications)
            this.notifications.push(entity);
        }

        if (!data['load-next'])
          this.moreData = false;
        this.offset = data['load-next'];
        this.inProgress = false;
        this.minds.notifications_count = 0;
        this.notificationService.clear();
      });
  }

  loadEntity(entity) {
    if (entity.type === 'comment') {
      this.client.get('api/v1/entities/entity/' + entity.parent_guid)
        .then((response: any) => {
          this.entity = response.entity;
        });
    } else {
      this.entity = entity;
    }
  }

  changeFilter(filter) {
      if(this.inProgress){
        return false;
      }
      this.notifications = [];
      this._filter = filter;
      this.load(true);
  }
}
{'fraction_non_alphanumeric': 0.1456953642384106, 'fraction_numerical': 0.004086233619839369, 'mean_word_length': 2.5243296921549154, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '1368510', 'n_tokens_mistral': 2155, 'n_tokens_neox': 2110, 'n_words': 427}