From 291c0c3a64a78ddc8dd5773525f755efdc44ee0b Mon Sep 17 00:00:00 2001 From: Ana Nunes da Silva Date: Tue, 23 Apr 2024 09:30:50 +0100 Subject: [PATCH] Making turbo stream work with link_to helpers --- .../admin/products_v3_controller.rb | 2 ++ .../admin/products_v3/_product_form.html.haml | 24 +++++++++++++++++++ .../admin/products_v3/_product_row.html.haml | 2 +- app/views/admin/products_v3/_table.html.haml | 4 ++++ .../admin/products_v3/clone.turbo_stream.haml | 4 ++-- 5 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 app/views/admin/products_v3/_product_form.html.haml diff --git a/app/controllers/admin/products_v3_controller.rb b/app/controllers/admin/products_v3_controller.rb index fa6470d267..a653ca55cf 100644 --- a/app/controllers/admin/products_v3_controller.rb +++ b/app/controllers/admin/products_v3_controller.rb @@ -36,6 +36,8 @@ module Admin def clone @product = Spree::Product.find(params[:id]) @cloned_product = @product.duplicate + @product_index = params[:product_index] + raise "Clone failed" unless @cloned_product.save respond_to(&:turbo_stream) diff --git a/app/views/admin/products_v3/_product_form.html.haml b/app/views/admin/products_v3/_product_form.html.haml new file mode 100644 index 0000000000..bcc6e098e4 --- /dev/null +++ b/app/views/admin/products_v3/_product_form.html.haml @@ -0,0 +1,24 @@ += form.fields_for("products", product, index: product_index) do |product_form| + %tbody.relaxed{ data: { 'record-id': product_form.object.id, + controller: "nested-form product", + action: 'rails-nested-form:add->bulk-form#registerElements' }, id: "product_#{product.id}" } + %tr + = render partial: 'product_row', locals: { product:, product_index:, f: product_form } + + - product.variants.each_with_index do |variant, variant_index| + = form.fields_for("products][#{product_index}][variants_attributes][", variant, index: variant_index) do |variant_form| + %tr.condensed{ 'data-controller': "variant" } + = render partial: 'variant_row', locals: { variant:, f: variant_form } + + = form.fields_for("products][#{product_index}][variants_attributes][NEW_RECORD", product.variants.build) do |new_variant_form| + %template{ 'data-nested-form-target': "template" } + %tr.condensed{ 'data-controller': "variant" } + = render partial: 'variant_row', locals: { variant: new_variant_form.object, f: new_variant_form } + + %tr{ 'data-nested-form-target': "target" } + %tr.condensed + %td + %td{ colspan: 11 } + %button.secondary.condensed.naked.icon-plus{ 'data-action': "nested-form#add", + 'aria-label': t('.new_variant') } + =t('.new_variant') \ No newline at end of file diff --git a/app/views/admin/products_v3/_product_row.html.haml b/app/views/admin/products_v3/_product_row.html.haml index 60bd64ab59..88d5a09a77 100644 --- a/app/views/admin/products_v3/_product_row.html.haml +++ b/app/views/admin/products_v3/_product_row.html.haml @@ -42,7 +42,7 @@ %td.align-right = render(VerticalEllipsisMenu::Component.new) do = link_to t('admin.products_page.actions.edit'), edit_admin_product_path(product), 'data-turbo': false - = link_to t('admin.products_page.actions.clone'), admin_clone_product_path(product), method: :post, 'data-turbo-stream': true + = link_to t('admin.products_page.actions.clone'), admin_clone_product_path(product, product_index:), 'data-turbo-method': 'post' %a{ "data-controller": "modal-link", "data-action": "click->modal-link#setModalDataSetOnConfirm click->modal-link#open", "data-modal-link-target-value": "product-delete-modal", "class": "delete", "data-modal-link-modal-dataset-value": {'data-current-id': product.id}.to_json } diff --git a/app/views/admin/products_v3/_table.html.haml b/app/views/admin/products_v3/_table.html.haml index 23f6fb5fb4..53f765a627 100644 --- a/app/views/admin/products_v3/_table.html.haml +++ b/app/views/admin/products_v3/_table.html.haml @@ -62,6 +62,7 @@ %th.align-left= t('admin.products_page.columns.inherits_properties') %th.align-right= t('admin.products_page.columns.actions') - products.each_with_index do |product, product_index| +<<<<<<< HEAD = form.fields_for("products", product, index: product_index) do |product_form| %tbody.relaxed{ data: { 'record-id': product_form.object.id, controller: "nested-form product", @@ -86,3 +87,6 @@ %button.secondary.condensed.naked.icon-plus{ 'data-action': "nested-form#add", 'aria-label': t('.new_variant') } =t('.new_variant') +======= + = render partial: 'product_form', locals: { product:, product_index:, form: } +>>>>>>> 520db0ebf3 (Making turbo stream work with link_to helpers) diff --git a/app/views/admin/products_v3/clone.turbo_stream.haml b/app/views/admin/products_v3/clone.turbo_stream.haml index 252eb8fd72..5c7b9f1a55 100644 --- a/app/views/admin/products_v3/clone.turbo_stream.haml +++ b/app/views/admin/products_v3/clone.turbo_stream.haml @@ -1,2 +1,2 @@ -= turbo_stream.replace "products-content" do - %h1= "This is a test." \ No newline at end of file += turbo_stream.before "product_#{@product.id}" do + %p= "This is a test." \ No newline at end of file