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

Implement drag & drop sorting for time entry activities

Klaus Zanders42e4c1237dea
diff --git a/modules/costs/app/components/admin/time_entry_activities/activity_component.html.erb b/modules/costs/app/components/admin/time_entry_activities/activity_component.html.erb
index 44194f0d36e..cd5930dd03e 100644
--- a/modules/costs/app/components/admin/time_entry_activities/activity_component.html.erb
+++ b/modules/costs/app/components/admin/time_entry_activities/activity_component.html.erb
@@ -1,30 +1,32 @@
<%=
- flex_layout(align_items: :center, justify_content: :space_between) do |activity_container|
- activity_container.with_column(flex_layout: true) do |activity_info|
- activity_info.with_column(mr: 2) do
- render(Primer::OpenProject::DragHandle.new(draggable: true))
- end
+ component_wrapper do
+ flex_layout(align_items: :center, justify_content: :space_between) do |activity_container|
+ activity_container.with_column(flex_layout: true) do |activity_info|
+ activity_info.with_column(mr: 2) do
+ render(Primer::OpenProject::DragHandle.new(draggable: true))
+ end
- activity_info.with_column(mr: 2) do
- render(Primer::Beta::Link.new(href: edit_admin_time_entry_activity_path(activity), underline: false)) do
- render(Primer::Beta::Text.new(font_weight: :bold)) { activity.name }
+ activity_info.with_column(mr: 2) do
+ render(Primer::Beta::Link.new(href: edit_admin_time_entry_activity_path(activity), underline: false)) do
+ render(Primer::Beta::Text.new(font_weight: :bold)) { activity.name }
+ end
end
- end
- activity_info.with_column(mr: 2) do
- render(Primer::Beta::Text.new(color: :subtle)) { activity.active? ? "Active" : "Inactive" }
+ activity_info.with_column(mr: 2) do
+ render(Primer::Beta::Text.new(color: :subtle)) { activity.active? ? "Active" : "Inactive" }
+ end
end
- end
- activity_container.with_column do
- render(Primer::Alpha::ActionMenu.new(test_selector: "op-time-entry-acrtivity--action-menu")) do |menu|
- menu.with_show_button(
- icon: "kebab-horizontal",
- scheme: :invisible,
- "aria-label": "Actions"
- )
+ activity_container.with_column do
+ render(Primer::Alpha::ActionMenu.new(test_selector: "op-time-entry-acrtivity--action-menu")) do |menu|
+ menu.with_show_button(
+ icon: "kebab-horizontal",
+ scheme: :invisible,
+ "aria-label": "Actions"
+ )
- build_activity_menu(menu)
+ build_activity_menu(menu)
+ end
end
end
end
diff --git a/modules/costs/app/components/admin/time_entry_activities/activity_component.rb b/modules/costs/app/components/admin/time_entry_activities/activity_component.rb
index d8f3c6839f7..16e9c4ec6e9 100644
--- a/modules/costs/app/components/admin/time_entry_activities/activity_component.rb
+++ b/modules/costs/app/components/admin/time_entry_activities/activity_component.rb
@@ -33,12 +33,17 @@ module Admin
class ActivityComponent < ApplicationComponent
include ApplicationHelper
include OpPrimer::ComponentHelpers
+ include OpTurbo::Streamable
options :activity
options :max_activity_position
private
+ def wrapper_uniq_by
+ activity.id
+ end
+
def first_item?
activity.position == 1
end
@@ -77,7 +82,7 @@ module Admin
tag: :button,
href: move_admin_time_entry_activity_path(activity),
# content_arguments: { data: { turbo_frame: ItemsComponent.wrapper_key } },
- form_arguments: { method: :post, inputs: form_inputs }) do |item|
+ form_arguments: { method: :put, inputs: form_inputs }) do |item|
item.with_leading_visual_icon(icon: "move-to-top")
end
end
@@ -89,7 +94,7 @@ module Admin
tag: :button,
href: move_admin_time_entry_activity_path(activity),
# content_arguments: { data: { turbo_frame: ItemsComponent.wrapper_key } },
- form_arguments: { method: :post, inputs: form_inputs }) do |item|
+ form_arguments: { method: :put, inputs: form_inputs }) do |item|
item.with_leading_visual_icon(icon: "chevron-up")
end
end
@@ -101,7 +106,7 @@ module Admin
tag: :button,
href: move_admin_time_entry_activity_path(activity),
# content_arguments: { data: { turbo_frame: ItemsComponent.wrapper_key } },
- form_arguments: { method: :post, inputs: form_inputs }) do |item|
+ form_arguments: { method: :put, inputs: form_inputs }) do |item|
item.with_leading_visual_icon(icon: "chevron-down")
end
end
@@ -113,7 +118,7 @@ module Admin
tag: :button,
href: move_admin_time_entry_activity_path(activity),
# content_arguments: { data: { turbo_frame: ItemsComponent.wrapper_key } },
- form_arguments: { method: :post, inputs: form_inputs }) do |item|
+ form_arguments: { method: :put, inputs: form_inputs }) do |item|
item.with_leading_visual_icon(icon: "move-to-bottom")
end
end
diff --git a/modules/costs/app/components/admin/time_entry_activities/index_component.html.erb b/modules/costs/app/components/admin/time_entry_activities/index_component.html.erb
index 96122131a93..d2da87b5829 100644
--- a/modules/costs/app/components/admin/time_entry_activities/index_component.html.erb
+++ b/modules/costs/app/components/admin/time_entry_activities/index_component.html.erb
@@ -29,7 +29,7 @@
end
flex.with_row do
- render(border_box_container(data: { test_selector: "admin-time-entry-activity" })) do |component|
+ render(border_box_container(data: drop_target_config)) do |component|
component.with_header(font_weight: :bold) { "Time entry activities" }
if time_entry_activities.empty?
@@ -38,7 +38,7 @@
end
else
time_entry_activities.each do |activity|
- component.with_row(test_selector: "time-entry-activity-row-#{activity.id}") do
+ component.with_row(test_selector: "time-entry-activity-row-#{activity.id}", data: draggable_item_config(activity)) do
render(Admin::TimeEntryActivities::ActivityComponent.new(activity: activity, max_activity_position: max_activity_position))
end
end
diff --git a/modules/costs/app/components/admin/time_entry_activities/index_component.rb b/modules/costs/app/components/admin/time_entry_activities/index_component.rb
index aa10de1c8a0..74bb76c0ca9 100644
--- a/modules/costs/app/components/admin/time_entry_activities/index_component.rb
+++ b/modules/costs/app/components/admin/time_entry_activities/index_component.rb
@@ -33,6 +33,7 @@ module Admin
class IndexComponent < ApplicationComponent
include ApplicationHelper
include OpPrimer::ComponentHelpers
+ include OpTurbo::Streamable
options :time_entry_activities
@@ -44,10 +45,26 @@ module Admin
def wrapper_data_attributes
{
- # controller: "projects--settings--border-box-filter",
+ controller: "generic-drag-and-drop",
"application-target": "dynamic"
}
end
+
+ def drop_target_config
+ {
+ "is-drag-and-drop-target": true,
+ "target-container-accessor": "& > ul",
+ "target-allowed-drag-type": "time-entry-activity"
+ }
+ end
+
+ def draggable_item_config(activity)
+ {
+ "draggable-id": activity.id,
+ "draggable-type": "time-entry-activity",
+ "drop-url": move_admin_time_entry_activity_path(activity)
+ }
+ end
end
end
end
diff --git a/modules/costs/app/controllers/admin/time_entry_activities_controller.rb b/modules/costs/app/controllers/admin/time_entry_activities_controller.rb
index f78bdb03f8c..ef64ce661f1 100644
--- a/modules/costs/app/controllers/admin/time_entry_activities_controller.rb
+++ b/modules/costs/app/controllers/admin/time_entry_activities_controller.rb
@@ -80,7 +80,7 @@ module Admin
end
def move
- if @time_entry_activity.update(move_to: move_to_attribute)
+ if @time_entry_activity.update(move_params)
flash[:notice] = I18n.t(:notice_successful_update)
else
flash[:error] = I18n.t(:error_type_could_not_be_saved)
@@ -95,13 +95,16 @@ module Admin
private
- def move_to_attribute
+ def move_params
move_to = params[:move_to]
+ position = Integer(params[:position], exception: false)
if move_to.in? %w(highest higher lower lowest)
- move_to
+ { move_to: move_to }
+ elsif position
+ { position: position }
else
- raise ArgumentError, "Invalid move_to value: #{move_to}"
+ {}
end
end
diff --git a/modules/costs/config/routes.rb b/modules/costs/config/routes.rb
index ab83cb18271..e3037a9cb60 100644
--- a/modules/costs/config/routes.rb
+++ b/modules/costs/config/routes.rb
@@ -71,7 +71,7 @@ Rails.application.routes.draw do
namespace :admin do
resources :time_entry_activities, except: [:show] do
member do
- post :move
+ put :move
get :reassign
end
end