diff --git a/CHANGELOG.md b/CHANGELOG.md index f06621ca1..77abd4585 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - Fix a bug: broken display of machines pages - Fix a bug: some automated tests were randomly failing because ElasticSearch was not synced - Fix a bug: payment related objects are not synced on Stripe when enabling the online payment module +- Fix a bug: unable set a main image of product and remove an image of product ## v5.5.4 2022 November 17 diff --git a/app/frontend/src/javascript/components/form/form-image-upload.tsx b/app/frontend/src/javascript/components/form/form-image-upload.tsx index 10c0db07b..fd90e1043 100644 --- a/app/frontend/src/javascript/components/form/form-image-upload.tsx +++ b/app/frontend/src/javascript/components/form/form-image-upload.tsx @@ -60,11 +60,12 @@ export const FormImageUpload = , - { - attachment_name: f.name, - _destroy: false - } as UnpackNestedValue>> + `${id}[attachment_name]` as Path, + f.name as UnpackNestedValue>> + ); + setValue( + `${id}[_destroy]` as Path, + false as UnpackNestedValue>> ); if (typeof onFileChange === 'function') { onFileChange({ attachment_name: f.name }); diff --git a/app/frontend/src/javascript/components/form/form-multi-file-upload.tsx b/app/frontend/src/javascript/components/form/form-multi-file-upload.tsx index 3746a9097..08a3fde47 100644 --- a/app/frontend/src/javascript/components/form/form-multi-file-upload.tsx +++ b/app/frontend/src/javascript/components/form/form-multi-file-upload.tsx @@ -6,9 +6,10 @@ import { FieldValues } from 'react-hook-form/dist/types/fields'; import { FormComponent, FormControlledComponent } from '../../models/form-component'; import { AbstractFormItemProps } from './abstract-form-item'; import { UseFormSetValue } from 'react-hook-form/dist/types/form'; -import { ArrayPath, FieldArray, useFieldArray } from 'react-hook-form'; +import { ArrayPath, FieldArray, Path, useFieldArray, useWatch } from 'react-hook-form'; import { FileType } from '../../models/file'; import { UnpackNestedValue } from 'react-hook-form/dist/types'; +import { FieldPathValue } from 'react-hook-form/dist/types/path'; interface FormMultiFileUploadProps extends FormComponent, FormControlledComponent, AbstractFormItemProps { setValue: UseFormSetValue, @@ -20,13 +21,26 @@ interface FormMultiFileUploadProps extend * This component allows to upload multiple files, in forms managed by react-hook-form. */ export const FormMultiFileUpload = ({ id, className, register, control, setValue, formState, addButtonLabel, accept }: FormMultiFileUploadProps) => { - const { fields, append, remove } = useFieldArray({ control, name: id as ArrayPath }); + const { append } = useFieldArray({ control, name: id as ArrayPath }); + const output = useWatch({ control, name: id as Path }); + + /** + * Remove an file + */ + const handleRemoveFile = (file: FileType, index: number) => { + return () => { + setValue( + `${id}.${index}._destroy` as Path, + true as UnpackNestedValue>> + ); + }; + }; return (
- {fields.map((field: FileType, index) => ( - ( + remove(index)}/> + onFileRemove={() => handleRemoveFile(field, index)}/> ))}
exten * This component allows to upload multiple images, in forms managed by react-hook-form. */ export const FormMultiImageUpload = ({ id, className, register, control, setValue, formState, addButtonLabel }: FormMultiImageUploadProps) => { - const { fields, append, remove } = useFieldArray({ control, name: id as ArrayPath }); + const { append } = useFieldArray({ control, name: id as ArrayPath }); const output = useWatch({ control, name: id as Path }); /** @@ -44,14 +44,10 @@ export const FormMultiImageUpload = >> ); } - if (typeof image.id === 'string') { - remove(index); - } else { - setValue( - `${id}.${index}._destroy` as Path, - true as UnpackNestedValue>> - ); - } + setValue( + `${id}.${index}._destroy` as Path, + true as UnpackNestedValue>> + ); }; }; @@ -74,8 +70,8 @@ export const FormMultiImageUpload =
- {fields.map((field: ImageType, index) => ( - ( + (i) { i[:attachment].blank? } + accepts_nested_attributes_for :product_images, allow_destroy: true, reject_if: ->(i) { i[:attachment].blank? && i[:id].blank? } has_many :product_stock_movements, dependent: :destroy accepts_nested_attributes_for :product_stock_movements, allow_destroy: true, reject_if: :all_blank diff --git a/app/views/api/products/_product.json.jbuilder b/app/views/api/products/_product.json.jbuilder index f5f143349..961baf234 100644 --- a/app/views/api/products/_product.json.jbuilder +++ b/app/views/api/products/_product.json.jbuilder @@ -4,12 +4,12 @@ json.extract! product, :id, :name, :slug, :sku, :is_active, :product_category_id :low_stock_threshold, :machine_ids, :created_at json.description sanitize(product.description) json.amount product.amount / 100.0 if product.amount.present? -json.product_files_attributes product.product_files do |f| +json.product_files_attributes product.product_files.order(created_at: :asc) do |f| json.id f.id json.attachment_name f.attachment_identifier json.attachment_url f.attachment_url end -json.product_images_attributes product.product_images do |f| +json.product_images_attributes product.product_images.order(created_at: :asc) do |f| json.id f.id json.attachment_name f.attachment_identifier json.attachment_url f.attachment_url