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

Trigger relations count re-rendering on Turbo <-> Angular sources

Aaron Contrerasc0c4b14d20d0
diff --git a/frontend/src/app/features/work-packages/components/wp-relations/wp-relations.component.ts b/frontend/src/app/features/work-packages/components/wp-relations/wp-relations.component.ts
index 1f86f8b6c4f..c0526237657 100644
--- a/frontend/src/app/features/work-packages/components/wp-relations/wp-relations.component.ts
+++ b/frontend/src/app/features/work-packages/components/wp-relations/wp-relations.component.ts
@@ -27,7 +27,7 @@
//++
import {
- ChangeDetectionStrategy, ChangeDetectorRef, Component, Input, OnInit, ViewChild, ElementRef,
+ ChangeDetectionStrategy, ChangeDetectorRef, Component, Input, OnInit, ViewChild, ElementRef, AfterViewInit,
} from '@angular/core';
import { I18nService } from 'core-app/core/i18n/i18n.service';
import { WorkPackageResource } from 'core-app/features/hal/resources/work-package-resource';
@@ -42,22 +42,19 @@ import { RelatedWorkPackagesGroup } from './wp-relations.interfaces';
import { PathHelperService } from 'core-app/core/path-helper/path-helper.service';
import { TurboRequestsService } from 'core-app/core/turbo/turbo-requests.service';
import { renderStreamMessage } from '@hotwired/turbo';
-
-interface TurboFrameElement extends HTMLElement {
- src:string;
-}
+import { HalEventsService } from 'core-app/features/hal/services/hal-events.service';
@Component({
selector: 'wp-relations',
changeDetection: ChangeDetectionStrategy.OnPush,
templateUrl: './wp-relations.template.html',
})
-export class WorkPackageRelationsComponent extends UntilDestroyedMixin implements OnInit {
+export class WorkPackageRelationsComponent extends UntilDestroyedMixin implements OnInit, AfterViewInit {
@Input() public workPackage:WorkPackageResource;
public relationGroups:RelatedWorkPackagesGroup = {};
- @ViewChild('#work-package-relations-tab-content') readonly relationTurboFrame:TurboFrameElement;
+ @ViewChild('frameElement') readonly relationTurboFrame:ElementRef<HTMLIFrameElement>;
public relationGroupKeys:string[] = [];
@@ -81,6 +78,7 @@ export class WorkPackageRelationsComponent extends UntilDestroyedMixin implement
private wpRelations:WorkPackageRelationsService,
private cdRef:ChangeDetectorRef,
private apiV3Service:ApiV3Service,
+ private halEvents:HalEventsService,
private PathHelper:PathHelperService,
private turboRequests:TurboRequestsService,
) {
@@ -89,6 +87,7 @@ export class WorkPackageRelationsComponent extends UntilDestroyedMixin implement
ngOnInit() {
this.turboFrameSrc = `${this.PathHelper.staticBase}/work_packages/${this.workPackage.id}/relations_tab`;
+
this.canAddRelation = !!this.workPackage.addRelation;
this.wpRelations
@@ -100,8 +99,6 @@ export class WorkPackageRelationsComponent extends UntilDestroyedMixin implement
.subscribe((relations:RelationsStateValue) => {
this.loadedRelations(relations);
// Refresh the turbo frame
- // eslint-disable-next-line no-self-assign
- this.relationTurboFrame.src = this.relationTurboFrame.src;
void this.turboRequests.requestStream(this.turboFrameSrc)
.then((html) => {
// eslint-disable-next-line no-self-assign
@@ -130,6 +127,43 @@ export class WorkPackageRelationsComponent extends UntilDestroyedMixin implement
});
}
+ ngAfterViewInit() {
+ /*
+ We globally listen for turbo:submit-end events to know when a form submission ends.
+ If the form action URL contains 'relation' or 'child', we know it is a relation or child creation/update.
+ In this case, we reload the relations state and push an updated event to the Hal resource
+ and the wpRelations service.
+
+ The reason for this workaround is that the form submissions occur in top layer
+ as they originate from dialog elements or turbo confirm elements. Because of this, we
+ cannot listen to the submit end event on the relationTurboFrame element and have
+ to rely on the form action URL.
+ */
+ document.addEventListener('turbo:submit-end', (event:CustomEvent) => {
+ const form = event.target as HTMLFormElement;
+ const requestIsWithinFrame = form.action?.includes('relation') || form.action?.includes('child');
+
+ if (requestIsWithinFrame) {
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
+ const { fetchResponse } = event.detail;
+
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
+ if (fetchResponse?.succeeded) {
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call
+
+ // Update the work package
+ void this.apiV3Service
+ .work_packages
+ .id(this.workPackage.id!)
+ .refresh();
+ this.halEvents.push(this.workPackage, { eventType: 'updated' });
+ // Refetch relations
+ void this.wpRelations.require(this.workPackage.id!, true);
+ }
+ }
+ });
+ }
+
private getRelatedWorkPackages(workPackageIds:string[]):Observable<WorkPackageResource[]> {
const observablesToGetZipped:Observable<WorkPackageResource>[] = workPackageIds.map((wpId) => this
.apiV3Service
diff --git a/frontend/src/app/features/work-packages/components/wp-relations/wp-relations.template.html b/frontend/src/app/features/work-packages/components/wp-relations/wp-relations.template.html
index 42f371d7bf2..544812414cb 100644
--- a/frontend/src/app/features/work-packages/components/wp-relations/wp-relations.template.html
+++ b/frontend/src/app/features/work-packages/components/wp-relations/wp-relations.template.html
@@ -1,4 +1,8 @@
-<turbo-frame [src]="turboFrameSrc" id="work-package-relations-tab-content">
+<turbo-frame
+ #frameElement
+ [attr.src]="this.turboFrameSrc"
+ id="work-package-relations-tab-content"
+>
<op-content-loader viewBox="0 0 100 100">
<svg:rect x="0" y="0" width="70" height="5" rx="1" />
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 ad245e3892c..602d0e9ef14 100644
--- a/spec/features/work_packages/details/relations/primerized_relations_spec.rb
+++ b/spec/features/work_packages/details/relations/primerized_relations_spec.rb
@@ -104,6 +104,8 @@ RSpec.describe "Primerized work package relations tab",
scroll_to_element relations_panel
expect(page).to have_css(relations_panel_selector)
+ tabs.expect_counter("relations", 4)
+
target = relation1.to == work_package ? "from" : "to"
target_relation_type = target == "from" ? relation1.reverse_type : relation1.relation_type
@@ -183,6 +185,8 @@ RSpec.describe "Primerized work package relations tab",
end
expect { relation1.reload }.to raise_error(ActiveRecord::RecordNotFound)
+
+ tabs.expect_counter("relations", 3)
end
it "can delete children" do
@@ -203,6 +207,8 @@ RSpec.describe "Primerized work package relations tab",
end
expect(child_wp.reload.parent).to be_nil
+
+ tabs.expect_counter("relations", 3)
end
end
@@ -239,6 +245,9 @@ RSpec.describe "Primerized work package relations tab",
expect(page).to have_text("Discovered relations have descriptions!")
end
+ # Unchanged
+ tabs.expect_counter("relations", 4)
+
# Edit again
within(relation_row) do
page.find("[data-test-selector='op-relation-row-#{relation1.id}-action-menu']").click
@@ -262,6 +271,9 @@ RSpec.describe "Primerized work package relations tab",
within(relation_row) do
expect(page).to have_text("And they can be edited!")
end
+
+ # Unchanged
+ tabs.expect_counter("relations", 4)
end
it "does not have an edit action for children" do
@@ -282,6 +294,8 @@ 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
@@ -313,6 +327,9 @@ RSpec.describe "Primerized work package relations tab",
expect(new_relation_row).to have_text(to3.subject)
expect(new_relation_row).to have_text("Discovered relations have descriptions!")
end
+
+ # Bumped by one
+ tabs.expect_counter("relations", 5)
end
end
@@ -322,6 +339,8 @@ 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 "Child"
end
@@ -348,6 +367,9 @@ RSpec.describe "Primerized work package relations tab",
child_row = page.find("[data-test-selector='op-relation-row-#{not_yet_child_wp.id}']")
expect(child_row).to have_text(not_yet_child_wp.subject)
end
+
+ # Bumped by one
+ tabs.expect_counter("relations", 5)
end
end
end