From 039b0d80ee8578115659c1cba75fb94d9a9db13a Mon Sep 17 00:00:00 2001 From: Ahmed Ejaz Date: Fri, 24 May 2024 01:55:21 +0500 Subject: [PATCH] 12398: implement the destroy action for products --- app/controllers/admin/products_v3_controller.rb | 17 +++++++++++++++-- .../admin/products_v3/destroy.turbo_stream.haml | 1 + 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/app/controllers/admin/products_v3_controller.rb b/app/controllers/admin/products_v3_controller.rb index 90505150fd..52d26bd924 100644 --- a/app/controllers/admin/products_v3_controller.rb +++ b/app/controllers/admin/products_v3_controller.rb @@ -31,12 +31,25 @@ module Admin end end - # will update this in further commits def destroy - @product = Spree::Product.find(params[:id]) + @product = ProductScopeQuery.new( + spree_current_user, + { id: params[:id] } + ).find_product + + if @product.destroy + flash[:success] = I18n.t('admin.products_v3.delete_product.success') + else + flash[:error] = I18n.t('admin.products_v3.delete_product.error') + end + respond_with do |format| format.turbo_stream end + + # using flash with turbo stream doesn't clear it because the page is not refreshed. + # so upon refreshing the page, the flash message appears again + flash.discard end def index_url(params) diff --git a/app/views/admin/products_v3/destroy.turbo_stream.haml b/app/views/admin/products_v3/destroy.turbo_stream.haml index 8f19c2fdfe..f517ebc6cd 100644 --- a/app/views/admin/products_v3/destroy.turbo_stream.haml +++ b/app/views/admin/products_v3/destroy.turbo_stream.haml @@ -1 +1,2 @@ = turbo_stream.remove dom_id(@product) += render partial: "admin/shared/flashes", locals: { flashes: flash } if defined? flash \ No newline at end of file