Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: adjustment reason edit with turbo-frame link and turbo template with no index #6045

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 All @@ -24,4 +24,3 @@
<% end %>
<% end %>
<% end %>
<%= render component("adjustment_reasons/index").new(page: @page) %>
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# frozen_string_literal: true

class SolidusAdmin::AdjustmentReasons::Edit::Component < SolidusAdmin::BaseComponent
def initialize(page:, adjustment_reason:)
@page = page
def initialize(adjustment_reason:)
@adjustment_reason = adjustment_reason
end

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 @@ -47,8 +51,22 @@ def batch_actions

def columns
[
:name,
:code,
{
header: :name,
data: ->(adjustment_reason) do
link_to adjustment_reason.name, row_url(adjustment_reason),
tvdeyen marked this conversation as resolved.
Show resolved Hide resolved
class: 'body-link',
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: dom_id(adjustment_reason, :edit), turbo_prefetch: false }
end
},
{
header: :active,
data: ->(adjustment_reason) do
Expand All @@ -57,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 @@ -52,10 +52,17 @@ def create
end

def edit
set_index_page

respond_to do |format|
format.html { render component('adjustment_reasons/edit').new(page: @page, adjustment_reason: @adjustment_reason) }
format.html do
if turbo_frame_request?
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
end

Expand All @@ -77,7 +84,7 @@ def update

respond_to do |format|
format.html do
page_component = component('adjustment_reasons/edit').new(page: @page, adjustment_reason: @adjustment_reason)
page_component = component('adjustment_reasons/edit').new(adjustment_reason: @adjustment_reason)
render page_component, status: :unprocessable_entity
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 @@ -68,6 +68,11 @@
get solidus_admin.edit_adjustment_reason_path(adjustment_reason)
expect(response).to have_http_status(:ok)
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": "adjustment_reason_#{adjustment_reason.id}_edit" }
expect(response).to have_http_status(:ok)
end
end

describe "PATCH /update" do
Expand Down
Loading