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

move calendar into a component

Klaus Zanders8ab02a3535a5
diff --git a/modules/costs/app/components/my/time_tracking/calendar_component.html.erb b/modules/costs/app/components/my/time_tracking/calendar_component.html.erb
new file mode 100644
index 00000000000..2f77e251365
--- /dev/null
+++ b/modules/costs/app/components/my/time_tracking/calendar_component.html.erb
@@ -0,0 +1,25 @@
+<%= component_wrapper(tag: "div", data: wrapper_data) do %>
+ <%= render(Primer::OpenProject::SubHeader.new) do |component|
+ component.with_text { title }
+
+ 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 %>
+
+ <div data-my--time-tracking-target="calendar"></div>
+<%- end %>
diff --git a/modules/costs/app/components/my/time_tracking/calendar_component.rb b/modules/costs/app/components/my/time_tracking/calendar_component.rb
new file mode 100644
index 00000000000..c0a47b806af
--- /dev/null
+++ b/modules/costs/app/components/my/time_tracking/calendar_component.rb
@@ -0,0 +1,115 @@
+# 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 CalendarComponent < ApplicationComponent
+ include OpTurbo::Streamable
+
+ options time_entries: [],
+ mode: :week,
+ date: Date.current
+
+ private
+
+ def wrapper_data
+ {
+ "controller" => "my--time-tracking",
+ "application-target" => "dynamic",
+ "my--time-tracking-mode-value" => mode,
+ "my--time-tracking-time-entries-value" => time_entries_json,
+ "my--time-tracking-initial-date-value" => date.iso8601,
+ "my--time-tracking-can-create-value" => User.current.allowed_in_any_project?(:log_own_time),
+ "my--time-tracking-can-edit-value" => User.current.allowed_in_any_project?(:edit_own_time_entries),
+ "my--time-tracking-force-times-value" => TimeEntry.must_track_start_and_end_time?
+ }
+ end
+
+ def time_entries_json
+ time_entries.map do |time_entry|
+ FullCalendar::TimeEntryEvent.from_time_entry(time_entry)
+ 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
+ end
+ end
+end
diff --git a/modules/costs/app/controllers/my/time_tracking_controller.rb b/modules/costs/app/controllers/my/time_tracking_controller.rb
index dc8d4697d53..b736f134092 100644
--- a/modules/costs/app/controllers/my/time_tracking_controller.rb
+++ b/modules/costs/app/controllers/my/time_tracking_controller.rb
@@ -96,12 +96,6 @@ module My
private
- def time_entries_json
- @time_entries.map do |time_entry|
- FullCalendar::TimeEntryEvent.from_time_entry(time_entry)
- end.to_json
- end
-
def current_day
return @current_day if defined?(@current_day)
diff --git a/modules/costs/app/views/my/time_tracking/_calendar.html.erb b/modules/costs/app/views/my/time_tracking/_calendar.html.erb
deleted file mode 100644
index 05b9ef528f7..00000000000
--- a/modules/costs/app/views/my/time_tracking/_calendar.html.erb
+++ /dev/null
@@ -1,52 +0,0 @@
-<div
- data-controller="my--time-tracking"
- data-application-target="dynamic"
- data-my--time-tracking-mode-value="<%= local_assigns[:mode] %>"
- data-my--time-tracking-time-entries-value="<%= time_entries_json %>"
- data-my--time-tracking-initial-date-value="<%= current_day %>"
- data-my--time-tracking-can-create-value="<%= User.current.allowed_in_any_project?(:log_own_time) %>"
- data-my--time-tracking-can-edit-value="<%= User.current.allowed_in_any_project?(:edit_own_time_entries) %>"
- data-my--time-tracking-force-times-value="<%= TimeEntry.must_track_start_and_end_time? %>">
- <%= render(Primer::OpenProject::SubHeader.new) do |component|
- component.with_text do
- "#{current_day.beginning_of_week.day}-#{current_day.end_of_week.day}. #{current_day.strftime('%B %Y')}"
- end
-
- component.with_action_component do
- render(Primer::Beta::ButtonGroup.new) do |group|
- group.with_button(
- icon: "arrow-left",
- aria: {
- label: "Previous week"
- },
- tag: :a,
- href: my_time_tracking_week_path(date: current_day - 1.week)
- )
- group.with_button(
- icon: "arrow-right",
- aria: {
- label: "Next week"
- },
- tag: :a,
- href: my_time_tracking_week_path(date: current_day + 1.week)
- )
- end
- end
-
- component.with_action_button(
- tag: :a,
- href: my_time_tracking_week_path(date: Time.zone.today)
- ) { "Today" }
-
- component.with_action_button(
- scheme: :primary,
- data: {
- "turbo-stream" => true
- },
- tag: :a,
- href: dialog_time_entries_path(onlyMe: true)
- ) { "Log Time" }
- end %>
-
- <div data-my--time-tracking-target="calendar"></div>
-</div>
diff --git a/modules/costs/app/views/my/time_tracking/day.html.erb b/modules/costs/app/views/my/time_tracking/day.html.erb
index bb66833433c..5a12f3e2a20 100644
--- a/modules/costs/app/views/my/time_tracking/day.html.erb
+++ b/modules/costs/app/views/my/time_tracking/day.html.erb
@@ -10,4 +10,4 @@
end
%>
-<%= render(partial: "calendar", locals: { mode: "day" }) %>
+<%= render(My::TimeTracking::CalendarComponent.new(time_entries: @time_entries, mode: :day, date: current_day)) %>
diff --git a/modules/costs/app/views/my/time_tracking/month.html.erb b/modules/costs/app/views/my/time_tracking/month.html.erb
index 09c750b46fd..cc81cb00474 100644
--- a/modules/costs/app/views/my/time_tracking/month.html.erb
+++ b/modules/costs/app/views/my/time_tracking/month.html.erb
@@ -10,4 +10,4 @@
end
%>
-<%= render(partial: "calendar", locals: { mode: "month" }) %>
+<%= render(My::TimeTracking::CalendarComponent.new(time_entries: @time_entries, mode: :month, date: current_day)) %>
diff --git a/modules/costs/app/views/my/time_tracking/week.html.erb b/modules/costs/app/views/my/time_tracking/week.html.erb
index 3ed0044c3dd..06b96d87ea0 100644
--- a/modules/costs/app/views/my/time_tracking/week.html.erb
+++ b/modules/costs/app/views/my/time_tracking/week.html.erb
@@ -28,4 +28,4 @@
end
%>
-<%= render(partial: "calendar", locals: { mode: "week" }) %>
+<%= render(My::TimeTracking::CalendarComponent.new(time_entries: @time_entries, mode: :week, date: current_day)) %>