Filter already connected items on repository item relationship modal [SCI-9916] (#6934)

This commit is contained in:
ajugo 2024-01-15 09:41:52 +01:00 committed by GitHub
parent 81fbd38b0c
commit 98ee391d03
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 70 additions and 29 deletions

View file

@ -1,10 +1,10 @@
# frozen_string_literal: true # frozen_string_literal: true
class RepositoryRowConnectionsController < ApplicationController class RepositoryRowConnectionsController < ApplicationController
before_action :load_repository, except: %i(repositories) before_action :load_repository, except: :repositories
before_action :load_create_vars, only: :create before_action :load_create_vars, only: :create
before_action :check_read_permissions, except: :repositories before_action :check_read_permissions, except: :repositories
before_action :load_repository_row, except: %i(repositories repository_rows) before_action :load_repository_row, except: :repositories
before_action :check_manage_permissions, only: %i(create destroy) before_action :check_manage_permissions, only: %i(create destroy)
def index def index
@ -97,11 +97,16 @@ class RepositoryRowConnectionsController < ApplicationController
end end
def repository_rows def repository_rows
repository_rows = @repository.repository_rows selected_repository = Repository.accessible_by_teams(current_team).find(params[:selected_repository_id])
.search_by_name_and_id(current_user, current_user.teams, params[:query])
.order(name: :asc) repository_rows = selected_repository.repository_rows
.page(params[:page] || 1) .where.not(id: @repository_row.id)
.per(Constants::SEARCH_LIMIT) .where.not(id: @repository_row.parent_connections.select(:parent_id))
.where.not(id: @repository_row.child_connections.select(:child_id))
.search_by_name_and_id(current_user, current_user.teams, params[:query])
.order(name: :asc)
.page(params[:page] || 1)
.per(Constants::SEARCH_LIMIT)
render json: { render json: {
data: repository_rows.select(:id, :name, :archived, :repository_id) data: repository_rows.select(:id, :name, :archived, :repository_id)
.preload(:repository) .preload(:repository)

View file

@ -130,15 +130,13 @@
import SelectSearch from '../shared/select_search.vue'; import SelectSearch from '../shared/select_search.vue';
import ChecklistSearch from '../shared/checklist_search.vue'; import ChecklistSearch from '../shared/checklist_search.vue';
import Select from '../shared/select.vue'; import Select from '../shared/select.vue';
import ChecklistSelect from '../shared/checklist_select.vue';
export default { export default {
name: 'RepositoryItemRelationshipsModal', name: 'RepositoryItemRelationshipsModal',
components: { components: {
'select-search': SelectSearch, 'select-search': SelectSearch,
ChecklistSearch, ChecklistSearch,
Select, Select
'checklist-select': ChecklistSelect
}, },
created() { created() {
window.repositoryItemRelationshipsModal = this; window.repositoryItemRelationshipsModal = this;
@ -174,7 +172,10 @@ export default {
this.loadingInventories = true; this.loadingInventories = true;
$.ajax({ $.ajax({
url: `${this.inventoriesUrl}?page=${this.nextInventoriesPage}`, url: this.inventoriesUrl,
data: {
page: this.nextInventoriesPage
},
success: (result) => { success: (result) => {
this.inventoryOptions = this.inventoryOptions.concat(result.data.map((val) => [val.id, val.name])); this.inventoryOptions = this.inventoryOptions.concat(result.data.map((val) => [val.id, val.name]));
this.loadingInventories = false; this.loadingInventories = false;
@ -187,7 +188,11 @@ export default {
this.loadingItems = true; this.loadingItems = true;
$.ajax({ $.ajax({
url: `${this.inventoryItemsUrl}/?page=${this.nextItemsPage}&repository_id=${inventoryValue}`, url: this.inventoryItemsUrl,
data: {
page: this.nextItemsPage,
selected_repository_id: inventoryValue
},
success: (result) => { success: (result) => {
this.itemOptions = this.itemOptions.concat(result.data.map((val) => ({ id: val.id, label: val.name }))); this.itemOptions = this.itemOptions.concat(result.data.map((val) => ({ id: val.id, label: val.name })));
this.loadingItems = false; this.loadingItems = false;
@ -237,7 +242,7 @@ export default {
this.nextItemsPage = 1; this.nextItemsPage = 1;
if (value) { if (value) {
this.loadingItems = true; this.loadingItems = true;
this.itemParams = [`repository_id=${value}`]; this.itemParams = { selected_repository_id: value };
} }
this.$nextTick(() => { this.$nextTick(() => {
this.fetchInventoryItems(value); this.fetchInventoryItems(value);

View file

@ -81,7 +81,7 @@ export default {
return; return;
} }
if (this.optionsUrlValue) { if (this.optionsUrl) {
// reset current options and next page when lazy loading is enabled // reset current options and next page when lazy loading is enabled
if (this.lazyLoadEnabled) { if (this.lazyLoadEnabled) {
this.currentOptions = []; this.currentOptions = [];
@ -106,14 +106,6 @@ export default {
return this.currentOptions.find(({ id }) => id === this.selectedValues[0])?.label; return this.currentOptions.find(({ id }) => id === this.selectedValues[0])?.label;
} }
return `${this.selectedValues.length} ${this.i18n.t('general.options_selected')}`; return `${this.selectedValues.length} ${this.i18n.t('general.options_selected')}`;
},
optionsUrlValue() {
if (!this.optionsUrl) return '';
let url = `${this.optionsUrl}?query=${this.query || ''}`;
if (this.lazyLoadEnabled && this.nextPage) url = `${url}&page=${this.nextPage}`;
if (this.params.length) url = `${url}&${this.params.join('&')}`;
return url;
} }
}, },
methods: { methods: {
@ -128,7 +120,12 @@ export default {
if (!this.nextPage || !this.optionsUrl) return; if (!this.nextPage || !this.optionsUrl) return;
$.ajax({ $.ajax({
url: this.optionsUrlValue, url: this.optionsUrl,
data: {
...(this.query && { query: this.query }),
...(this.lazyLoadEnabled && this.nextPage && { page: this.nextPage }),
...this.params
},
success: (result) => { success: (result) => {
if (this.lazyLoadEnabled) { if (this.lazyLoadEnabled) {
this.nextPage = result.next_page; this.nextPage = result.next_page;

View file

@ -31,7 +31,8 @@ json.actions do
end end
json.row_connections do json.row_connections do
json.inventories_url repository_row_connections_repositories_url json.inventories_url repository_row_connections_repositories_url
json.inventory_items_url repository_row_connections_repository_rows_url json.inventory_items_url repository_rows_repository_repository_row_repository_row_connections_path(@repository,
@repository_row)
json.create_url repository_repository_row_repository_row_connections_url(@repository, @repository_row) json.create_url repository_repository_row_repository_row_connections_url(@repository, @repository_row)
end end
end end

View file

@ -735,7 +735,11 @@ Rails.application.routes.draw do
get :actions_toolbar get :actions_toolbar
end end
resources :repository_row_connections, only: %i(index create destroy) resources :repository_row_connections, only: %i(index create destroy) do
collection do
get :repository_rows
end
end
member do member do
get 'repository_stock_value/new', to: 'repository_stock_values#new', as: 'new_repository_stock' get 'repository_stock_value/new', to: 'repository_stock_values#new', as: 'new_repository_stock'
get 'repository_stock_value/edit', to: 'repository_stock_values#edit', as: 'edit_repository_stock' get 'repository_stock_value/edit', to: 'repository_stock_values#edit', as: 'edit_repository_stock'
@ -792,7 +796,6 @@ Rails.application.routes.draw do
namespace :repository_row_connections do namespace :repository_row_connections do
get :repositories get :repositories
get :repository_rows
end end
resources :connected_devices, controller: 'users/connected_devices', only: %i(destroy) resources :connected_devices, controller: 'users/connected_devices', only: %i(destroy)

View file

@ -123,14 +123,44 @@ describe RepositoryRowConnectionsController, type: :controller do
end end
describe 'GET #repository_rows' do describe 'GET #repository_rows' do
let!(:child_repository_row) { create :repository_row, repository: repository }
let!(:parent_repository_row) { create :repository_row, repository: repository }
let!(:child_connection) do
create :repository_row_connection,
parent: repository_row,
child: child_repository_row,
created_by: user,
last_modified_by: user
end
let!(:parent_connection) do
create :repository_row_connection,
child: repository_row,
parent: parent_repository_row,
created_by: user,
last_modified_by: user
end
it 'returns a successful response' do it 'returns a successful response' do
get :repository_rows, format: :json, params: { repository_id: repository.id } get :repository_rows, format: :json, params: {
repository_id: repository.id,
repository_row_id: repository_row.id,
selected_repository_id: repository.id
}
expect(response).to have_http_status(:success) expect(response).to have_http_status(:success)
end end
it 'returns the correct data structure' do it 'returns the correct data structure' do
get :repository_rows, format: :json, params: { repository_id: repository.id } get :repository_rows, format: :json, params: {
expect(response.body).to include(repository_row.name) repository_id: repository.id,
repository_row_id: repository_row.id,
selected_repository_id: repository.id
}
expect(response.body).not_to include(repository_row.name)
expect(response.body).not_to include(child_repository_row.name)
expect(response.body).not_to include(parent_repository_row.name)
expect(response.body).to include(other_repository_row.name)
end end
end end
end end