Skip to content

Commit

Permalink
[#57904] use targetConnected event in Stimulus
Browse files Browse the repository at this point in the history
  • Loading branch information
EinLama committed Sep 25, 2024
1 parent d3fc2ae commit 58f8a1f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
14 changes: 8 additions & 6 deletions frontend/src/stimulus/controllers/poll-for-changes.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}
}
Expand All @@ -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: {
Expand All @@ -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');
Expand Down
17 changes: 7 additions & 10 deletions modules/meeting/app/components/meetings/header_component.html.erb
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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") }

Expand Down

0 comments on commit 58f8a1f

Please sign in to comment.