All files / app/assets/javascripts activities.js

80% Statements 16/20
66.66% Branches 2/3
55.55% Functions 5/9
80% Lines 16/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63                      25x 25x     25x   25x   25x 25x 25x 25x         50x                                         25x 25x       25x 25x   25x 25x   25x      
/* eslint-disable class-methods-use-this */
 
import $ from 'jquery';
import { setCookie } from '~/lib/utils/common_utils';
import { createAlert } from '~/alert';
import { s__ } from '~/locale';
import { localTimeAgo } from './lib/utils/datetime_utility';
import Pager from './pager';
 
export default class Activities {
  constructor(containerSelector = '') {
    this.containerSelector = containerSelector;
    this.containerEl = this.containerSelector
      ? document.querySelector(this.containerSelector)
      : undefined;
    this.$contentList = $('.content_list');
 
    this.loadActivities();
 
    $('.event-filter-link').on('click', (e) => {
      e.preventDefault();
      this.toggleFilter(e.currentTarget);
      this.reloadActivities();
    });
  }
 
  loadActivities() {
    Pager.init({
      limit: 20,
      preload: true,
      prepareData: (data) => data,
      successCallback: () => this.updateTooltips(),
      errorCallback: () =>
        createAlert({
          message: s__(
            'Activity|An error occurred while retrieving activity. Reload the page to try again.',
          ),
          parent: this.containerEl,
        }),
      container: this.containerSelector,
    });
  }
 
  updateTooltips() {
    localTimeAgo(document.querySelectorAll('.content_list .js-timeago'));
  }
 
  reloadActivities() {
    this.$contentList.html('');
    this.loadActivities();
  }
 
  toggleFilter(sender) {
    const $sender = $(sender);
    const filter = $sender.attr('id').split('_')[0];
 
    $('.event-filter .active').removeClass('active');
    setCookie('event_filter', filter);
 
    $sender.closest('li').toggleClass('active');
  }
}