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

[OP#65504] Show ongoing timer in list view

Klaus Zanders6586d14bf36b
diff --git a/modules/costs/app/components/my/time_tracking/list_stats_component.rb b/modules/costs/app/components/my/time_tracking/list_stats_component.rb
index 9082a81643d..0de44535c26 100644
--- a/modules/costs/app/components/my/time_tracking/list_stats_component.rb
+++ b/modules/costs/app/components/my/time_tracking/list_stats_component.rb
@@ -47,7 +47,7 @@ module My
end
def total_hours
- total_hours = time_entries.sum(&:hours).round(2)
+ total_hours = time_entries.sum(&:hours_for_calculation).round(2)
DurationConverter.output(total_hours, format: :hours_and_minutes).presence || "0h"
end
diff --git a/modules/costs/app/components/my/time_tracking/stop_timer_component.rb b/modules/costs/app/components/my/time_tracking/stop_timer_component.rb
new file mode 100644
index 00000000000..54915887c1b
--- /dev/null
+++ b/modules/costs/app/components/my/time_tracking/stop_timer_component.rb
@@ -0,0 +1,49 @@
+# frozen_string_literal: true
+
+# -- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+# ++
+
+module My
+ module TimeTracking
+ class StopTimerComponent < ApplicationComponent
+ options :time_entry
+
+ def call
+ render(
+ Primer::Beta::Link.new(
+ href: dialog_time_entry_path(time_entry, onlyMe: true),
+ data: { "turbo-stream" => true },
+ underline: false
+ )
+ ) do
+ render(Primer::Beta::Octicon.new(icon: "op-stopwatch-stop", "aria-label": t("button_stop_timer")))
+ end
+ end
+ end
+ end
+end
diff --git a/modules/costs/app/components/my/time_tracking/time_entry_row.rb b/modules/costs/app/components/my/time_tracking/time_entry_row.rb
index d9908b38968..e74220f1d29 100644
--- a/modules/costs/app/components/my/time_tracking/time_entry_row.rb
+++ b/modules/costs/app/components/my/time_tracking/time_entry_row.rb
@@ -42,36 +42,16 @@ module My
render(Primer::Alpha::ActionMenu.new) do |menu|
menu.with_show_button(icon: "kebab-horizontal", "aria-label": t("label_more"), scheme: :invisible)
- menu.with_item(
- content_arguments: {
- data: {
- "turbo-stream" => true
- }
- },
- tag: :a,
- label: t("label_edit"),
- href: dialog_time_entry_path(time_entry, onlyMe: true)
- ) do |item|
- item.with_leading_visual_icon(icon: :pencil)
+
+ if time_entry.ongoing?
+ stop_timer_action_button(menu)
+ else
+ edit_action_button(menu)
end
if can_delete_time_entry?
-
- menu.with_item(
- scheme: :danger,
- content_arguments: {
- data: {
- "turbo" => true,
- "turbo-method" => :delete,
- "turbo-confirm" => t("js.text_are_you_sure")
- }
- },
- href: time_entry_path(time_entry, no_dialog: true),
- label: t("label_delete"),
- tag: :a
- ) do |item|
- item.with_leading_visual_icon(icon: :trash)
- end
+ menu.with_divider
+ delete_action_button(menu)
end
end
end
@@ -80,22 +60,20 @@ module My
I18n.l(time_entry.spent_on)
end
- def time # rubocop:disable Metrics/AbcSize
- return if time_entry.start_time.blank?
-
- times = [I18n.l(time_entry.start_timestamp, format: :time)]
-
- times << if time_entry.start_timestamp.to_date == time_entry.end_timestamp.to_date
- I18n.l(time_entry.end_timestamp, format: :time)
- else
- I18n.l(time_entry.end_timestamp, format: :short)
- end
-
- times.join(" - ")
+ def time
+ if time_entry.ongoing?
+ ongoing_time
+ else
+ time_range
+ end
end
def hours
- DurationConverter.output(time_entry.hours, format: :hours_and_minutes)
+ if time_entry.ongoing?
+ concat(render(My::TimeTracking::StopTimerComponent.new(time_entry: time_entry)))
+ end
+
+ DurationConverter.output(time_entry.hours_for_calculation, format: :hours_and_minutes)
end
def subject
@@ -115,10 +93,85 @@ module My
time_entry.activity&.name
end
+ def scheme
+ if time_entry.ongoing?
+ :info
+ else
+ super
+ end
+ end
+
delegate :comments, to: :time_entry
private
+ def stop_timer_action_button(menu)
+ menu.with_item(
+ content_arguments: {
+ data: {
+ "turbo-stream" => true
+ }
+ },
+ tag: :a,
+ label: t("button_stop_timer"),
+ href: dialog_time_entry_path(time_entry, onlyMe: true)
+ ) do |item|
+ item.with_leading_visual_icon(icon: "op-stopwatch-stop")
+ end
+ end
+
+ def edit_action_button(menu)
+ menu.with_item(
+ content_arguments: {
+ data: {
+ "turbo-stream" => true
+ }
+ },
+ tag: :a,
+ label: t("label_edit"),
+ href: dialog_time_entry_path(time_entry, onlyMe: true)
+ ) do |item|
+ item.with_leading_visual_icon(icon: :pencil)
+ end
+ end
+
+ def delete_action_button(menu)
+ menu.with_item(
+ scheme: :danger,
+ content_arguments: {
+ data: {
+ "turbo" => true,
+ "turbo-method" => :delete,
+ "turbo-confirm" => t("js.text_are_you_sure")
+ }
+ },
+ href: time_entry_path(time_entry, no_dialog: true),
+ label: t("label_delete"),
+ tag: :a
+ ) do |item|
+ item.with_leading_visual_icon(icon: :trash)
+ end
+ end
+
+ def ongoing_time
+ I18n.t("label_timer_since",
+ time: I18n.l(time_entry.created_at, format: :time))
+ end
+
+ def time_range # rubocop:disable Metrics/AbcSize
+ return if time_entry.start_time.blank?
+
+ times = [I18n.l(time_entry.start_timestamp, format: :time)]
+
+ times << if time_entry.start_timestamp.to_date == time_entry.end_timestamp.to_date
+ I18n.l(time_entry.end_timestamp, format: :time)
+ else
+ I18n.l(time_entry.end_timestamp, format: :short)
+ end
+
+ times.join(" - ")
+ end
+
def time_entry
model
end
diff --git a/modules/costs/app/controllers/my/time_tracking_controller.rb b/modules/costs/app/controllers/my/time_tracking_controller.rb
index 1532352dea8..659f501ac12 100644
--- a/modules/costs/app/controllers/my/time_tracking_controller.rb
+++ b/modules/costs/app/controllers/my/time_tracking_controller.rb
@@ -125,7 +125,7 @@ module My
@time_entries = TimeEntry
.includes(:project, :activity, { work_package: :status })
.where(project_id: Project.visible.select(:id))
- .where(user: User.current, spent_on: time_scope, ongoing: false)
+ .where(user: User.current, spent_on: time_scope)
.order(:spent_on, :start_time, :hours)
end
diff --git a/modules/costs/app/models/time_entry.rb b/modules/costs/app/models/time_entry.rb
index 688a3fc35e4..143be8a5af8 100644
--- a/modules/costs/app/models/time_entry.rb
+++ b/modules/costs/app/models/time_entry.rb
@@ -117,7 +117,7 @@ class TimeEntry < ApplicationRecord
def ongoing_hours
return nil unless ongoing?
- (Time.zone.now.to_i - created_at.to_i) / SECONDS_PER_HOUR
+ ((Time.zone.now.to_i - created_at.to_i) / SECONDS_PER_HOUR).round(2)
end
def start_time=(value)
@@ -152,6 +152,10 @@ class TimeEntry < ApplicationRecord
start_time.present?
end
+ def hours_for_calculation
+ ongoing? ? ongoing_hours : hours
+ end
+
def start_timestamp # rubocop:disable Metrics/AbcSize
return nil if start_time.blank?
return nil if time_zone.blank?
diff --git a/modules/costs/config/locales/en.yml b/modules/costs/config/locales/en.yml
index 8e2af4fcb92..d0e20c7ceb1 100644
--- a/modules/costs/config/locales/en.yml
+++ b/modules/costs/config/locales/en.yml
@@ -105,6 +105,7 @@ en:
button_log_time: "Log"
button_add_activity: "Activity"
button_add_time_entry: "Log time"
+ button_stop_timer: "Stop timer"
caption_booked_on_project: "Booked on project"
caption_default: "Default"
@@ -187,6 +188,7 @@ en:
label_day: "Day"
label_today_capitalized: "Today"
label_view_mode_switcher: "Change view"
+ label_timer_since: "Started at %{time}"
placeholder_activity_select_work_package_first: Work package selection is required first