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

Add navigation etc to table

Klaus Zanders39e62f7d8afd
diff --git a/modules/costs/app/components/my/time_tracking/calendar_component.rb b/modules/costs/app/components/my/time_tracking/calendar_component.rb
index 489bb5c706c..ec126f2ae92 100644
--- a/modules/costs/app/components/my/time_tracking/calendar_component.rb
+++ b/modules/costs/app/components/my/time_tracking/calendar_component.rb
@@ -32,6 +32,7 @@ module My
module TimeTracking
class CalendarComponent < ApplicationComponent
include OpTurbo::Streamable
+ include ComponentNavigation
options time_entries: [],
mode: :week,
@@ -58,59 +59,6 @@ module My
end.to_json
end
- def today_href
- case mode
- when :day
- my_time_tracking_day_path(date: Date.current)
- when :week
- my_time_tracking_week_path(date: Date.current)
- when :month
- my_time_tracking_month_path(date: Date.current)
- end
- end
-
- def title # rubocop:disable Metrics/AbcSize
- case mode
- when :day
- I18n.l(date, format: :long)
- when :week
- bow = date.beginning_of_week
- eow = date.end_of_week
-
- if bow.year == eow.year && bow.month == eow.month
- "#{bow.strftime('%d.')} - #{eow.strftime('%d. %B %Y')}"
- elsif bow.year == eow.year
- "#{bow.strftime('%d. %B')} - #{eow.strftime('%d. %B %Y')}"
- else
- "#{bow.strftime('%d. %B %Y')} - #{eow.strftime('%d. %B %Y')}"
- end
- when :month
- date.strftime("%B %Y")
- end
- end
-
- def previous_attrs
- case mode
- when :day
- { href: my_time_tracking_day_path(date: date - 1.day), aria: { label: I18n.t(:label_previous_day) } }
- when :week
- { href: my_time_tracking_week_path(date: date - 1.week), aria: { label: I18n.t(:label_previous_week) } }
- when :month
- { href: my_time_tracking_month_path(date: date - 1.month), aria: { label: I18n.t(:label_previous_month) } }
- end
- end
-
- def next_attrs
- case mode
- when :day
- { href: my_time_tracking_day_path(date: date + 1.day), aria: { label: I18n.t(:label_next_day) } }
- when :week
- { href: my_time_tracking_week_path(date: date + 1.week), aria: { label: I18n.t(:label_next_week) } }
- when :month
- { href: my_time_tracking_month_path(date: date + 1.month), aria: { label: I18n.t(:label_next_month) } }
- end
- end
-
def total_hours
total_hours = time_entries.sum(&:hours).round(2)
total_str = DurationConverter.output(total_hours, format: :hours_and_minutes)
diff --git a/modules/costs/app/components/my/time_tracking/component_navigation.rb b/modules/costs/app/components/my/time_tracking/component_navigation.rb
new file mode 100644
index 00000000000..175d1fdcb8c
--- /dev/null
+++ b/modules/costs/app/components/my/time_tracking/component_navigation.rb
@@ -0,0 +1,95 @@
+# 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
+ module ComponentNavigation
+ extend ActiveSupport::Concern
+ def title # rubocop:disable Metrics/AbcSize
+ case mode
+ when :day
+ I18n.l(date, format: :long)
+ when :week
+ bow = date.beginning_of_week
+ eow = date.end_of_week
+
+ if bow.year == eow.year && bow.month == eow.month
+ "#{bow.strftime('%d.')} - #{eow.strftime('%d. %B %Y')}"
+ elsif bow.year == eow.year
+ "#{bow.strftime('%d. %B')} - #{eow.strftime('%d. %B %Y')}"
+ else
+ "#{bow.strftime('%d. %B %Y')} - #{eow.strftime('%d. %B %Y')}"
+ end
+ when :month
+ date.strftime("%B %Y")
+ end
+ end
+
+ def today_href
+ case mode
+ when :day
+ my_time_tracking_day_path(date: Date.current, view_mode: params[:view_mode])
+ when :week
+ my_time_tracking_week_path(date: Date.current, view_mode: params[:view_mode])
+ when :month
+ my_time_tracking_month_path(date: Date.current, view_mode: params[:view_mode])
+ end
+ end
+
+ def previous_attrs # rubocop:disable Metrics/AbcSize
+ case mode
+ when :day
+ { href: my_time_tracking_day_path(date: date - 1.day, view_mode: params[:view_mode]),
+ aria: { label: I18n.t(:label_previous_day) } }
+ when :week
+ { href: my_time_tracking_week_path(date: date - 1.week, view_mode: params[:view_mode]),
+ aria: { label: I18n.t(:label_previous_week) } }
+ when :month
+ { href: my_time_tracking_month_path(date: date - 1.month, view_mode: params[:view_mode]),
+ aria: { label: I18n.t(:label_previous_month) } }
+ end
+ end
+
+ def next_attrs # rubocop:disable Metrics/AbcSize
+ case mode
+ when :day
+ { href: my_time_tracking_day_path(date: date + 1.day, view_mode: params[:view_mode]),
+ aria: { label: I18n.t(:label_next_day) } }
+ when :week
+ { href: my_time_tracking_week_path(date: date + 1.week, view_mode: params[:view_mode]),
+ aria: { label: I18n.t(:label_next_week) } }
+ when :month
+ { href: my_time_tracking_month_path(date: date + 1.month, view_mode: params[:view_mode]),
+ aria: { label: I18n.t(:label_next_month) } }
+ end
+ end
+ end
+ end
+end
diff --git a/modules/costs/app/components/my/time_tracking/table_component.html.erb b/modules/costs/app/components/my/time_tracking/table_component.html.erb
index 1a5757b9205..620d41e1554 100644
--- a/modules/costs/app/components/my/time_tracking/table_component.html.erb
+++ b/modules/costs/app/components/my/time_tracking/table_component.html.erb
@@ -1,8 +1,36 @@
-<h1> We are a table component </h1>
-<h2><%= title %></h2>
+<%= component_wrapper(tag: "div") do %>
+ <%= render(Primer::OpenProject::SubHeader.new) do |component|
+ component.with_text { title }
-<ul>
- <%- time_entries.each do |te| %>
- <li><%= te.spent_on %> <%= te.hours %>h - <%= te.work_package.subject %></li>
+ component.with_action_component do
+ render(Primer::Beta::ButtonGroup.new) do |group|
+ group.with_button(icon: "arrow-left", tag: :a, **previous_attrs)
+ group.with_button(icon: "arrow-right", tag: :a, **next_attrs)
+ end
+ end
+
+ component.with_action_button(tag: :a, href: today_href) { "Today" }
+
+ component.with_action_button(
+ scheme: :primary,
+ data: {
+ "turbo-stream" => true
+ },
+ tag: :a,
+ href: dialog_time_entries_path(onlyMe: true)
+ ) { "Log Time" }
+ end %>
+
+ <ul>
+ <%- range.each do |date| %>
+ <li>
+ <h3><%= date %> (<%= time_entries_by_day[date].count %> entries, <%= total_hours_per_day(date) %>)</h3>
+ <ul>
+ <%- time_entries_by_day[date].each do |te| %>
+ <li><%= te.spent_on %> <%= te.hours %>h - <%= te.work_package.subject %></li>
+ <%- end %>
+ </ul>
+ </li>
<%- end %>
-</ul>
+ </ul>
+<%- end %>
diff --git a/modules/costs/app/components/my/time_tracking/table_component.rb b/modules/costs/app/components/my/time_tracking/table_component.rb
index adf3f3e80a3..4e5375789d2 100644
--- a/modules/costs/app/components/my/time_tracking/table_component.rb
+++ b/modules/costs/app/components/my/time_tracking/table_component.rb
@@ -32,6 +32,7 @@ module My
module TimeTracking
class TableComponent < ApplicationComponent
include OpTurbo::Streamable
+ include ComponentNavigation
options time_entries: [],
mode: :week,
@@ -39,57 +40,23 @@ module My
private
- def today_href
+ def range
case mode
- when :day
- my_time_tracking_day_path(date: Date.current)
- when :week
- my_time_tracking_week_path(date: Date.current)
- when :month
- my_time_tracking_month_path(date: Date.current)
+ when :day then [date]
+ when :week then date.all_week
+ when :month then date.all_month
end
end
- def title # rubocop:disable Metrics/AbcSize
- case mode
- when :day
- I18n.l(date, format: :long)
- when :week
- bow = date.beginning_of_week
- eow = date.end_of_week
-
- if bow.year == eow.year && bow.month == eow.month
- "#{bow.strftime('%d.')} - #{eow.strftime('%d. %B %Y')}"
- elsif bow.year == eow.year
- "#{bow.strftime('%d. %B')} - #{eow.strftime('%d. %B %Y')}"
- else
- "#{bow.strftime('%d. %B %Y')} - #{eow.strftime('%d. %B %Y')}"
- end
- when :month
- date.strftime("%B %Y")
- end
- end
-
- def previous_attrs
- case mode
- when :day
- { href: my_time_tracking_day_path(date: date - 1.day), aria: { label: I18n.t(:label_previous_day) } }
- when :week
- { href: my_time_tracking_week_path(date: date - 1.week), aria: { label: I18n.t(:label_previous_week) } }
- when :month
- { href: my_time_tracking_month_path(date: date - 1.month), aria: { label: I18n.t(:label_previous_month) } }
+ def time_entries_by_day
+ @time_entries_by_day ||= time_entries.group_by(&:spent_on).tap do |hash|
+ hash.default_proc = ->(h, k) { h[k] = [] }
end
end
- def next_attrs
- case mode
- when :day
- { href: my_time_tracking_day_path(date: date + 1.day), aria: { label: I18n.t(:label_next_day) } }
- when :week
- { href: my_time_tracking_week_path(date: date + 1.week), aria: { label: I18n.t(:label_next_week) } }
- when :month
- { href: my_time_tracking_month_path(date: date + 1.month), aria: { label: I18n.t(:label_next_month) } }
- end
+ def total_hours_per_day(date)
+ total_hours = time_entries_by_day[date].sum(&:hours).round(2)
+ DurationConverter.output(total_hours, format: :hours_and_minutes)
end
end
end
diff --git a/modules/costs/config/routes.rb b/modules/costs/config/routes.rb
index 7a9e24d2ecb..08529386bbe 100644
--- a/modules/costs/config/routes.rb
+++ b/modules/costs/config/routes.rb
@@ -57,7 +57,7 @@ Rails.application.routes.draw do
get "/time-tracking/week(-:view_mode)" => "time_tracking#week",
defaults: { view_mode: "calendar" },
as: :time_tracking_week
- get "/time-tracking/month(-view_mode)" => "time_tracking#month",
+ get "/time-tracking/month(-:view_mode)" => "time_tracking#month",
defaults: { view_mode: "calendar" },
as: :time_tracking_month
end