Skip to content

Commit

Permalink
Enable morphing the Stages and Gates form preview action
Browse files Browse the repository at this point in the history
  • Loading branch information
dombesz committed Dec 17, 2024
1 parent 4bd144d commit 8040262
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
9 changes: 8 additions & 1 deletion app/components/concerns/op_turbo/streamable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class MissingComponentWrapper < StandardError; end
# rubocop:enable OpenProject/AddPreviewForViewComponent

INLINE_ACTIONS = %i[dialog flash].freeze
# Turbo allows the response method for these actions only:
ACTIONS_WITH_METHOD = %i[update replace].freeze

extend ActiveSupport::Concern

Expand All @@ -43,7 +45,7 @@ def wrapper_key
end

included do
def render_as_turbo_stream(view_context:, action: :update)
def render_as_turbo_stream(view_context:, action: :update, method: nil)
case action
when :update, *INLINE_ACTIONS
@inner_html_only = true
Expand All @@ -63,8 +65,13 @@ def render_as_turbo_stream(view_context:, action: :update)
"Wrap your component in a `component_wrapper` block in order to use turbo-stream methods"
end

if method && !action.in?(ACTIONS_WITH_METHOD)
raise ArgumentError, "The #{action} action does not supports a method"
end

OpTurbo::StreamComponent.new(
action:,
method:,
target: wrapper_key,
template:
).render_in(view_context)
Expand Down
13 changes: 7 additions & 6 deletions app/controllers/concerns/op_turbo/component_stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,24 @@ def respond_to_with_turbo_streams(status: turbo_status, &format_block)

alias_method :respond_with_turbo_streams, :respond_to_with_turbo_streams

def update_via_turbo_stream(component:, status: :ok)
modify_via_turbo_stream(component:, action: :update, status:)
def update_via_turbo_stream(component:, status: :ok, method: nil)
modify_via_turbo_stream(component:, action: :update, status:, method:)
end

def replace_via_turbo_stream(component:, status: :ok)
modify_via_turbo_stream(component:, action: :replace, status:)
def replace_via_turbo_stream(component:, status: :ok, method: nil)
modify_via_turbo_stream(component:, action: :replace, status:, method:)
end

def remove_via_turbo_stream(component:, status: :ok)
modify_via_turbo_stream(component:, action: :remove, status:)
end

def modify_via_turbo_stream(component:, action:, status:)
def modify_via_turbo_stream(component:, action:, status:, method: nil)
@turbo_status = status
turbo_streams << component.render_as_turbo_stream(
view_context:,
action:
action:,
method:
)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ export default class ProjectLifeCyclesFormController extends Controller {
return; // flatpickr is still open, do not submit yet.
}

// TODO: If morphing is working correctly, we don't need to blur the input field.
target.blur();
const form = this.formTarget;
form.action = previewUrl;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ def project_life_cycles_form
.call(permitted_params.project_life_cycles)

update_via_turbo_stream(
component: ProjectLifeCycles::Sections::EditComponent.new(service_call.result)
component: ProjectLifeCycles::Sections::EditComponent.new(service_call.result),
method: "morph"
)
# TODO: :unprocessable_entity is not nice, change the dialog logic to accept :ok
# without dismissing the dialog, alternatively use turbo frames instead of streams.
Expand Down

0 comments on commit 8040262

Please sign in to comment.