From e8c8de89b9f6623c15335675b0b74d1bf8675d95 Mon Sep 17 00:00:00 2001 From: Tobias Dillmann Date: Wed, 25 Sep 2024 16:26:25 +0200 Subject: [PATCH] [#57904] use targetConnected event in Stimulus --- .../controllers/poll-for-changes.controller.ts | 14 ++++++++------ .../meetings/header_component.html.erb | 17 +++++++---------- .../meetings/update_flash_component.rb | 4 ++-- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/frontend/src/stimulus/controllers/poll-for-changes.controller.ts b/frontend/src/stimulus/controllers/poll-for-changes.controller.ts index fb49f49f888e..811dc91926fd 100644 --- a/frontend/src/stimulus/controllers/poll-for-changes.controller.ts +++ b/frontend/src/stimulus/controllers/poll-for-changes.controller.ts @@ -39,6 +39,10 @@ export default class PollForChangesController extends ApplicationController { autoscrollEnabled: Boolean, }; + static targets = ['reloadButton']; + + declare reloadButtonTarget:HTMLLinkElement; + declare referenceValue:string; declare urlValue:string; declare intervalValue:number; @@ -56,7 +60,6 @@ export default class PollForChangesController extends ApplicationController { } if (this.autoscrollEnabledValue) { - window.addEventListener('beforeunload', this.rememberCurrentScrollPosition.bind(this)); window.addEventListener('DOMContentLoaded', this.autoscrollToLastKnownPosition.bind(this)); } } @@ -66,6 +69,10 @@ export default class PollForChangesController extends ApplicationController { clearInterval(this.interval); } + reloadButtonTargetConnected() { + this.reloadButtonTarget.addEventListener('click', this.rememberCurrentScrollPosition.bind(this)); + } + triggerTurboStream() { void fetch(`${this.urlValue}?reference=${this.referenceValue}`, { headers: { @@ -90,11 +97,6 @@ export default class PollForChangesController extends ApplicationController { } autoscrollToLastKnownPosition() { - const params = new URLSearchParams(window.location.search); - if (params.get('autoscroll') !== 'true') { - return; - } - const lastKnownPos = sessionStorage.getItem(this.scrollPositionKey()); if (lastKnownPos) { const content = document.getElementById('content-body'); diff --git a/modules/meeting/app/components/meetings/header_component.html.erb b/modules/meeting/app/components/meetings/header_component.html.erb index 7fdcbb846635..01f201391234 100644 --- a/modules/meeting/app/components/meetings/header_component.html.erb +++ b/modules/meeting/app/components/meetings/header_component.html.erb @@ -1,17 +1,14 @@ -<%= - params = { - controller: "poll-for-changes", - poll_for_changes_reference_value: @meeting.changed_hash, - poll_for_changes_url_value: check_for_updates_meeting_path(@meeting), - poll_for_changes_interval_value: check_for_updates_interval, - poll_for_changes_autoscroll_enabled_value: true - } +<% + helpers.content_controller "poll-for-changes", + poll_for_changes_reference_value: @meeting.changed_hash, + poll_for_changes_url_value: check_for_updates_meeting_path(@meeting), + poll_for_changes_interval_value: check_for_updates_interval, + poll_for_changes_autoscroll_enabled_value: true component_wrapper do render(Primer::OpenProject::PageHeader.new( test_selector: "meeting-page-header", - state: @state, - data: params + state: @state )) do |header| header.with_title do |title| title.with_editable_form(model: @meeting, diff --git a/modules/meeting/app/components/meetings/update_flash_component.rb b/modules/meeting/app/components/meetings/update_flash_component.rb index ad1996463f74..0b25370a52e3 100644 --- a/modules/meeting/app/components/meetings/update_flash_component.rb +++ b/modules/meeting/app/components/meetings/update_flash_component.rb @@ -42,8 +42,8 @@ def call ) do |banner| banner.with_action_button( tag: :a, - href: helpers.meeting_path(meeting, autoscroll: true), - data: { turbo: false }, + href: helpers.meeting_path(meeting), + data: { turbo: false, poll_for_changes_target: "reloadButton" }, size: :medium ) { I18n.t("label_meeting_reload") }