Filter projects by group on search page

After selecting a group, the projects dropdown will now filter by group projects instead

Closes #29489
This commit is contained in:
Phil Hughes 2017-05-08 12:42:42 +01:00
parent 59c9ec5ce6
commit d891e5f731
4 changed files with 50 additions and 9 deletions

View file

@ -1,4 +1,5 @@
/* eslint-disable func-names, space-before-function-paren, wrap-iife, no-var, one-var, one-var-declaration-per-line, object-shorthand, prefer-arrow-callback, comma-dangle, prefer-template, quotes, no-else-return, max-len */ /* eslint-disable func-names, space-before-function-paren, wrap-iife, no-var, one-var, one-var-declaration-per-line, object-shorthand, prefer-arrow-callback, comma-dangle, prefer-template, quotes, no-else-return, max-len */
/* global Flash */
/* global Api */ /* global Api */
(function() { (function() {
@ -7,6 +8,7 @@
var $groupDropdown, $projectDropdown; var $groupDropdown, $projectDropdown;
$groupDropdown = $('.js-search-group-dropdown'); $groupDropdown = $('.js-search-group-dropdown');
$projectDropdown = $('.js-search-project-dropdown'); $projectDropdown = $('.js-search-project-dropdown');
this.groupId = $groupDropdown.data('group-id');
this.eventListeners(); this.eventListeners();
$groupDropdown.glDropdown({ $groupDropdown.glDropdown({
selectable: true, selectable: true,
@ -46,14 +48,18 @@
search: { search: {
fields: ['name'] fields: ['name']
}, },
data: function(term, callback) { data: (term, callback) => {
return Api.projects(term, { order_by: 'id' }, function(data) { this.getProjectsData(term)
.then((data) => {
data.unshift({ data.unshift({
name_with_namespace: 'Any' name_with_namespace: 'Any'
}); });
data.splice(1, 0, 'divider'); data.splice(1, 0, 'divider');
return callback(data);
}); return data;
})
.then(data => callback(data))
.catch(() => new Flash('Error fetching projects'));
}, },
id: function(obj) { id: function(obj) {
return obj.id; return obj.id;
@ -95,6 +101,18 @@
return $('.js-search-input').val('').trigger('keyup').focus(); return $('.js-search-input').val('').trigger('keyup').focus();
}; };
Search.prototype.getProjectsData = function(term) {
return new Promise((resolve) => {
if (this.groupId) {
Api.groupProjects(this.groupId, term, resolve);
} else {
Api.projects(term, {
order_by: 'id',
}, resolve);
}
});
};
return Search; return Search;
})(); })();
}).call(window); }).call(window);

View file

@ -3,7 +3,7 @@
- if params[:project_id].present? - if params[:project_id].present?
= hidden_field_tag :project_id, params[:project_id] = hidden_field_tag :project_id, params[:project_id]
.dropdown .dropdown
%button.dropdown-menu-toggle.js-search-group-dropdown{ type: "button", data: { toggle: "dropdown", default_label: "Group:" } } %button.dropdown-menu-toggle.js-search-group-dropdown{ type: "button", data: { toggle: "dropdown", default_label: "Group:", group_id: params[:group_id] } }
%span.dropdown-toggle-text %span.dropdown-toggle-text
Group: Group:
- if @group.present? - if @group.present?

View file

@ -0,0 +1,4 @@
---
title: Restricts search projects dropdown to group projects when group is selected
merge_request:
author:

View file

@ -20,6 +20,7 @@ describe "Search", feature: true do
context 'search filters', js: true do context 'search filters', js: true do
let(:group) { create(:group) } let(:group) { create(:group) }
let!(:group_project) { create(:empty_project, group: group) }
before do before do
group.add_owner(user) group.add_owner(user)
@ -36,6 +37,24 @@ describe "Search", feature: true do
expect(find('.js-search-group-dropdown')).to have_content(group.name) expect(find('.js-search-group-dropdown')).to have_content(group.name)
end end
it 'filters by group projects after filtering by group' do
find('.js-search-group-dropdown').click
wait_for_ajax
page.within '.search-holder' do
click_link group.name
end
expect(find('.js-search-group-dropdown')).to have_content(group.name)
page.within('.project-filter') do
find('.js-search-project-dropdown').click
wait_for_ajax
expect(page).to have_link(group_project.name_with_namespace)
end
end
it 'shows project name after filtering' do it 'shows project name after filtering' do
page.within('.project-filter') do page.within('.project-filter') do
find('.js-search-project-dropdown').click find('.js-search-project-dropdown').click