Skip to content

Commit 9d10847

Browse files
committed
handle drag-n-drop ordering, except for expected turbo stream response
1 parent 715c5dc commit 9d10847

File tree

5 files changed

+38
-8
lines changed

5 files changed

+38
-8
lines changed

app/components/settings/project_life_cycle_step_definitions/index_component.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ See COPYRIGHT and LICENSE files for more details.
5050
end
5151

5252
flex.with_row do
53-
render(border_box_container(mb: 3)) do |component|
53+
render(border_box_container(mb: 3, data: drop_target_config)) do |component|
5454
component.with_header(font_weight: :bold, py: 2) do
5555
flex_layout(justify_content: :space_between, align_items: :center) do |header_container|
5656
header_container.with_column do
@@ -66,7 +66,7 @@ See COPYRIGHT and LICENSE files for more details.
6666
end
6767
else
6868
definitions.each do |definition|
69-
component.with_row(data: { "projects--settings--border-box-filter-target": "searchItem" }) do
69+
component.with_row(data: { "projects--settings--border-box-filter-target": "searchItem", **draggable_item_config(definition) }) do
7070
render(Settings::ProjectLifeCycleStepDefinitions::RowComponent.new(
7171
definition,
7272
first?: definition == definitions.first,

app/components/settings/project_life_cycle_step_definitions/index_component.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,25 @@ class IndexComponent < ApplicationComponent
3737

3838
def wrapper_data_attributes
3939
{
40-
controller: "projects--settings--border-box-filter",
40+
controller: "projects--settings--border-box-filter generic-drag-and-drop",
4141
"application-target": "dynamic"
4242
}
4343
end
44+
45+
def drop_target_config
46+
{
47+
"is-drag-and-drop-target": true,
48+
"target-container-accessor": "& > ul",
49+
"target-allowed-drag-type": "life-cycle-step-definition"
50+
}
51+
end
52+
53+
def draggable_item_config(definition)
54+
{
55+
"draggable-type": "life-cycle-step-definition",
56+
"drop-url": drop_admin_settings_project_life_cycle_step_definition_path(definition)
57+
}
58+
end
4459
end
4560
end
4661
end

app/components/settings/project_life_cycle_step_definitions/row_component.html.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ See COPYRIGHT and LICENSE files for more details.
3030
<%=
3131
flex_layout(align_items: :center, justify_content: :space_between) do |row_container|
3232
row_container.with_column(flex_layout: true, classes: "gap-2") do |title_container|
33+
title_container.with_column do
34+
render(Primer::OpenProject::DragHandle.new)
35+
end
3336
title_container.with_column do
3437
render(Primer::Beta::Link.new(
3538
classes: 'filter-target-visible-text',

app/controllers/admin/settings/project_life_cycle_step_definitions_controller.rb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ProjectLifeCycleStepDefinitionsController < ::Admin::SettingsController
3333
before_action :check_feature_flag
3434

3535
before_action :find_definitions, only: %i[index]
36-
before_action :find_definition, only: %i[edit update destroy move]
36+
before_action :find_definition, only: %i[edit update destroy move drop]
3737

3838
def index; end
3939

@@ -58,7 +58,7 @@ def create
5858

5959
if @definition.save
6060
flash[:notice] = I18n.t(:notice_successful_create)
61-
redirect_to action: :index
61+
redirect_to action: :index, status: :see_other
6262
else
6363
render :form, status: :unprocessable_entity
6464
end
@@ -67,7 +67,7 @@ def create
6767
def update
6868
if @definition.update(definition_params)
6969
flash[:notice] = I18n.t(:notice_successful_update)
70-
redirect_to action: :index
70+
redirect_to action: :index, status: :see_other
7171
else
7272
render :form, status: :unprocessable_entity
7373
end
@@ -81,7 +81,7 @@ def destroy
8181
flash[:error] = I18n.t(:notice_bad_request)
8282
end
8383

84-
redirect_to action: :index
84+
redirect_to action: :index, status: :see_other
8585
end
8686

8787
def move
@@ -92,7 +92,18 @@ def move
9292
flash[:error] = I18n.t(:notice_bad_request)
9393
end
9494

95-
redirect_to action: :index
95+
redirect_to action: :index, status: :see_other
96+
end
97+
98+
def drop
99+
if @definition.update(params.permit(:position))
100+
flash[:notice] = I18n.t(:notice_successful_update)
101+
else
102+
# TODO: handle better
103+
flash[:error] = I18n.t(:notice_bad_request)
104+
end
105+
106+
redirect_to action: :index, status: :see_other
96107
end
97108

98109
private

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@
522522
end
523523
member do
524524
patch :move
525+
put :drop # should be patch, but requires passing method to generic-drag-and-drop controller
525526
end
526527
end
527528
resources :project_custom_fields, controller: "/admin/settings/project_custom_fields" do

0 commit comments

Comments
 (0)