Skip to content

Commit

Permalink
refactor: join attach and actions modal to use the same turbo_frame (
Browse files Browse the repository at this point in the history
…#3275)

* refactor: join attach and actions modal

* fix method name

* try 7.2

* Revert "try 7.2"

This reverts commit 0f3b569.

* fix attach modal to not clear the background

* fix has one attach data

* extract url from associations view to controller

* lint
  • Loading branch information
Paul-Bob authored Sep 28, 2024
1 parent 8f4220e commit b0d8e48
Show file tree
Hide file tree
Showing 16 changed files with 35 additions and 39 deletions.
2 changes: 1 addition & 1 deletion app/components/avo/actions_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def render_action_link(action, icon: nil)
def action_data_attributes(action)
{
action_name: action.action_name,
"turbo-frame": Avo::ACTIONS_TURBO_FRAME_ID,
"turbo-frame": Avo::MODAL_FRAME_ID,
action: "click->actions-picker#visitAction",
"actions-picker-target": action.standalone ? "standaloneAction" : "resourceAction",
disabled: is_disabled?(action),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
icon: 'heroicons/outline/link',
color: :primary,
style: :text,
'data-turbo-frame': 'attach_modal' do %>
data: {
turbo_frame: Avo::MODAL_FRAME_ID,
target: :attach
} do %>
<%= t('avo.attach_item', item: @field.name.humanize(capitalize: false)) %>
<% end %>
<% end %>
Expand Down
4 changes: 2 additions & 2 deletions app/components/avo/resource_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def render_attach_button(control)
color: :primary,
style: :text,
data: {
turbo_frame: :attach_modal,
turbo_frame: Avo::MODAL_FRAME_ID,
target: :attach
} do
control.label
Expand Down Expand Up @@ -282,7 +282,7 @@ def render_action(action)
title: action.title,
size: action.size,
data: {
turbo_frame: Avo::ACTIONS_TURBO_FRAME_ID,
turbo_frame: Avo::MODAL_FRAME_ID,
action_name: action.action.action_name,
tippy: action.title ? :tooltip : nil,
action: "click->actions-picker#visitAction",
Expand Down
7 changes: 3 additions & 4 deletions app/controllers/avo/actions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,13 @@ def respond
# Trigger download, removes modal and flash the messages
[
turbo_stream.download(content: Base64.encode64(@response[:path]), filename: @response[:filename]),
turbo_stream.close_action_modal,
turbo_stream.close_modal,
turbo_stream.flash_alerts
]
when :navigate_to_action
frame_id = Avo::ACTIONS_TURBO_FRAME_ID
src, _ = @response[:action].link_arguments(resource: @action.resource, **@response[:navigate_to_action_args])

turbo_stream.turbo_frame_set_src(frame_id, src)
turbo_stream.turbo_frame_set_src(Avo::MODAL_FRAME_ID, src)
when :redirect
turbo_stream.redirect_to(
Avo::ExecutionContext.new(target: @response[:path]).handle,
Expand All @@ -118,7 +117,7 @@ def respond
when :close_modal
# Close the modal and flash the messages
[
turbo_stream.close_action_modal,
turbo_stream.close_modal,
turbo_stream.flash_alerts
]
else
Expand Down
10 changes: 10 additions & 0 deletions app/controllers/avo/associations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ def new
[@attachment_resource.new(record: record).record_title, record.to_param]
end
end

@url = Avo::Services::URIService.parse(avo.root_url.to_s)
.append_paths("resources", params[:resource_name], params[:id], params[:related_name])
.append_query(
{
view: @resource&.view&.to_s,
for_attribute: @field&.try(:for_attribute)
}.compact
)
.to_s
end

def create
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/avo/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def new

# Handle special cases when creating a new record via a belongs_to relationship
if params[:via_belongs_to_resource_class].present?
return render turbo_stream: turbo_stream.append("attach_modal", partial: "avo/base/new_via_belongs_to")
return render turbo_stream: turbo_stream.append(Avo::MODAL_FRAME_ID, partial: "avo/base/new_via_belongs_to")
end

set_actions
Expand Down
6 changes: 3 additions & 3 deletions app/helpers/avo/turbo_stream_actions_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ def flash_alerts
template: @view_context.render(Avo::FlashAlertsComponent.new(flashes: @view_context.flash.discard))
end

def close_action_modal
def close_modal
turbo_stream_action_tag :replace,
target: Avo::ACTIONS_TURBO_FRAME_ID,
template: @view_context.turbo_frame_tag(Avo::ACTIONS_TURBO_FRAME_ID, data: {turbo_temporary: 1})
target: Avo::MODAL_FRAME_ID,
template: @view_context.turbo_frame_tag(Avo::MODAL_FRAME_ID, data: {turbo_temporary: 1})
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/js/controllers/actions_picker_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default class extends Controller {
}

get actionsShowTurboFrame() {
return document.querySelector('turbo-frame#actions_show')
return document.querySelector(`turbo-frame#${window.Avo.configuration.modal_frame_id}`)
}

enableTarget() {
Expand Down
2 changes: 1 addition & 1 deletion app/views/avo/actions/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<%= render Avo::LoadingComponent.new(title: "...") %>
<% end %>

<%= turbo_frame_tag Avo::ACTIONS_TURBO_FRAME_ID do %>
<%= turbo_frame_tag Avo::MODAL_FRAME_ID do %>
<div
data-controller="<%= ["action", @action.get_stimulus_controllers].join(" ") %>"
data-action-no-confirmation-value="<%= @action.no_confirmation %>"
Expand Down
17 changes: 3 additions & 14 deletions app/views/avo/associations/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
<%= turbo_frame_tag 'attach_modal' do %>
<%
url = Avo::Services::URIService.parse(avo.root_url.to_s)
.append_paths('resources', params[:resource_name], params[:id], params[:related_name])
.append_query(
{
view: @resource&.view&.to_s,
for_attribute: @field&.try(:for_attribute)
}.compact
)
.to_s
%>
<%= turbo_frame_tag Avo::MODAL_FRAME_ID do %>
<%= form_with scope: 'fields',
url: url,
url: @url,
local: true,
data: {
'turbo-frame': '_top'
turbo_frame: :_top
} do |form| %>
<%= render Avo::ModalComponent.new do |c| %>
<% c.with_heading do %>
Expand Down
1 change: 0 additions & 1 deletion app/views/avo/partials/_attach_modal.html.erb

This file was deleted.

1 change: 1 addition & 0 deletions app/views/avo/partials/_javascript.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
param_key: '<%= Avo::DynamicFilters.configuration.param_key %>'
}
<% end %>
Avo.configuration.modal_frame_id = '<%= ::Avo::MODAL_FRAME_ID %>'
<% end %>
3 changes: 1 addition & 2 deletions app/views/layouts/avo/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@
</div>
</div>
</div>
<%= turbo_frame_tag Avo::ACTIONS_TURBO_FRAME_ID, data: {turbo_temporary: 1} %>
<%= render partial: "avo/partials/attach_modal" %>
<%= turbo_frame_tag Avo::MODAL_FRAME_ID, data: {turbo_temporary: 1} %>
<%= render partial: "avo/partials/alerts" %>
<%= render partial: "avo/partials/scripts" %>
<%= render partial: "avo/partials/confirm_dialog" %>
Expand Down
2 changes: 1 addition & 1 deletion lib/avo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module Avo
IN_DEVELOPMENT = ENV["AVO_IN_DEVELOPMENT"] == "1"
PACKED = !IN_DEVELOPMENT
COOKIES_KEY = "avo"
ACTIONS_TURBO_FRAME_ID = :actions_show
MODAL_FRAME_ID = :modal_frame
ACTIONS_BACKGROUND_FRAME = :actions_background

class LicenseVerificationTemperedError < StandardError; end
Expand Down
6 changes: 1 addition & 5 deletions lib/avo/base_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,7 @@ def link_arguments(resource:, arguments: {}, **args)
)
.to_s

data = {
turbo_frame: Avo::ACTIONS_TURBO_FRAME_ID,
}

[path, data]
[path, {turbo_frame: Avo::MODAL_FRAME_ID}]
end

# Encrypt the arguments so we can pass sensible data as a query param.
Expand Down
4 changes: 2 additions & 2 deletions spec/system/avo/actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@

click_on "Actions"
click_on "Do Nothing"
expect(page).to have_css('turbo-frame#actions_show')
expect(page).to have_css("turbo-frame#modal_frame")
expect(page).to have_selector(modal = "[role='dialog']")
click_on "Run"
expect(page).not_to have_selector(modal)
Expand All @@ -214,7 +214,7 @@

click_on "Actions"
click_on "Close modal"
expect(page).to have_css('turbo-frame#actions_show')
expect(page).to have_css("turbo-frame#modal_frame")
expect(page).to have_selector(modal = "[role='dialog']")
click_on "Run"
expect(page).not_to have_selector(modal)
Expand Down

0 comments on commit b0d8e48

Please sign in to comment.