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

Use a timestamp to check if we should send a flash

Oliver Günther37b74bf0826d
diff --git a/frontend/src/stimulus/controllers/poll-for-changes.controller.ts b/frontend/src/stimulus/controllers/poll-for-changes.controller.ts
index 3640468db3b..9be894e9623 100644
--- a/frontend/src/stimulus/controllers/poll-for-changes.controller.ts
+++ b/frontend/src/stimulus/controllers/poll-for-changes.controller.ts
@@ -32,20 +32,43 @@ import { ApplicationController } from 'stimulus-use';
import { renderStreamMessage } from '@hotwired/turbo';
export default class PollForChangesController extends ApplicationController {
+ static values = {
+ url: String,
+ interval: Number,
+ updatedAt: String,
+ };
+
+ declare updatedAtValue:string;
+ declare urlValue:string;
+ declare intervalValue:number;
+
+ private interval:number;
+
connect() {
super.connect();
- setTimeout(() => {
- console.log("Checking");
- this.triggerTurboStream();
- }, 2000);
+
+ this.interval = setInterval(() => {
+ void this.triggerTurboStream();
+ }, this.intervalValue || 10_000);
+ }
+
+ disconnect() {
+ super.disconnect();
+ clearInterval(this.interval);
}
async triggerTurboStream():Promise<void> {
- await fetch(`/meetings/2512/check_for_updates`, {
+ await fetch(`${this.urlValue}?updatedAt=${this.updatedAtValue}`, {
headers: {
Accept: 'text/vnd.turbo-stream.html',
},
- }).then((r) => r.text())
- .then((html) => renderStreamMessage(html));
+ }).then(async (r) => {
+ if (r.status === 200) {
+ clearInterval(this.interval);
+
+ const html = await r.text();
+ renderStreamMessage(html);
+ }
+ });
}
}
diff --git a/frontend/src/turbo/flash-stream-action.ts b/frontend/src/turbo/flash-stream-action.ts
index 0105e0c9786..ed8fe21828a 100644
--- a/frontend/src/turbo/flash-stream-action.ts
+++ b/frontend/src/turbo/flash-stream-action.ts
@@ -4,6 +4,12 @@ export function registerFlashStreamAction() {
StreamActions.flash = function dialogStreamAction(this:StreamElement) {
const content = this.templateElement.content;
+ const flash = content.querySelector('.flash') as HTMLElement;
+ if (flash.dataset.singleton) {
+ const elements = document.querySelectorAll('.flash')
+ elements.forEach((element) => element.parentElement?.remove());
+ }
+
document.body.append(content);
};
}
diff --git a/modules/meeting/app/components/meetings/header_component.html.erb b/modules/meeting/app/components/meetings/header_component.html.erb
index 19ac108a6d4..95528e5eb7e 100644
--- a/modules/meeting/app/components/meetings/header_component.html.erb
+++ b/modules/meeting/app/components/meetings/header_component.html.erb
@@ -1,6 +1,12 @@
<%=
component_wrapper do
- render(Primer::OpenProject::PageHeader.new) do |header|
+ render(Primer::OpenProject::PageHeader.new(
+ data: {
+ controller: "poll-for-changes",
+ poll_for_changes_updated_at_value: @meeting.updated_at.iso8601,
+ poll_for_changes_url_value: check_for_updates_meeting_path(@meeting),
+ }
+ )) do |header|
if show_state?
header.with_title { @meeting.title }
header.with_breadcrumbs(breadcrumb_items)
diff --git a/modules/meeting/app/components/meetings/show_component.html.erb b/modules/meeting/app/components/meetings/show_component.html.erb
index 1f95ddd844a..38e4240339e 100644
--- a/modules/meeting/app/components/meetings/show_component.html.erb
+++ b/modules/meeting/app/components/meetings/show_component.html.erb
@@ -1,5 +1,5 @@
<%=
- flex_layout(data: { controller: "poll-for-changes", turbo: true }) do |show_page|
+ flex_layout(data: { turbo: true }) do |show_page|
show_page.with_row do
# prototyical usage of Primer's flash message component wrapped in a component which can be updated via turbo stream
# empty initially
diff --git a/modules/meeting/app/components/meetings/update_flash_component.html.erb b/modules/meeting/app/components/meetings/update_flash_component.html.erb
index 9902f60371d..93d9b5a99ff 100644
--- a/modules/meeting/app/components/meetings/update_flash_component.html.erb
+++ b/modules/meeting/app/components/meetings/update_flash_component.html.erb
@@ -1,6 +1,15 @@
-<%= render(Primer::Alpha::Banner.new(full: false, icon: 'info', scheme: :default)) do |component| %>
+<%= render(Primer::Alpha::Banner.new(
+ full: false,
+ icon: 'info',
+ scheme: :default,
+ data: {
+ singleton: true
+ }
+)) do |component| %>
<% component.with_action_button(
- src: meeting_path(@meeting),
+ tag: :a,
+ href: meeting_path(@meeting),
+ data: { turbo: false },
size: :medium) { "Reload page" } %>
You really need to update this upage
<% end %>
diff --git a/modules/meeting/app/controllers/meetings_controller.rb b/modules/meeting/app/controllers/meetings_controller.rb
index 9d2097a7f29..3aca0502c02 100644
--- a/modules/meeting/app/controllers/meetings_controller.rb
+++ b/modules/meeting/app/controllers/meetings_controller.rb
@@ -76,7 +76,11 @@ class MeetingsController < ApplicationController
end
def check_for_updates
- respond_with_flash(Meetings::UpdateFlashComponent.new(meeting: @meeting))
+ if Time.parse(params[:updatedAt]) < @meeting.updated_at.change(usec: 0)
+ respond_with_flash(Meetings::UpdateFlashComponent.new(meeting: @meeting))
+ else
+ head :no_content
+ end
end
def create # rubocop:disable Metrics/AbcSize