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

simplify rendering logic

Klaus Zandersc733466243de
diff --git a/modules/costs/app/components/my/time_tracking/mode_switcher_component.rb b/modules/costs/app/components/my/time_tracking/mode_switcher_component.rb
index 429a6b85ac6..7a9460c0a9c 100644
--- a/modules/costs/app/components/my/time_tracking/mode_switcher_component.rb
+++ b/modules/costs/app/components/my/time_tracking/mode_switcher_component.rb
@@ -32,18 +32,19 @@ module My
module TimeTracking
class ModeSwitcherComponent < ApplicationComponent
options :current_mode,
- :view_mode
+ :view_mode,
+ :date
def call
render(Primer::Alpha::SegmentedControl.new("aria-label": I18n.t(:label_meeting_date_time))) do |control|
- %i[day week month].each do |action|
+ %i[day week month].each do |mode|
control.with_item(
tag: :a,
- href: url_for(params.permit(:controller, :date).merge(action:, view_mode: view_mode)),
- icon: icon_for_mode(action),
- label: t("label_#{action}"),
- title: t("label_#{action}"),
- selected: (current_mode == action)
+ href: my_time_tracking_path(date:, view_mode:, mode:),
+ icon: icon_for_mode(mode),
+ label: t("label_#{mode}"),
+ title: t("label_#{mode}"),
+ selected: (current_mode == mode)
)
end
end
diff --git a/modules/costs/app/components/my/time_tracking/sub_header_component.html.erb b/modules/costs/app/components/my/time_tracking/sub_header_component.html.erb
index f3cbceefe02..a823a822bb0 100644
--- a/modules/costs/app/components/my/time_tracking/sub_header_component.html.erb
+++ b/modules/costs/app/components/my/time_tracking/sub_header_component.html.erb
@@ -1,6 +1,6 @@
<%= render(Primer::OpenProject::SubHeader.new) do |component|
component.with_filter_component do
- render(My::TimeTracking::ModeSwitcherComponent.new(current_mode: mode, view_mode: view_mode))
+ render(My::TimeTracking::ModeSwitcherComponent.new(current_mode: mode, view_mode: view_mode, date: date))
end
component.with_text { title }
diff --git a/modules/costs/app/components/my/time_tracking/sub_header_component.rb b/modules/costs/app/components/my/time_tracking/sub_header_component.rb
index 0428a2516a2..daa5dc72d69 100644
--- a/modules/costs/app/components/my/time_tracking/sub_header_component.rb
+++ b/modules/costs/app/components/my/time_tracking/sub_header_component.rb
@@ -54,26 +54,19 @@ module My
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
+ my_time_tracking_path(date: Date.current, view_mode:, mode:)
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]),
+ { href: my_time_tracking_path(date: date - 1.day, view_mode:, 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]),
+ { href: my_time_tracking_path(date: date - 1.week, view_mode:, 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]),
+ { href: my_time_tracking_path(date: date - 1.month, view_mode:, mode:),
aria: { label: I18n.t(:label_previous_month) } }
end
end
@@ -81,13 +74,13 @@ module My
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]),
+ { href: my_time_tracking_path(date: date + 1.day, view_mode:, 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]),
+ { href: my_time_tracking_path(date: date + 1.week, view_mode:, 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]),
+ { href: my_time_tracking_path(date: date + 1.month, view_mode:, mode:),
aria: { label: I18n.t(:label_next_month) } }
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 6111bd14d60..8b559a5390c 100644
--- a/modules/costs/app/controllers/my/time_tracking_controller.rb
+++ b/modules/costs/app/controllers/my/time_tracking_controller.rb
@@ -30,62 +30,60 @@
module My
class TimeTrackingController < ApplicationController
- before_action :require_login
+ include OpTurbo::ComponentStream
- no_authorization_required!(:index, :day, :week, :month)
+ before_action :require_login, :view_mode, :mode, :date
+
+ no_authorization_required!(:index, :refresh)
menu_item :my_time_tracking
layout "global"
- helper_method :current_day, :today?, :this_week?, :this_month?, :list_view_component, :view_mode
+ helper_method :list_view_component
def index
- if mobile?
- redirect_to action: :day, view_mode: default_view_mode
- else
- redirect_to action: :week, view_mode: default_view_mode
+ case mode
+ when :day then load_time_entries(date)
+ when :week then load_time_entries(date.all_week)
+ when :month then load_time_entries(date.all_month)
end
end
- def day
- load_time_entries(current_day)
- end
-
- def week
- load_time_entries(current_day.all_week)
+ def refresh
+ if mode == :month
+ load_time_entries(date.all_week)
+ else
+ load_time_entries(date)
+ end
end
- def month
- load_time_entries(current_day.all_month)
- end
+ private
- def today?
- current_day == Time.zone.today
+ def date
+ @date ||= parsed_date || current_date
end
- def this_week?
- current_day == Time.zone.today.beginning_of_week
+ def parsed_date
+ if params[:date].present?
+ begin
+ Date.iso8601(params[:date])
+ rescue StandardError
+ nil
+ end
+ end
end
- def this_month?
- current_day == Time.zone.today.beginning_of_month
+ def default_mode
+ if mobile?
+ "day"
+ else
+ "week"
+ end
end
- private
-
- def current_day
- return @current_day if defined?(@current_day)
-
- parsed_date = if params[:date].present?
- begin
- Date.iso8601(params[:date])
- rescue StandardError
- nil
- end
- end
-
- @current_day = parsed_date || current_date
+ def mode
+ @mode ||= (params[:mode].presence || default_mode).to_sym
end
def default_view_mode
@@ -97,15 +95,11 @@ module My
end
def view_mode
- if ["calendar", "list"].include?(params[:view_mode])
- params[:view_mode]
- else
- default_view_mode
- end.to_sym
+ @view_mode ||= (params[:view_mode].presence || default_view_mode).to_sym
end
def current_date
- case params[:action].to_sym
+ case mode
when :day then Time.zone.today
when :week then Time.zone.today.beginning_of_week
when :month then Time.zone.today.beginning_of_month
@@ -124,14 +118,14 @@ module My
if view_mode == :list
My::TimeTracking::ListComponent.new(
time_entries: @time_entries,
- mode: params[:action].to_sym,
- date: current_day
+ mode: mode,
+ date: date
)
else
My::TimeTracking::CalendarComponent.new(
time_entries: @time_entries,
- mode: params[:action].to_sym,
- date: current_day
+ mode: mode,
+ date: date
)
end
end
diff --git a/modules/costs/app/views/my/time_tracking/day.html.erb b/modules/costs/app/views/my/time_tracking/index.html.erb
similarity index 100%
rename from modules/costs/app/views/my/time_tracking/day.html.erb
rename to modules/costs/app/views/my/time_tracking/index.html.erb
diff --git a/modules/costs/app/views/my/time_tracking/month.html.erb b/modules/costs/app/views/my/time_tracking/month.html.erb
deleted file mode 100644
index b3f3712cf7d..00000000000
--- a/modules/costs/app/views/my/time_tracking/month.html.erb
+++ /dev/null
@@ -1 +0,0 @@
-<%= render(list_view_component) %>
diff --git a/modules/costs/app/views/my/time_tracking/week.html.erb b/modules/costs/app/views/my/time_tracking/week.html.erb
deleted file mode 100644
index b3f3712cf7d..00000000000
--- a/modules/costs/app/views/my/time_tracking/week.html.erb
+++ /dev/null
@@ -1 +0,0 @@
-<%= render(list_view_component) %>
diff --git a/modules/costs/config/routes.rb b/modules/costs/config/routes.rb
index 642a837e29f..856b91dc7e4 100644
--- a/modules/costs/config/routes.rb
+++ b/modules/costs/config/routes.rb
@@ -50,13 +50,15 @@ Rails.application.routes.draw do
namespace "my" do
get "/timer" => "timer#show", as: "timers"
- get "/time-tracking" => "time_tracking#index"
- get "/time-tracking/day(-:view_mode)(/:date)" => "time_tracking#day",
- as: :time_tracking_day
- get "/time-tracking/week(-:view_mode)(/:date)" => "time_tracking#week",
- as: :time_tracking_week
- get "/time-tracking/month(-:view_mode)(/:date)" => "time_tracking#month",
- as: :time_tracking_month
+ get "/time-tracking/(:mode-:view_mode)(/:date)" => "time_tracking#index",
+ as: :time_tracking,
+ constraints: {
+ mode: /day|week|month/,
+ view_mode: /list|calendar/,
+ date: /\d{4}-\d{2}-\d{2}/
+ }
+ get "/time-tracking/refresh" => "time_tracking#refresh",
+ as: :time_tracking_refresh
end
scope "projects/:project_id", as: "project", module: "projects" do
diff --git a/modules/costs/spec/controllers/my/time_tracking_controller_spec.rb b/modules/costs/spec/controllers/my/time_tracking_controller_spec.rb
index 1ec0f15ef15..eefcebe57c0 100644
--- a/modules/costs/spec/controllers/my/time_tracking_controller_spec.rb
+++ b/modules/costs/spec/controllers/my/time_tracking_controller_spec.rb
@@ -48,9 +48,10 @@ RSpec.describe My::TimeTrackingController do
allow(TimeEntry).to receive(:can_track_start_and_end_time?).and_return(true)
end
- it "redirects to the week calendar view" do
+ it "renders the calendar week view" do
get :index
- expect(response).to redirect_to(action: :week, view_mode: "calendar")
+ expect(assigns(:mode)).to eq(:week)
+ expect(assigns(:view_mode)).to eq(:calendar)
end
end
@@ -59,9 +60,10 @@ RSpec.describe My::TimeTrackingController do
allow(TimeEntry).to receive(:can_track_start_and_end_time?).and_return(false)
end
- it "redirects to the week list view" do
+ it "renders the week list view" do
get :index
- expect(response).to redirect_to(action: :week, view_mode: "list")
+ expect(assigns(:mode)).to eq(:week)
+ expect(assigns(:view_mode)).to eq(:list)
end
end
end
@@ -76,9 +78,10 @@ RSpec.describe My::TimeTrackingController do
allow(TimeEntry).to receive(:can_track_start_and_end_time?).and_return(true)
end
- it "redirects to the day calendar view" do
+ it "renders the day calendar view" do
get :index
- expect(response).to redirect_to(action: :day, view_mode: "calendar")
+ expect(assigns(:mode)).to eq(:day)
+ expect(assigns(:view_mode)).to eq(:calendar)
end
end
@@ -87,9 +90,10 @@ RSpec.describe My::TimeTrackingController do
allow(TimeEntry).to receive(:can_track_start_and_end_time?).and_return(false)
end
- it "redirects to the day list view" do
+ it "renders the day list view" do
get :index
- expect(response).to redirect_to(action: :day, view_mode: "list")
+ expect(assigns(:mode)).to eq(:day)
+ expect(assigns(:view_mode)).to eq(:list)
end
end
end
@@ -97,52 +101,52 @@ RSpec.describe My::TimeTrackingController do
describe "GET /my/time-tracking/day" do
it "without a date param it uses the current date" do
- get :day
- expect(assigns(:current_day)).to eq(Date.current)
+ get :index, params: { mode: :day }
+ expect(assigns(:date)).to eq(Date.current)
end
it "with a date param it uses the given date" do
- get :day, params: { date: "2023-12-31" }
- expect(assigns(:current_day)).to eq(Date.parse("2023-12-31"))
+ get :index, params: { mode: :day, date: "2023-12-31" }
+ expect(assigns(:date)).to eq(Date.parse("2023-12-31"))
end
it "with an invalid date param it uses the current date" do
- get :day, params: { date: "invalid-date" }
- expect(assigns(:current_day)).to eq(Date.current)
+ get :index, params: { mode: :day, date: "invalid-date" }
+ expect(assigns(:date)).to eq(Date.current)
end
end
describe "GET /my/time-tracking/week" do
it "without a date param it uses the beginning of current week" do
- get :week
- expect(assigns(:current_day)).to eq(Date.current.beginning_of_week)
+ get :index, params: { mode: :week }
+ expect(assigns(:date)).to eq(Date.current.beginning_of_week)
end
it "with a date param it uses the given date" do
- get :week, params: { date: "2023-12-31" }
- expect(assigns(:current_day)).to eq(Date.parse("2023-12-31"))
+ get :index, params: { mode: :week, date: "2023-12-31" }
+ expect(assigns(:date)).to eq(Date.parse("2023-12-31"))
end
it "with an invalid date param it uses the beginning of current week" do
- get :week, params: { date: "invalid-date" }
- expect(assigns(:current_day)).to eq(Date.current.beginning_of_week)
+ get :index, params: { mode: :week, date: "invalid-date" }
+ expect(assigns(:date)).to eq(Date.current.beginning_of_week)
end
end
describe "GET /my/time-tracking/month" do
it "without a date param it uses the beginning of current month" do
- get :month
- expect(assigns(:current_day)).to eq(Date.current.beginning_of_month)
+ get :index, params: { mode: :month }
+ expect(assigns(:date)).to eq(Date.current.beginning_of_month)
end
it "with a date param it uses the given date" do
- get :month, params: { date: "2023-12-31" }
- expect(assigns(:current_day)).to eq(Date.parse("2023-12-31"))
+ get :index, params: { mode: :month, date: "2023-12-31" }
+ expect(assigns(:date)).to eq(Date.parse("2023-12-31"))
end
it "with an invalid date param it uses the beginning of current month" do
- get :month, params: { date: "invalid-date" }
- expect(assigns(:current_day)).to eq(Date.current.beginning_of_month)
+ get :index, params: { mode: :month, date: "invalid-date" }
+ expect(assigns(:date)).to eq(Date.current.beginning_of_month)
end
end
end