cycle_analytics_bundle.js 4.42 KB
Newer Older
1 2
/* global Flash */

3
import Vue from 'vue';
4
import Cookies from 'js-cookie';
5
import LimitWarningComponent from './components/limit_warning_component';
6

7 8 9 10 11 12 13 14 15 16 17
require('./components/stage_code_component');
require('./components/stage_issue_component');
require('./components/stage_plan_component');
require('./components/stage_production_component');
require('./components/stage_review_component');
require('./components/stage_staging_component');
require('./components/stage_test_component');
require('./components/total_time_component');
require('./cycle_analytics_service');
require('./cycle_analytics_store');
require('./default_event_objects');
18 19

$(() => {
20
  const OVERVIEW_DIALOG_COOKIE = 'cycle_analytics_help_dismissed';
21 22 23
  const cycleAnalyticsEl = document.querySelector('#cycle-analytics');
  const cycleAnalyticsStore = gl.cycleAnalytics.CycleAnalyticsStore;
  const cycleAnalyticsService = new gl.cycleAnalytics.CycleAnalyticsService({
24 25
    requestPath: cycleAnalyticsEl.dataset.requestPath,
  });
26 27 28 29

  gl.cycleAnalyticsApp = new Vue({
    el: '#cycle-analytics',
    name: 'CycleAnalytics',
30 31 32 33 34
    data: {
      state: cycleAnalyticsStore.state,
      isLoading: false,
      isLoadingStage: false,
      isEmptyStage: false,
35
      hasError: false,
36 37 38 39 40 41 42 43 44 45 46
      startDate: 30,
      isOverviewDialogDismissed: Cookies.get(OVERVIEW_DIALOG_COOKIE),
    },
    computed: {
      currentStage() {
        return cycleAnalyticsStore.currentActiveStage();
      },
    },
    components: {
      'stage-issue-component': gl.cycleAnalytics.StageIssueComponent,
      'stage-plan-component': gl.cycleAnalytics.StagePlanComponent,
47
      'stage-code-component': gl.cycleAnalytics.StageCodeComponent,
48 49 50 51 52
      'stage-test-component': gl.cycleAnalytics.StageTestComponent,
      'stage-review-component': gl.cycleAnalytics.StageReviewComponent,
      'stage-staging-component': gl.cycleAnalytics.StageStagingComponent,
      'stage-production-component': gl.cycleAnalytics.StageProductionComponent,
    },
53 54 55 56
    created() {
      this.fetchCycleAnalyticsData();
    },
    methods: {
57
      handleError() {
58
        cycleAnalyticsStore.setErrorState(true);
59
        return new Flash('There was an error while fetching cycle analytics data.');
60 61 62 63 64 65 66 67
      },
      initDropdown() {
        const $dropdown = $('.js-ca-dropdown');
        const $label = $dropdown.find('.dropdown-label');

        $dropdown.find('li a').off('click').on('click', (e) => {
          e.preventDefault();
          const $target = $(e.currentTarget);
68
          this.startDate = $target.data('value');
69 70

          $label.text($target.text().trim());
71
          this.fetchCycleAnalyticsData({ startDate: this.startDate });
72 73 74
        });
      },
      fetchCycleAnalyticsData(options) {
75
        const fetchOptions = options || { startDate: this.startDate };
76

77
        this.isLoading = true;
78 79

        cycleAnalyticsService
80 81
          .fetchCycleAnalyticsData(fetchOptions)
          .done((response) => {
82
            cycleAnalyticsStore.setCycleAnalyticsData(response);
83
            this.selectDefaultStage();
84 85
            this.initDropdown();
          })
86 87 88 89 90 91 92 93
          .error(() => {
            this.handleError();
          })
          .always(() => {
            this.isLoading = false;
          });
      },
      selectDefaultStage() {
94 95
        const stage = this.state.stages.first();
        this.selectStage(stage);
96 97 98 99 100
      },
      selectStage(stage) {
        if (this.isLoadingStage) return;
        if (this.currentStage === stage) return;

101 102 103 104 105
        if (!stage.isUserAllowed) {
          cycleAnalyticsStore.setActiveStage(stage);
          return;
        }

106
        this.isLoadingStage = true;
107
        cycleAnalyticsStore.setStageEvents([], stage);
108 109 110 111 112 113 114 115
        cycleAnalyticsStore.setActiveStage(stage);

        cycleAnalyticsService
          .fetchStageData({
            stage,
            startDate: this.startDate,
          })
          .done((response) => {
116
            this.isEmptyStage = !response.events.length;
117
            cycleAnalyticsStore.setStageEvents(response.events, stage);
118 119 120
          })
          .error(() => {
            this.isEmptyStage = true;
121 122
          })
          .always(() => {
123
            this.isLoadingStage = false;
124
          });
125 126 127 128 129 130
      },
      dismissOverviewDialog() {
        this.isOverviewDialogDismissed = true;
        Cookies.set(OVERVIEW_DIALOG_COOKIE, '1');
      },
    },
131
  });
132 133

  // Register global components
134
  Vue.component('limit-warning', LimitWarningComponent);
135
  Vue.component('total-time', gl.cycleAnalytics.TotalTimeComponent);
136
});