Commit 5b7b49cc authored by Winnie Hellmann's avatar Winnie Hellmann Committed by Clement Ho

Remove nested Promise from groups select

parent def26df0
...@@ -4,96 +4,97 @@ import Api from './api'; ...@@ -4,96 +4,97 @@ import Api from './api';
import { normalizeHeaders } from './lib/utils/common_utils'; import { normalizeHeaders } from './lib/utils/common_utils';
import { __ } from '~/locale'; import { __ } from '~/locale';
export default function groupsSelect() { const groupsSelect = () => {
import(/* webpackChunkName: 'select2' */ 'select2/select2') // Needs to be accessible in rspec
.then(() => { window.GROUP_SELECT_PER_PAGE = 20;
// Needs to be accessible in rspec $('.ajax-groups-select').each(function setAjaxGroupsSelect2() {
window.GROUP_SELECT_PER_PAGE = 20; const $select = $(this);
$('.ajax-groups-select').each(function setAjaxGroupsSelect2() { const allAvailable = $select.data('allAvailable');
const $select = $(this); const skipGroups = $select.data('skipGroups') || [];
const allAvailable = $select.data('allAvailable'); const parentGroupID = $select.data('parentId');
const skipGroups = $select.data('skipGroups') || []; const groupsPath = parentGroupID
const parentGroupID = $select.data('parentId'); ? Api.subgroupsPath.replace(':id', parentGroupID)
const groupsPath = parentGroupID : Api.groupsPath;
? Api.subgroupsPath.replace(':id', parentGroupID)
: Api.groupsPath;
$select.select2({ $select.select2({
placeholder: __('Search for a group'), placeholder: __('Search for a group'),
allowClear: $select.hasClass('allowClear'), allowClear: $select.hasClass('allowClear'),
multiple: $select.hasClass('multiselect'), multiple: $select.hasClass('multiselect'),
minimumInputLength: 0, minimumInputLength: 0,
ajax: { ajax: {
url: Api.buildUrl(groupsPath), url: Api.buildUrl(groupsPath),
dataType: 'json', dataType: 'json',
quietMillis: 250, quietMillis: 250,
transport(params) { transport(params) {
axios[params.type.toLowerCase()](params.url, { axios[params.type.toLowerCase()](params.url, {
params: params.data, params: params.data,
}) })
.then(res => { .then(res => {
const results = res.data || []; const results = res.data || [];
const headers = normalizeHeaders(res.headers); const headers = normalizeHeaders(res.headers);
const currentPage = parseInt(headers['X-PAGE'], 10) || 0; const currentPage = parseInt(headers['X-PAGE'], 10) || 0;
const totalPages = parseInt(headers['X-TOTAL-PAGES'], 10) || 0; const totalPages = parseInt(headers['X-TOTAL-PAGES'], 10) || 0;
const more = currentPage < totalPages; const more = currentPage < totalPages;
params.success({ params.success({
results, results,
pagination: { pagination: {
more, more,
}, },
}); });
}) })
.catch(params.error); .catch(params.error);
}, },
data(search, page) { data(search, page) {
return { return {
search, search,
page, page,
per_page: window.GROUP_SELECT_PER_PAGE, per_page: window.GROUP_SELECT_PER_PAGE,
all_available: allAvailable, all_available: allAvailable,
}; };
}, },
results(data, page) { results(data, page) {
if (data.length) return { results: [] }; if (data.length) return { results: [] };
const groups = data.length ? data : data.results || []; const groups = data.length ? data : data.results || [];
const more = data.pagination ? data.pagination.more : false; const more = data.pagination ? data.pagination.more : false;
const results = groups.filter(group => skipGroups.indexOf(group.id) === -1); const results = groups.filter(group => skipGroups.indexOf(group.id) === -1);
return { return {
results, results,
page, page,
more, more,
}; };
}, },
}, },
// eslint-disable-next-line consistent-return // eslint-disable-next-line consistent-return
initSelection(element, callback) { initSelection(element, callback) {
const id = $(element).val(); const id = $(element).val();
if (id !== '') { if (id !== '') {
return Api.group(id, callback); return Api.group(id, callback);
} }
}, },
formatResult(object) { formatResult(object) {
return `<div class='group-result'> <div class='group-name'>${object.full_name}</div> <div class='group-path'>${object.full_path}</div> </div>`; return `<div class='group-result'> <div class='group-name'>${object.full_name}</div> <div class='group-path'>${object.full_path}</div> </div>`;
}, },
formatSelection(object) { formatSelection(object) {
return object.full_name; return object.full_name;
}, },
dropdownCssClass: 'ajax-groups-dropdown select2-infinite', dropdownCssClass: 'ajax-groups-dropdown select2-infinite',
// we do not want to escape markup since we are displaying html in results // we do not want to escape markup since we are displaying html in results
escapeMarkup(m) { escapeMarkup(m) {
return m; return m;
}, },
}); });
$select.on('select2-loaded', () => { $select.on('select2-loaded', () => {
const dropdown = document.querySelector('.select2-infinite .select2-results'); const dropdown = document.querySelector('.select2-infinite .select2-results');
dropdown.style.height = `${Math.floor(dropdown.scrollHeight)}px`; dropdown.style.height = `${Math.floor(dropdown.scrollHeight)}px`;
}); });
}); });
}) };
export default () =>
import(/* webpackChunkName: 'select2' */ 'select2/select2')
.then(groupsSelect)
.catch(() => {}); .catch(() => {});
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment