debian-mirror-gitlab/app/assets/javascripts/environments/mixins/environments_pagination_api_mixin.js

69 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-03-17 18:26:18 +05:30
/**
* API callbacks for pagination and tabs
*
* Components need to have `scope`, `page` and `requestData`
*/
2020-06-23 00:09:42 +05:30
import { validateParams } from '~/pipelines/utils';
2021-03-11 19:13:27 +05:30
import { historyPushState, buildUrlWithCurrentLocation } from '../../lib/utils/common_utils';
2018-03-17 18:26:18 +05:30
export default {
methods: {
onChangeTab(scope) {
2021-01-03 14:25:43 +05:30
if (this.scope === scope) {
return;
}
2020-05-24 23:13:21 +05:30
let params = {
scope,
page: '1',
2021-04-17 20:07:23 +05:30
nested: true,
2020-05-24 23:13:21 +05:30
};
params = this.onChangeWithFilter(params);
this.updateContent(params);
2018-03-17 18:26:18 +05:30
},
onChangePage(page) {
/* URLS parameters are strings, we need to parse to match types */
2020-05-24 23:13:21 +05:30
let params = {
2018-12-13 13:39:08 +05:30
page: Number(page).toString(),
2021-04-17 20:07:23 +05:30
nested: true,
2018-12-13 13:39:08 +05:30
};
if (this.scope) {
params.scope = this.scope;
}
2020-05-24 23:13:21 +05:30
params = this.onChangeWithFilter(params);
2018-12-13 13:39:08 +05:30
this.updateContent(params);
2018-03-17 18:26:18 +05:30
},
2020-05-24 23:13:21 +05:30
onChangeWithFilter(params) {
2020-06-23 00:09:42 +05:30
return { ...params, ...validateParams(this.requestData) };
2020-05-24 23:13:21 +05:30
},
2018-03-17 18:26:18 +05:30
updateInternalState(parameters) {
// stop polling
this.poll.stop();
2018-12-13 13:39:08 +05:30
const queryString = Object.keys(parameters)
2021-03-08 18:12:59 +05:30
.map((parameter) => {
2018-12-13 13:39:08 +05:30
const value = parameters[parameter];
// update internal state for UI
this[parameter] = value;
return `${parameter}=${encodeURIComponent(value)}`;
})
.join('&');
2018-03-17 18:26:18 +05:30
// update polling parameters
this.requestData = parameters;
historyPushState(buildUrlWithCurrentLocation(`?${queryString}`));
this.isLoading = true;
},
},
};