12398: add turbo stream to delete variants

This commit is contained in:
Ahmed Ejaz
2024-05-24 02:41:51 +05:00
committed by zanetagebka
parent bb2e7af190
commit aa040737dd
6 changed files with 24 additions and 3 deletions

View File

@@ -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

View File

@@ -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')

View File

@@ -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|

View File

@@ -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' }

View File

@@ -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

View File

@@ -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