gitlab-org--gitlab-foss/app/assets/javascripts/cycle_analytics.js.es6

98 lines
2.2 KiB
JavaScript
Raw Normal View History

2016-10-18 14:40:16 -04:00
//= require vue
2016-09-14 17:03:42 -04:00
((global) => {
2016-09-19 16:41:36 -04:00
const COOKIE_NAME = 'cycle_analytics_help_dismissed';
const store = gl.cycleAnalyticsStore = {
isLoading: true,
hasError: false,
isHelpDismissed: Cookies.get(COOKIE_NAME),
analytics: {}
};
2016-09-19 16:41:36 -04:00
2016-09-14 17:03:42 -04:00
gl.CycleAnalytics = class CycleAnalytics {
constructor() {
2016-09-19 16:41:36 -04:00
const that = this;
2016-09-14 17:03:42 -04:00
this.vue = new Vue({
el: '#cycle-analytics',
name: 'CycleAnalytics',
created: this.fetchData(),
data: store,
2016-09-19 16:41:36 -04:00
methods: {
dismissLanding() {
that.dismissLanding();
}
}
2016-09-14 17:03:42 -04:00
});
}
2016-09-19 16:41:36 -04:00
fetchData(options) {
store.isLoading = true;
2016-09-19 16:41:36 -04:00
options = options || { startDate: 30 };
$.ajax({
url: $('#cycle-analytics').data('request-path'),
method: 'GET',
dataType: 'json',
contentType: 'application/json',
data: {
cycle_analytics: {
start_date: options.startDate
}
}
2016-09-19 16:41:36 -04:00
}).done((data) => {
this.decorateData(data);
2016-09-19 16:41:36 -04:00
this.initDropdown();
})
.error((data) => {
this.handleError(data);
})
.always(() => {
store.isLoading = false;
2016-09-19 16:41:36 -04:00
})
2016-09-14 17:03:42 -04:00
}
2016-09-19 16:41:36 -04:00
decorateData(data) {
data.summary = data.summary || [];
data.stats = data.stats || [];
data.summary.forEach((item) => {
item.value = item.value || '-';
});
data.stats.forEach((item) => {
item.value = item.value || '- - -';
});
2016-09-19 16:41:36 -04:00
store.analytics = data;
2016-09-14 17:03:42 -04:00
}
handleError(data) {
store.hasError = true;
2016-09-19 16:41:36 -04:00
new Flash('There was an error while fetching cycle analytics data.', 'alert');
}
dismissLanding() {
store.isHelpDismissed = true;
Cookies.set(COOKIE_NAME, true);
2016-09-14 17:03:42 -04:00
}
initDropdown() {
const $dropdown = $('.js-ca-dropdown');
const $label = $dropdown.find('.dropdown-label');
2016-09-19 16:41:36 -04:00
$dropdown.find('li a').off('click').on('click', (e) => {
2016-09-14 17:03:42 -04:00
e.preventDefault();
const $target = $(e.currentTarget);
2016-09-14 17:03:42 -04:00
const value = $target.data('value');
2016-09-14 17:03:42 -04:00
2016-09-14 17:03:42 -04:00
$label.text($target.text().trim());
2016-09-19 16:41:36 -04:00
this.fetchData({ startDate: value });
2016-09-14 17:03:42 -04:00
})
}
2016-09-19 16:41:36 -04:00
2016-09-14 17:03:42 -04:00
}
})(window.gl || (window.gl = {}));