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

Update counter as turbo stream

Oliver Günthera395654731fc
diff --git a/app/components/work_packages/details/tab_component.html.erb b/app/components/work_packages/details/tab_component.html.erb
index 5c88f03c2f1..9e47fba192c 100644
--- a/app/components/work_packages/details/tab_component.html.erb
+++ b/app/components/work_packages/details/tab_component.html.erb
@@ -10,7 +10,7 @@
) do |c|
c.with_text { t("js.work_packages.tabs.#{node.name}") }
count = node.badge(work_package:).to_i
- c.with_counter(count:, test_selector: "wp-details-tab-component--tab-counter") if count > 0
+ c.with_counter(count:, hide_if_zero: true, id: "wp-details-tab-#{node.name}-counter", test_selector: "wp-details-tab-component--tab-counter")
end
end
end
diff --git a/app/controllers/concerns/work_packages/with_split_view.rb b/app/controllers/concerns/work_packages/with_split_view.rb
index 0f9cfe74896..158718129f5 100644
--- a/app/controllers/concerns/work_packages/with_split_view.rb
+++ b/app/controllers/concerns/work_packages/with_split_view.rb
@@ -32,12 +32,33 @@ module WorkPackages
included do
helper_method :split_view_base_route
+
+ before_action :find_work_package, only: %i[split_view update_counter]
+ before_action :find_counter_menu, only: %i[update_counter]
end
def split_view_work_package_id
params[:work_package_id].to_i
end
+ def update_counter
+ respond_to do |format|
+ format.turbo_stream do
+ render turbo_stream: [
+ turbo_stream.replace("wp-details-tab-#{@counter_menu.name}-counter") do
+ count = @counter_menu.badge(work_package: @work_package).to_i
+ Primer::Beta::Counter
+ .new(count:,
+ hide_if_zero: true,
+ id: "wp-details-tab-#{@counter_menu.name}-counter",
+ test_selector: "wp-details-tab-component--tab-counter")
+ .render_in(view_context)
+ end
+ ]
+ end
+ end
+ end
+
def close_split_view
respond_to do |format|
format.turbo_stream do
@@ -65,5 +86,24 @@ module WorkPackages
yield(format) if format_block
end
end
+
+ private
+
+ def find_work_package
+ @work_package = WorkPackage.visible.find(params[:work_package_id])
+ rescue ActiveRecord::RecordNotFound
+ flash[:error] = I18n.t(:error_work_package_id_not_found)
+ redirect_to split_view_base_route
+ end
+
+ def find_counter_menu
+ @counter_menu = Redmine::MenuManager
+ .items(:work_package_split_view, nil)
+ .root
+ .children
+ .detect { |node| node.name.to_s == params[:counter] }
+
+ render_400 if @counter_menu.nil?
+ end
end
end
diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb
index 2db952aeb48..250ac416886 100644
--- a/app/controllers/notifications_controller.rb
+++ b/app/controllers/notifications_controller.rb
@@ -31,7 +31,7 @@ class NotificationsController < ApplicationController
before_action :require_login
before_action :filtered_query, only: :mark_all_read
- no_authorization_required! :index, :split_view, :close_split_view, :mark_all_read, :date_alerts, :share_upsale
+ no_authorization_required! :index, :split_view, :update_counter, :close_split_view, :mark_all_read, :date_alerts, :share_upsale
def index
render_notifications_layout
diff --git a/app/views/notifications/menus/_menu.html.erb b/app/views/notifications/menus/_menu.html.erb
index a9df3947768..9033251ad41 100644
--- a/app/views/notifications/menus/_menu.html.erb
+++ b/app/views/notifications/menus/_menu.html.erb
@@ -1,5 +1,5 @@
<%= turbo_frame_tag "notifications_sidemenu",
- src: notifications_menu_path(**params.permit(:filter, :name)),
+ src: notifications_menu_path(**params.slice(:filter, :name).permit!),
target: '_top',
data: { turbo: false },
loading: :lazy %>
diff --git a/config/initializers/menus.rb b/config/initializers/menus.rb
index 58783d58090..28fa3edaa69 100644
--- a/config/initializers/menus.rb
+++ b/config/initializers/menus.rb
@@ -670,5 +670,8 @@ Redmine::MenuManager.map :work_package_split_view do |menu|
menu.push :watchers,
{ tab: :watchers },
skip_permissions_check: true,
+ badge: ->(work_package:, **) {
+ work_package.watchers.count
+ },
caption: :"js.work_packages.tabs.watchers"
end
diff --git a/config/routes.rb b/config/routes.rb
index c54e101879c..222c08b6abf 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -701,21 +701,22 @@ Rails.application.routes.draw do
concern :with_split_view do |options|
get "details/:work_package_id(/:tab)",
- on: :collection,
action: options.fetch(:action, :split_view),
defaults: { tab: :overview },
as: :details,
work_package_split_view: true
get "/:work_package_id/close",
- on: :collection,
action: :close_split_view
+
+ get "/:work_package_id/update_counter",
+ action: :update_counter
end
resources :notifications, only: :index do
- concerns :with_split_view, base_route: :notifications_path
-
collection do
+ concerns :with_split_view, base_route: :notifications_path
+
post :mark_all_read
resource :menu, module: :notifications, only: %i[show], as: :notifications_menu
end
diff --git a/frontend/src/app/features/work-packages/components/wp-single-view-tabs/watchers-tab/watchers-tab.component.ts b/frontend/src/app/features/work-packages/components/wp-single-view-tabs/watchers-tab/watchers-tab.component.ts
index 32f72a86be6..cebccd4eec5 100644
--- a/frontend/src/app/features/work-packages/components/wp-single-view-tabs/watchers-tab/watchers-tab.component.ts
+++ b/frontend/src/app/features/work-packages/components/wp-single-view-tabs/watchers-tab/watchers-tab.component.ts
@@ -42,6 +42,7 @@ import {
} from 'core-app/features/work-packages/services/notifications/work-package-notification.service';
import { UntilDestroyedMixin } from 'core-app/shared/helpers/angular/until-destroyed.mixin';
import { ApiV3Service } from 'core-app/core/apiv3/api-v3.service';
+import { TurboRequestsService } from "core-app/core/turbo/turbo-requests.service";
@Component({
templateUrl: './watchers-tab.html',
@@ -89,6 +90,7 @@ export class WorkPackageWatchersTabComponent extends UntilDestroyedMixin impleme
readonly cdRef:ChangeDetectorRef,
readonly pathHelper:PathHelperService,
readonly apiV3Service:ApiV3Service,
+ readonly turboRequests:TurboRequestsService,
) {
super();
}
@@ -97,6 +99,7 @@ export class WorkPackageWatchersTabComponent extends UntilDestroyedMixin impleme
this.$element = jQuery(this.elementRef.nativeElement);
const { workPackageId } = this.uiRouterGlobals.params as unknown as { workPackageId:string };
this.workPackageId = (this.workPackage.id as string) || workPackageId;
+
this
.apiV3Service
.work_packages
@@ -150,6 +153,8 @@ export class WorkPackageWatchersTabComponent extends UntilDestroyedMixin impleme
.id(this.workPackage)
.refresh();
+ this.updateCounter();
+
this.cdRef.detectChanges();
})
.catch((error:any) => this.notificationService.showError(error, this.workPackage));
@@ -168,8 +173,16 @@ export class WorkPackageWatchersTabComponent extends UntilDestroyedMixin impleme
.work_packages
.id(this.workPackage)
.refresh();
+
+ this.updateCounter();
+
this.cdRef.detectChanges();
})
.catch((error:any) => this.notificationService.showError(error, this.workPackage));
}
+
+ public updateCounter() {
+ const url = this.pathHelper.notificationsPath() + `/${this.workPackageId}/update_counter?counter=watchers`;
+ void this.turboRequests.request(url);
+ }
}