From be4abe7719a500852ce4f913d345e9b3e8794dc3 Mon Sep 17 00:00:00 2001 From: Filipa Lacerda Date: Tue, 14 Nov 2017 15:35:29 +0000 Subject: Stops page reload when changing tabs or pages - uses API requests instead --- app/assets/javascripts/lib/utils/common_utils.js | 36 +++++ app/assets/javascripts/lib/utils/poll.js | 8 +- .../pipelines/components/navigation_tabs.vue | 87 +++-------- .../javascripts/pipelines/components/pipelines.vue | 163 ++++++++++++++------- app/views/projects/pipelines/index.html.haml | 6 - .../unreleased/32098-pipelines-navigation.yml | 6 + spec/features/projects/pipelines/pipelines_spec.rb | 26 +++- spec/javascripts/fixtures/pipelines.html.haml | 8 +- spec/javascripts/lib/utils/common_utils_spec.js | 30 ++++ spec/javascripts/lib/utils/poll_spec.js | 6 +- spec/javascripts/pipelines/navigation_tabs_spec.js | 128 ++++------------ spec/javascripts/pipelines/pipelines_spec.js | 133 +++++++++++++++-- 12 files changed, 385 insertions(+), 252 deletions(-) create mode 100644 changelogs/unreleased/32098-pipelines-navigation.yml diff --git a/app/assets/javascripts/lib/utils/common_utils.js b/app/assets/javascripts/lib/utils/common_utils.js index 5c4926d6ac8..195e2ca6a78 100644 --- a/app/assets/javascripts/lib/utils/common_utils.js +++ b/app/assets/javascripts/lib/utils/common_utils.js @@ -309,6 +309,42 @@ export const setParamInURL = (param, value) => { return search; }; +/** + * Given a string of query parameters creates an object. + * + * @example + * `scope=all&page=2` -> { scope: 'all', page: '2'} + * `scope=all` -> { scope: 'all' } + * ``-> {} + * @param {String} query + * @returns {Object} + */ +export const parseQueryStringIntoObject = (query = '') => { + if (query === '') return {}; + + return query + .split('&') + .reduce((acc, element) => { + const val = element.split('='); + Object.assign(acc, { + [val[0]]: decodeURIComponent(val[1]), + }); + return acc; + }, {}); +}; + +export const buildUrlWithCurrentLocation = param => (param ? `${window.location.pathname}${param}` : window.location.pathname); + +/** + * Based on the current location and the string parameters provided + * creates a new entry in the history without reloading the page. + * + * @param {String} param + */ +export const historyPushState = (newUrl) => { + window.history.pushState({}, document.title, newUrl); +}; + /** * Converts permission provided as strings to booleans. * diff --git a/app/assets/javascripts/lib/utils/poll.js b/app/assets/javascripts/lib/utils/poll.js index 1485e900945..65a8cf2c891 100644 --- a/app/assets/javascripts/lib/utils/poll.js +++ b/app/assets/javascripts/lib/utils/poll.js @@ -60,7 +60,6 @@ export default class Poll { checkConditions(response) { const headers = normalizeHeaders(response.headers); const pollInterval = parseInt(headers[this.intervalHeader], 10); - if (pollInterval > 0 && response.status === httpStatusCodes.OK && this.canPoll) { this.timeoutID = setTimeout(() => { this.makeRequest(); @@ -102,7 +101,12 @@ export default class Poll { /** * Restarts polling after it has been stoped */ - restart() { + restart(options) { + // update data + if (options && options.data) { + this.options.data = options.data; + } + this.canPoll = true; this.makeRequest(); } diff --git a/app/assets/javascripts/pipelines/components/navigation_tabs.vue b/app/assets/javascripts/pipelines/components/navigation_tabs.vue index 73f7e3a0cad..07befd23500 100644 --- a/app/assets/javascripts/pipelines/components/navigation_tabs.vue +++ b/app/assets/javascripts/pipelines/components/navigation_tabs.vue @@ -2,16 +2,8 @@ export default { name: 'PipelineNavigationTabs', props: { - scope: { - type: String, - required: true, - }, - count: { - type: Object, - required: true, - }, - paths: { - type: Object, + tabs: { + type: Array, required: true, }, }, @@ -23,68 +15,37 @@ // 0 is valid in a badge, but evaluates to false, we need to check for undefined return count !== undefined; }, + + onTabClick(tab) { + this.$emit('onChangeTab', tab.scope); + }, }, }; diff --git a/app/assets/javascripts/pipelines/components/pipelines.vue b/app/assets/javascripts/pipelines/components/pipelines.vue index 3da60e88474..cf241c8ffed 100644 --- a/app/assets/javascripts/pipelines/components/pipelines.vue +++ b/app/assets/javascripts/pipelines/components/pipelines.vue @@ -1,10 +1,17 @@