Skip to content

Commit

Permalink
handle responses for destroy/move/drop using turbo streams
Browse files Browse the repository at this point in the history
  • Loading branch information
toy committed Dec 18, 2024
1 parent bfa79b6 commit 8165066
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,98 +28,100 @@ See COPYRIGHT and LICENSE files for more details.
++#%>

<%=
flex_layout(data: wrapper_data_attributes) do |flex|
flex.with_row do
if allowed_to_customize_life_cycle?
render(Primer::OpenProject::SubHeader.new) do |subheader|
subheader.with_filter_input(
name: "border-box-filter",
label: t("settings.project_life_cycle_step_definitions.filter.label"),
visually_hide_label: true,
placeholder: t("settings.project_life_cycle_step_definitions.filter.label"),
leading_visual: {
icon: :search,
size: :small
},
show_clear_button: true,
data: {
action: "input->projects--settings--border-box-filter#filterLists",
"projects--settings--border-box-filter-target": "filter"
}
)
subheader.with_action_component do
render(Primer::Alpha::ActionMenu.new(
anchor_align: :end)
) do |menu|
menu.with_show_button(
scheme: :primary,
aria: { label: I18n.t("settings.project_life_cycle_step_definitions.label_add_description") },
) do |button|
button.with_leading_visual_icon(icon: :plus)
button.with_trailing_action_icon(icon: :"triangle-down")
I18n.t("settings.project_life_cycle_step_definitions.label_add")
end
component_wrapper do
flex_layout(data: wrapper_data_attributes) do |flex|
flex.with_row do
if allowed_to_customize_life_cycle?
render(Primer::OpenProject::SubHeader.new) do |subheader|
subheader.with_filter_input(
name: "border-box-filter",
label: t("settings.project_life_cycle_step_definitions.filter.label"),
visually_hide_label: true,
placeholder: t("settings.project_life_cycle_step_definitions.filter.label"),
leading_visual: {
icon: :search,
size: :small
},
show_clear_button: true,
data: {
action: "input->projects--settings--border-box-filter#filterLists",
"projects--settings--border-box-filter-target": "filter"
}
)
subheader.with_action_component do
render(Primer::Alpha::ActionMenu.new(
anchor_align: :end)
) do |menu|
menu.with_show_button(
scheme: :primary,
aria: { label: I18n.t("settings.project_life_cycle_step_definitions.label_add_description") },
) do |button|
button.with_leading_visual_icon(icon: :plus)
button.with_trailing_action_icon(icon: :"triangle-down")
I18n.t("settings.project_life_cycle_step_definitions.label_add")
end

menu.with_item(
label: I18n.t("settings.project_life_cycle_step_definitions.label_add_stage"),
href: new_stage_admin_settings_project_life_cycle_step_definitions_path
) do |item|
item.with_leading_visual_icon(icon: "git-commit")
end
menu.with_item(
label: I18n.t("settings.project_life_cycle_step_definitions.label_add_stage"),
href: new_stage_admin_settings_project_life_cycle_step_definitions_path
) do |item|
item.with_leading_visual_icon(icon: "git-commit")
end

menu.with_item(
label: I18n.t("settings.project_life_cycle_step_definitions.label_add_gate"),
href: new_gate_admin_settings_project_life_cycle_step_definitions_path
) do |item|
item.with_leading_visual_icon(icon: "diamond")
menu.with_item(
label: I18n.t("settings.project_life_cycle_step_definitions.label_add_gate"),
href: new_gate_admin_settings_project_life_cycle_step_definitions_path
) do |item|
item.with_leading_visual_icon(icon: "diamond")
end
end
end
end
else
render EnterpriseEdition::BannerComponent.new(:customize_life_cycle, mb: 3)
end
else
render EnterpriseEdition::BannerComponent.new(:customize_life_cycle, mb: 3)
end
end

flex.with_row do
render(border_box_container(mb: 3, data: drop_target_config)) do |component|
component.with_header(font_weight: :bold, py: 2) do
flex_layout(justify_content: :space_between, align_items: :center) do |header_container|
header_container.with_column do
render(Primer::Beta::Text.new(font_weight: :bold)) do
I18n.t("settings.project_life_cycle_step_definitions.section_header")
flex.with_row do
render(border_box_container(mb: 3, data: drop_target_config)) do |component|
component.with_header(font_weight: :bold, py: 2) do
flex_layout(justify_content: :space_between, align_items: :center) do |header_container|
header_container.with_column do
render(Primer::Beta::Text.new(font_weight: :bold)) do
I18n.t("settings.project_life_cycle_step_definitions.section_header")
end
end
end
end
end
if definitions.empty?
component.with_row do
render(Primer::Beta::Text.new(color: :subtle)) do
t("settings.project_life_cycle_step_definitions.non_defined")
if definitions.empty?
component.with_row do
render(Primer::Beta::Text.new(color: :subtle)) do
t("settings.project_life_cycle_step_definitions.non_defined")
end
end
end
else
definitions.each do |definition|
component.with_row(
data: {
"projects--settings--border-box-filter-target": "searchItem",
**draggable_item_config(definition)
}
) do
render(Settings::ProjectLifeCycleStepDefinitions::RowComponent.new(
definition,
allowed_to_customize_life_cycle?: allowed_to_customize_life_cycle?,
first?: definition == definitions.first,
last?: definition == definitions.last,
))
else
definitions.each do |definition|
component.with_row(
data: {
"projects--settings--border-box-filter-target": "searchItem",
**draggable_item_config(definition)
}
) do
render(Settings::ProjectLifeCycleStepDefinitions::RowComponent.new(
definition,
allowed_to_customize_life_cycle?: allowed_to_customize_life_cycle?,
first?: definition == definitions.first,
last?: definition == definitions.last,
))
end
end
end
end
end
end
flex.with_row(display: :none, data: { "projects--settings--border-box-filter-target": "noResultsText" }) do
render Primer::Beta::Text.new do
I18n.t("js.autocompleter.notFoundText")
flex.with_row(display: :none, data: { "projects--settings--border-box-filter-target": "noResultsText" }) do
render Primer::Beta::Text.new do
I18n.t("js.autocompleter.notFoundText")
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module Settings
module ProjectLifeCycleStepDefinitions
class IndexComponent < ApplicationComponent
include OpPrimer::ComponentHelpers
include OpTurbo::Streamable

options :definitions,
:allowed_to_customize_life_cycle?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@

module Admin::Settings
class ProjectLifeCycleStepDefinitionsController < ::Admin::SettingsController
include FlashMessagesOutputSafetyHelper
include OpTurbo::ComponentStream

menu_item :project_life_cycle_step_definitions_settings

helper_method :allowed_to_customize_life_cycle?
Expand Down Expand Up @@ -78,35 +81,34 @@ def update

def destroy
if @definition.destroy
flash[:notice] = I18n.t(:notice_successful_delete)
update_flash_message_via_turbo_stream(message: I18n.t(:notice_successful_delete))
else
# TODO: handle better
flash[:error] = I18n.t(:notice_bad_request)
render_error_flash_message_via_turbo_stream(message: join_flash_messages(@definition.errors.full_messages))
end

redirect_to action: :index, status: :see_other
update_definitions_via_turbo_stream

respond_to_with_turbo_streams
end

def move
if @definition.update(params.permit(:move_to))
flash[:notice] = I18n.t(:notice_successful_update)
else
# TODO: handle better
flash[:error] = I18n.t(:notice_bad_request)
unless @definition.update(params.permit(:move_to))
render_error_flash_message_via_turbo_stream(message: join_flash_messages(@definition.errors.full_messages))
end

redirect_to action: :index, status: :see_other
update_definitions_via_turbo_stream

respond_to_with_turbo_streams
end

def drop
if @definition.update(params.permit(:position))
flash[:notice] = I18n.t(:notice_successful_update)
else
# TODO: handle better
flash[:error] = I18n.t(:notice_bad_request)
unless @definition.update(params.permit(:position))
render_error_flash_message_via_turbo_stream(message: join_flash_messages(@definition.errors.full_messages))
end

redirect_to action: :index, status: :see_other
update_definitions_via_turbo_stream

respond_to_with_turbo_streams
end

private
Expand Down Expand Up @@ -134,5 +136,14 @@ def find_definition
def definition_params
params.require(:project_life_cycle_step_definition).permit(:type, :name, :color_id)
end

def update_definitions_via_turbo_stream
update_via_turbo_stream(
component: Settings::ProjectLifeCycleStepDefinitions::IndexComponent.new(
definitions: find_definitions,
allowed_to_customize_life_cycle?: allowed_to_customize_life_cycle?
)
)
end
end
end

0 comments on commit 8165066

Please sign in to comment.