Skip to content

Commit

Permalink
Remove the turboEventListener when the component is destroyed to avoi…
Browse files Browse the repository at this point in the history
…d unneccessary reloads
  • Loading branch information
HDinger committed Dec 2, 2024
1 parent bc6a30e commit e2f4304
Showing 1 changed file with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
Component,
ElementRef,
Input,
OnDestroy,
OnInit,
ViewChild,
} from '@angular/core';
Expand All @@ -56,13 +57,15 @@ import {
changeDetection: ChangeDetectionStrategy.OnPush,
templateUrl: './wp-relations.template.html',
})
export class WorkPackageRelationsComponent extends UntilDestroyedMixin implements OnInit, AfterViewInit {
export class WorkPackageRelationsComponent extends UntilDestroyedMixin implements OnInit, AfterViewInit, OnDestroy {
@Input() public workPackage:WorkPackageResource;

@ViewChild('frameElement') readonly relationTurboFrame:ElementRef<HTMLIFrameElement>;

turboFrameSrc:string;

private turboFrameListener:EventListener = this.updateFrontendData.bind(this);

constructor(
private wpRelations:WorkPackageRelationsService,
private apiV3Service:ApiV3Service,
Expand All @@ -77,6 +80,12 @@ export class WorkPackageRelationsComponent extends UntilDestroyedMixin implement
this.turboFrameSrc = `${this.PathHelper.staticBase}/work_packages/${this.workPackage.id}/relations_tab`;
}

ngOnDestroy() {
super.ngOnDestroy();

document.removeEventListener('turbo:submit-end', this.turboFrameListener);
}

ngAfterViewInit() {
// Listen to any changes to the relations and update the frame
this
Expand Down Expand Up @@ -104,7 +113,16 @@ export class WorkPackageRelationsComponent extends UntilDestroyedMixin implement
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) => {
document.addEventListener('turbo:submit-end', this.turboFrameListener);
}

public updateCounter() {
const url = this.PathHelper.workPackageUpdateCounterPath(this.workPackage.id!, 'relations');
void this.turboRequests.request(url);
}

private updateFrontendData(event:CustomEvent) {
if (event) {
const form = event.target as HTMLFormElement;
const updateWorkPackage = !!form.dataset?.updateWorkPackage;

Expand All @@ -124,12 +142,7 @@ export class WorkPackageRelationsComponent extends UntilDestroyedMixin implement
this.updateCounter();
}
}
});
}

public updateCounter() {
const url = this.PathHelper.workPackageUpdateCounterPath(this.workPackage.id!, 'relations');
void this.turboRequests.request(url);
}
}

private updateRelationsTab() {
Expand Down

0 comments on commit e2f4304

Please sign in to comment.