Skip to content

Commit

Permalink
wip: eager load corresponding turbo-frame on edit path visit
Browse files Browse the repository at this point in the history
  • Loading branch information
chaimann committed Dec 20, 2024
1 parent 1346608 commit 542eaa1
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= turbo_frame_tag :edit_adjustment_reason_modal do %>
<%= turbo_frame_tag @adjustment_reason, :edit, target: '_top' do %>
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
<%= form_for @adjustment_reason, url: solidus_admin.adjustment_reason_path(@adjustment_reason), html: { id: form_id } do |f| %>
<div class="flex flex-col gap-6 pb-4">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@ def page_actions
end

def turbo_frames
%w[
new_adjustment_reason_modal
edit_adjustment_reason_modal
return @turbo_frames if defined? @turbo_frames

@turbo_frames = [
component('utils/turbo_frame').new(id: dom_id(Spree::AdjustmentReason, :new)),
]

@page.records.each { @turbo_frames << component('utils/turbo_frame').new(id: dom_id(_1, :edit)) }
@turbo_frames
end

def row_url(adjustment_reason)
spree.edit_admin_adjustment_reason_path(adjustment_reason, _turbo_frame: :edit_adjustment_reason_modal)
spree.edit_admin_adjustment_reason_path(adjustment_reason, _turbo_frame: dom_id(adjustment_reason, :edit))
end

def batch_actions
Expand All @@ -52,15 +56,15 @@ def columns
data: ->(adjustment_reason) do
link_to adjustment_reason.name, row_url(adjustment_reason),
class: 'body-link',
data: { turbo_frame: :edit_adjustment_reason_modal, turbo_prefetch: false }
data: { turbo_frame: dom_id(adjustment_reason, :edit), turbo_prefetch: false }
end
},
{
header: :code,
data: ->(adjustment_reason) do
link_to adjustment_reason.code, row_url(adjustment_reason),
class: 'body-link',
data: { turbo_frame: :edit_adjustment_reason_modal, turbo_prefetch: false }
data: { turbo_frame: dom_id(adjustment_reason, :edit), turbo_prefetch: false }
end
},
{
Expand All @@ -71,4 +75,11 @@ def columns
},
]
end

def eager_loaded_frame(frame_id, src)
frame = turbo_frames.index_by(&:id)[frame_id]
return unless frame

frame.src = src
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= turbo_frame_tag :new_adjustment_reason_modal do %>
<%= turbo_frame_tag :new_adjustment_reason do %>
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
<%= form_for @adjustment_reason, url: solidus_admin.adjustment_reasons_path, html: { id: form_id } do |f| %>
<div class="flex flex-col gap-6 pb-4">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@
<% end %>

<% turbo_frames.each do |frame| %>
<%= turbo_frame_tag frame %>
<%= render frame %>
<% end %>
<% end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= turbo_frame_tag(@id, src: @src) %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class SolidusAdmin::Utils::TurboFrame::Component < SolidusAdmin::BaseComponent
attr_reader :id
attr_writer :src

def initialize(id:, src: nil)
@id = id
@src = src
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ def edit
render component('adjustment_reasons/edit').new(adjustment_reason: @adjustment_reason), layout: false
else
set_index_page
component = component('adjustment_reasons/index').new(page: @page)
component.eager_loaded_frame(dom_id(@adjustment_reason, :edit), solidus_admin.edit_adjustment_reason_path(@adjustment_reason))
render component
end
end
end
Expand Down
1 change: 1 addition & 0 deletions admin/app/controllers/solidus_admin/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class BaseController < ApplicationController
include ActiveStorage::SetCurrent
include Spree::Core::ControllerHelpers::Store
include GearedPagination::Controller
include ActionView::RecordIdentifier

include SolidusAdmin::ControllerHelpers::Authentication
include SolidusAdmin::ControllerHelpers::Authorization
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
end

it "renders the edit template with a 200 OK status for Turbo-Frame request" do
get solidus_admin.edit_adjustment_reason_path(adjustment_reason), headers: { "Turbo-Frame": :edit_adjustment_reason_modal }
get solidus_admin.edit_adjustment_reason_path(adjustment_reason), headers: { "Turbo-Frame": "adjustment_reason_#{adjustment_reason.id}_edit" }
expect(response).to have_http_status(:ok)
end
end
Expand Down

0 comments on commit 542eaa1

Please sign in to comment.