From 5de91acf88be63eee26439016d992e1a86c6194a Mon Sep 17 00:00:00 2001 From: Du Peng Date: Thu, 13 Oct 2022 12:38:54 +0200 Subject: [PATCH] (bug) fix save empty product image and is_main cannot set to new image after remove old main image --- app/frontend/src/javascript/components/store/product-form.tsx | 3 ++- app/models/product.rb | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/app/frontend/src/javascript/components/store/product-form.tsx b/app/frontend/src/javascript/components/store/product-form.tsx index a6eb4610e..5ce179770 100644 --- a/app/frontend/src/javascript/components/store/product-form.tsx +++ b/app/frontend/src/javascript/components/store/product-form.tsx @@ -170,7 +170,7 @@ export const ProductForm: React.FC = ({ product, title, onSucc */ const addProductImage = () => { setValue('product_images_attributes', output.product_images_attributes.concat({ - is_main: output.product_images_attributes.length === 0 + is_main: output.product_images_attributes.filter(i => i.is_main).length === 0 })); }; @@ -209,6 +209,7 @@ export const ProductForm: React.FC = ({ product, title, onSucc } return { ...image, + is_main: i === k ? false : image.is_main, _destroy: i === k }; })); diff --git a/app/models/product.rb b/app/models/product.rb index 918e7c14b..6326c331e 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -15,7 +15,7 @@ class Product < ApplicationRecord accepts_nested_attributes_for :product_files, allow_destroy: true, reject_if: :all_blank has_many :product_images, as: :viewable, dependent: :destroy - accepts_nested_attributes_for :product_images, allow_destroy: true, reject_if: :all_blank + accepts_nested_attributes_for :product_images, allow_destroy: true, reject_if: ->(i) { i[:attachment].blank? } has_many :product_stock_movements, dependent: :destroy accepts_nested_attributes_for :product_stock_movements, allow_destroy: true, reject_if: :all_blank