All files / ee/app/assets/javascripts/analytics/productivity_analytics index.js

0% Statements 0/49
0% Branches 0/18
0% Functions 0/9
0% Lines 0/49

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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202                                                                                                                                                                                                                                                                                                                                                                                                                   
import Vue from 'vue';
import VueApollo from 'vue-apollo';
// eslint-disable-next-line no-restricted-imports
import { mapState, mapActions } from 'vuex';
import DateRange from '~/analytics/shared/components/daterange.vue';
import { getIdFromGraphQLId } from '~/graphql_shared/utils';
import createDefaultClient from '~/lib/graphql';
import { queryToObject } from '~/lib/utils/url_utility';
import { buildGroupFromDataset, buildProjectFromDataset } from '../shared/utils';
import ProductivityAnalyticsApp from './components/app.vue';
import FilterDropdowns from './components/filter_dropdowns.vue';
import FilteredSearchProductivityAnalytics from './filtered_search_productivity_analytics';
import store from './store';
import { getLabelsEndpoint, getMilestonesEndpoint } from './utils';
 
Vue.use(VueApollo);
 
const apolloProvider = new VueApollo({
  defaultClient: createDefaultClient(),
});
 
export default () => {
  const container = document.getElementById('js-productivity-analytics');
  const groupProjectSelectContainer = container.querySelector('.js-group-project-select-container');
  const searchBarContainer = container.querySelector('.js-search-bar');
 
  // we need to store the HTML content so we can reset it later
  const issueFilterHtml = searchBarContainer.querySelector('.issues-filters').innerHTML;
  const timeframeContainer = container.querySelector('.js-timeframe-container');
  const appContainer = container.querySelector('.js-productivity-analytics-app-container');
 
  const { mergedAfter, mergedBefore } = container.dataset;
 
  const mergedAfterDate = new Date(mergedAfter);
  const mergedBeforeDate = new Date(mergedBefore);
 
  const { endpoint, emptyStateSvgPath, noAccessSvgPath } = appContainer.dataset;
  const minDate = timeframeContainer.dataset.startDate
    ? new Date(timeframeContainer.dataset.startDate)
    : null;
 
  const group = buildGroupFromDataset(container.dataset);
 
  let project = null;
 
  let initialData = {
    mergedAfter: mergedAfterDate,
    mergedBefore: mergedBeforeDate,
    minDate,
  };
 
  // let's set the initial data (from URL query params) only if we receive a valid group from BE
  if (group) {
    project = buildProjectFromDataset(container.dataset);
 
    const {
      authorUsername,
      labelName,
      milestoneTitle,
      'not[author_username]': notAuthorUsername,
      'not[milestone_title]': notMilestoneTitle,
      'not[label_name][]': notLabelName,
    } = queryToObject(window.location.search);
 
    initialData = {
      ...initialData,
      groupNamespace: group.full_path,
      projectPath: project ? project.path_with_namespace : null,
      authorUsername,
      labelName,
      milestoneTitle,
      notAuthorUsername,
      notLabelName,
      notMilestoneTitle,
    };
  }
 
  let filterManager;
 
  // eslint-disable-next-line no-new
  new Vue({
    el: groupProjectSelectContainer,
    apolloProvider,
    store,
    created() {
      // let's not fetch any data by default since we might not have a valid group yet
      let skipFetch = true;
 
      this.setEndpoint(endpoint);
 
      if (group) {
        this.initFilteredSearch({
          groupNamespace: group.full_path,
          groupId: group.id,
          projectNamespace: project?.path_with_namespace || null,
          projectId: container.dataset.projectId || null,
        });
 
        // let's fetch data now since we do have a valid group
        skipFetch = false;
      }
 
      this.setInitialData({ skipFetch, data: initialData });
    },
    methods: {
      ...mapActions(['setEndpoint']),
      ...mapActions('filters', ['setInitialData']),
      onGroupSelected({ groupNamespace, groupId }) {
        this.initFilteredSearch({ groupNamespace, groupId });
      },
      onProjectSelected({ groupNamespace, groupId, projectNamespace, projectId }) {
        this.initFilteredSearch({
          groupNamespace,
          groupId,
          projectNamespace,
          projectId: getIdFromGraphQLId(projectId),
        });
      },
      initFilteredSearch({ groupNamespace, groupId, projectNamespace = '', projectId = null }) {
        // let's unbind attached event handlers first and reset the template
        if (filterManager) {
          filterManager.cleanup();
 
          // eslint-disable-next-line no-unsanitized/property
          searchBarContainer.innerHTML = issueFilterHtml;
        }
 
        searchBarContainer.classList.remove('hide');
 
        const filteredSearchInput = searchBarContainer.querySelector('.filtered-search');
        const labelsEndpoint = getLabelsEndpoint(groupNamespace, projectNamespace);
        const milestonesEndpoint = getMilestonesEndpoint(groupNamespace, projectNamespace);
 
        filteredSearchInput.dataset.groupId = groupId;
 
        if (projectId) {
          filteredSearchInput.dataset.projectId = projectId;
        }
 
        filteredSearchInput.dataset.labelsEndpoint = labelsEndpoint;
        filteredSearchInput.dataset.milestonesEndpoint = milestonesEndpoint;
        filterManager = new FilteredSearchProductivityAnalytics({ isGroup: false });
        filterManager.setup();
      },
    },
    render(h) {
      return h(FilterDropdowns, {
        props: {
          group,
          project,
        },
        on: {
          groupSelected: this.onGroupSelected,
          projectSelected: this.onProjectSelected,
        },
      });
    },
  });
 
  // eslint-disable-next-line no-new
  new Vue({
    el: timeframeContainer,
    store,
    computed: {
      ...mapState('filters', ['groupNamespace', 'startDate', 'endDate']),
    },
    methods: {
      ...mapActions('filters', ['setDateRange']),
      onDateRangeChange({ startDate, endDate }) {
        this.setDateRange({ startDate, endDate });
      },
    },
    render(h) {
      return h(DateRange, {
        props: {
          show: this.groupNamespace !== null,
          startDate: mergedAfterDate,
          endDate: mergedBeforeDate,
          minDate,
        },
        on: {
          change: this.onDateRangeChange,
        },
      });
    },
  });
 
  // eslint-disable-next-line no-new
  new Vue({
    el: appContainer,
    store,
    render(h) {
      return h(ProductivityAnalyticsApp, {
        props: {
          emptyStateSvgPath,
          noAccessSvgPath,
        },
      });
    },
  });
};