From aa040737dd57633586b2cbb6d1f8de00ebe1caee Mon Sep 17 00:00:00 2001 From: Ahmed Ejaz Date: Fri, 24 May 2024 02:41:51 +0500 Subject: [PATCH] 12398: add turbo stream to delete variants --- app/controllers/admin/products_v3_controller.rb | 17 +++++++++++++++++ .../admin/products_v3/_product_row.html.haml | 2 +- app/views/admin/products_v3/_table.html.haml | 2 +- .../admin/products_v3/_variant_row.html.haml | 2 +- .../destroy_product_variant.turbo_stream.haml | 3 +++ config/routes/admin.rb | 1 + 6 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 app/views/admin/products_v3/destroy_product_variant.turbo_stream.haml diff --git a/app/controllers/admin/products_v3_controller.rb b/app/controllers/admin/products_v3_controller.rb index b4968e0a2f..15d12d3849 100644 --- a/app/controllers/admin/products_v3_controller.rb +++ b/app/controllers/admin/products_v3_controller.rb @@ -52,6 +52,23 @@ module Admin flash.discard end + def destroy_variant + @record = Spree::Variant.active.find(params[:id]) + authorize! :delete, @record + + if VariantDeleter.new.delete(@record) + flash[:success] = I18n.t('admin.products_v3.delete_variant.success') + else + flash[:error] = I18n.t('admin.products_v3.delete_variant.error') + end + + respond_with do |format| + format.turbo_stream { render :destroy_product_variant } + end + + flash.discard + end + def index_url(params) "/admin/products?#{params.to_query}" # todo: fix routing so this can be automaticly generated end diff --git a/app/views/admin/products_v3/_product_row.html.haml b/app/views/admin/products_v3/_product_row.html.haml index 0dd86aa73b..0e16514257 100644 --- a/app/views/admin/products_v3/_product_row.html.haml +++ b/app/views/admin/products_v3/_product_row.html.haml @@ -43,5 +43,5 @@ = link_to t('admin.products_page.actions.clone'), clone_admin_product_path(product), 'data-turbo': false %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, 'data-path': admin_product_destroy_path(product)}.to_json } + "data-modal-link-modal-dataset-value": {'data-path': admin_product_destroy_path(product)}.to_json } = t('admin.products_page.actions.delete') diff --git a/app/views/admin/products_v3/_table.html.haml b/app/views/admin/products_v3/_table.html.haml index 92f8ded399..464130d181 100644 --- a/app/views/admin/products_v3/_table.html.haml +++ b/app/views/admin/products_v3/_table.html.haml @@ -71,7 +71,7 @@ - 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", 'class': "nested-form-wrapper", 'data-new-record': variant.new_record? ? "true" : false } + %tr.condensed{ id: dom_id(variant), 'data-controller': "variant", 'class': "nested-form-wrapper", 'data-new-record': variant.new_record? ? "true" : false } = render partial: 'variant_row', locals: { variant:, f: variant_form, category_options:, tax_category_options: } = form.fields_for("products][#{product_index}][variants_attributes][NEW_RECORD", product.variants.build) do |new_variant_form| diff --git a/app/views/admin/products_v3/_variant_row.html.haml b/app/views/admin/products_v3/_variant_row.html.haml index f60b2522eb..02f23b8eeb 100644 --- a/app/views/admin/products_v3/_variant_row.html.haml +++ b/app/views/admin/products_v3/_variant_row.html.haml @@ -66,7 +66,7 @@ - if variant.product.variants.size > 1 %a{ "data-controller": "modal-link", "data-action": "click->modal-link#setModalDataSetOnConfirm click->modal-link#open", "data-modal-link-target-value": "variant-delete-modal", "class": "delete", - "data-modal-link-modal-dataset-value": {'data-current-id': variant.id}.to_json } + "data-modal-link-modal-dataset-value": {'data-path': admin_destroy_variant_path(variant)}.to_json } = t('admin.products_page.actions.delete') - else %a{ 'data-action': "nested-form#remove", class: 'delete' } diff --git a/app/views/admin/products_v3/destroy_product_variant.turbo_stream.haml b/app/views/admin/products_v3/destroy_product_variant.turbo_stream.haml new file mode 100644 index 0000000000..74c600fe84 --- /dev/null +++ b/app/views/admin/products_v3/destroy_product_variant.turbo_stream.haml @@ -0,0 +1,3 @@ +- # @record can either be Product or Variant += turbo_stream.remove dom_id(@record) += render partial: "admin/shared/flashes", locals: { flashes: flash } if defined? flash diff --git a/config/routes/admin.rb b/config/routes/admin.rb index 3d4665eea2..21caa2b677 100644 --- a/config/routes/admin.rb +++ b/config/routes/admin.rb @@ -77,6 +77,7 @@ Openfoodnetwork::Application.routes.draw do get '/products', to: 'products_v3#index' # we already have DELETE admin/products/:id here delete 'products_v3/:id', to: 'products_v3#destroy', as: 'product_destroy' + delete 'products_v3/destroy_variant/:id', to: 'products_v3#destroy_variant', as: 'destroy_variant' end resources :variant_overrides do