Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Allow only relatable nodes in autocomplete results

Aaron Contreras0c6c61f7b1f6
diff --git a/app/components/work_package_relations_tab/work_package_relation_form_component.html.erb b/app/components/work_package_relations_tab/work_package_relation_form_component.html.erb
index 1ff439ae8c7..6fdc8cea68d 100644
--- a/app/components/work_package_relations_tab/work_package_relation_form_component.html.erb
+++ b/app/components/work_package_relations_tab/work_package_relation_form_component.html.erb
@@ -18,12 +18,14 @@
# These are not available inside the render_inline_form block
# so we need to re-define them here. Figure out solution for this.
relation = @relation
+ work_package = @work_package
target_work_package = related_work_package
stimulus_controller = STIMULUS_CONTROLLER
i18n_namespace = I18N_NAMESPACE
dialog_id = DIALOG_ID
to_id_field_test_selector = TO_ID_FIELD_TEST_SELECTOR
to_id_field_value = relation.to.present? ? "#{target_work_package.type.name.upcase} ##{target_work_package.id} - #{target_work_package.subject}" : nil
+ url = ::API::V3::Utilities::PathHelper::ApiV3Path.work_package_available_relation_candidates(@work_package.id, type: relation.relation_type_for(@work_package))
render_inline_form(f) do |my_form|
if relation.persisted?
my_form.text_field(
@@ -39,13 +41,13 @@
value: relation.relation_type
)
- my_form.work_package_autocompleter(
+ my_form.autocompleter(
name: :to_id,
label: WorkPackage.model_name.human,
visually_hide_label: false,
autocomplete_options: {
resource: 'work_packages',
- searchKey: 'subjectOrId',
+ url:,
openDirectly: false,
focusDirectly: false,
dropdownPosition: 'bottom',
diff --git a/frontend/src/app/shared/components/autocompleter/op-autocompleter/op-autocompleter.component.ts b/frontend/src/app/shared/components/autocompleter/op-autocompleter/op-autocompleter.component.ts
index 202bbee25fd..668666ed618 100644
--- a/frontend/src/app/shared/components/autocompleter/op-autocompleter/op-autocompleter.component.ts
+++ b/frontend/src/app/shared/components/autocompleter/op-autocompleter/op-autocompleter.component.ts
@@ -24,7 +24,7 @@ import {
} from '@angular/core';
import { DropdownPosition, NgSelectComponent } from '@ng-select/ng-select';
import { BehaviorSubject, merge, NEVER, Observable, of, Subject, timer } from 'rxjs';
-import { debounce, distinctUntilChanged, filter, switchMap, tap } from 'rxjs/operators';
+import { debounce, distinctUntilChanged, filter, switchMap, tap, map } from 'rxjs/operators';
import { AddTagFn, GroupValueFn } from '@ng-select/ng-select/lib/ng-select.component';
import { HalResource } from 'core-app/features/hal/resources/hal-resource';
@@ -49,6 +49,11 @@ import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { ID } from '@datorama/akita';
import { HttpClient } from '@angular/common/http';
import { ApiV3Service } from 'core-app/core/apiv3/api-v3.service';
+import { HalResourceService } from 'core-app/features/hal/services/hal-resource.service';
+import { CollectionResource } from 'core-app/features/hal/resources/collection-resource';
+import { ApiV3FilterBuilder } from 'core-app/shared/helpers/api-v3/api-v3-filter-builder';
+import { addFiltersToPath } from 'core-app/core/apiv3/helpers/add-filters-to-path';
+import { aR } from '@fullcalendar/core/internal-common';
export interface IAutocompleteItem {
id:ID;
@@ -291,6 +296,7 @@ export class OpAutocompleterComponent<T extends IAutocompleteItem = IAutocomplet
readonly ngZone:NgZone,
readonly vcRef:ViewContainerRef,
readonly I18n:I18nService,
+ readonly halResourceService:HalResourceService,
) {
super();
}
@@ -459,9 +465,12 @@ export class OpAutocompleterComponent<T extends IAutocompleteItem = IAutocomplet
filter(() => !!(this.defaultData || this.getOptionsFn)),
distinctUntilChanged(),
tap(() => this.loading$.next(true)),
- // tap(() => console.log('Debounce is ', this.getDebounceTimeout())),
debounce(() => timer(this.getDebounceTimeout())),
switchMap((queryString:string) => {
+ if (this.url) {
+ return this.fetchFromUrl(queryString);
+ }
+
if (this.defaultData) {
return this.opAutocompleterService.loadData(queryString, this.resource, this.filters, this.searchKey);
}
@@ -479,6 +488,39 @@ export class OpAutocompleterComponent<T extends IAutocompleteItem = IAutocomplet
);
}
+ private fetchFromUrl(queryString:string):Observable<unknown> {
+ // Exit early if the query string is empty as there is no typeahead
+ if (queryString === null || queryString.length === 0) {
+ return of([]);
+ }
+
+ // Build filters if provided
+ const finalFilters = new ApiV3FilterBuilder();
+ this.filters?.forEach((currentFilter) => {
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
+ finalFilters.add(currentFilter.name, currentFilter.operator, currentFilter.values);
+ });
+
+ const urlWithFilters = addFiltersToPath(this.url, finalFilters);
+
+ // Add default sort parameters if resource is work packages
+ if (this.resource === 'work_packages') {
+ urlWithFilters.searchParams.set('sortBy', '[["updatedAt","desc"]]');
+ }
+
+ // Add query string to the url if provided
+ urlWithFilters.searchParams.set('query', queryString);
+
+ const stringifiedBuiltOutUrl = urlWithFilters.toString();
+
+ return this
+ .halResourceService
+ .get(stringifiedBuiltOutUrl)
+ .pipe(
+ map((collection:CollectionResource<T>) => collection.elements),
+ );
+ }
+
private getDebounceTimeout():number {
if (this.initialDebounce) {
this.initialDebounce = false;
diff --git a/spec/features/work_packages/details/relations/primerized_relations_spec.rb b/spec/features/work_packages/details/relations/primerized_relations_spec.rb
index 602d0e9ef14..8ea392a835c 100644
--- a/spec/features/work_packages/details/relations/primerized_relations_spec.rb
+++ b/spec/features/work_packages/details/relations/primerized_relations_spec.rb
@@ -294,8 +294,6 @@ RSpec.describe "Primerized work package relations tab",
relations_panel.find("[data-test-selector='new-relation-action-menu']").click
- tabs.expect_counter("relations", 4)
-
within page.find_by_id("new-relation-action-menu-list") do # Primer appends "list" to the menu id automatically
click_link_or_button "Successor (after)"
end
@@ -331,6 +329,34 @@ RSpec.describe "Primerized work package relations tab",
# Bumped by one
tabs.expect_counter("relations", 5)
end
+
+ it "does not autocomplete unrelatable work packages" do
+ # to1 is already related to work_package as relation1
+ # in a successor relation, so it should not be autocompleteable anymore
+ # under the "Successor (after)" type
+ scroll_to_element relations_panel
+
+ relations_panel.find("[data-test-selector='new-relation-action-menu']").click
+
+ within page.find_by_id("new-relation-action-menu-list") do # Primer appends "list" to the menu id automatically
+ click_link_or_button "Successor (after)"
+ end
+
+ wait_for_reload
+
+ within "##{WorkPackageRelationsTab::WorkPackageRelationFormComponent::DIALOG_ID}" do
+ expect(page).to have_text("Add successor (after)")
+ expect(page).to have_button("Add description")
+
+ autocomplete_field = page.find("[data-test-selector='work-package-relation-form-to-id']")
+ search_autocomplete(autocomplete_field,
+ query: to1.subject,
+ results_selector: "body")
+ expect_no_ng_option(autocomplete_field,
+ to1.subject,
+ results_selector: "body")
+ end
+ end
end
describe "attaching a child" do