From f6fa9096963a807d13826d3f8f4c91ce537c6b49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=AA=20Ho=C3=A0ng=20Ph=C6=B0=C6=A1ng?= Date: Thu, 17 Aug 2023 15:22:49 +0400 Subject: [PATCH] Fix can not edit non-required attribute on a variant that has other required attributes (#3966) * Resend current values of all not-updated attributes Fix variant editor can not save due to requirement of receiving values of all required attributes from API * add changeset --- .changeset/slimy-mangos-refuse.md | 5 +++ .../views/ProductUpdate/handlers/utils.ts | 32 +++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 .changeset/slimy-mangos-refuse.md diff --git a/.changeset/slimy-mangos-refuse.md b/.changeset/slimy-mangos-refuse.md new file mode 100644 index 000000000..667e052a6 --- /dev/null +++ b/.changeset/slimy-mangos-refuse.md @@ -0,0 +1,5 @@ +--- +"saleor-dashboard": patch +--- + +Fix can not edit non-required attribute on a variant that has other required attributes diff --git a/src/products/views/ProductUpdate/handlers/utils.ts b/src/products/views/ProductUpdate/handlers/utils.ts index aaaea7d80..d28535f04 100644 --- a/src/products/views/ProductUpdate/handlers/utils.ts +++ b/src/products/views/ProductUpdate/handlers/utils.ts @@ -141,9 +141,12 @@ export function getBulkVariantUpdateInputs( const createToUpdateInput = (data: DatagridChangeOpts) => - (variant, variantIndex): ProductVariantBulkUpdateInput => ({ + ( + variant: ProductFragment["variants"][number], + variantIndex: number, + ): ProductVariantBulkUpdateInput => ({ id: variant.id, - attributes: getAttributeData(data.updates, variantIndex, data.removed), + attributes: getVariantAttributesForUpdate(data, variantIndex, variant), sku: getSkuData(data.updates, variantIndex, data.removed), name: getNameData(data.updates, variantIndex, data.removed), stocks: getVaraintUpdateStockData( @@ -155,6 +158,31 @@ const createToUpdateInput = channelListings: getUpdateVariantChannelInputs(data, variantIndex, variant), }); +const getVariantAttributesForUpdate = ( + data: DatagridChangeOpts, + variantIndex: number, + variant: ProductFragment["variants"][number], +) => { + const updatedAttributes = getAttributeData( + data.updates, + variantIndex, + data.removed, + ); + // Re-send current values for all not-updated attributes, in case some of them were required + const notUpdatedAttributes: ReturnType = + variant.attributes + .filter(attribute => + updatedAttributes.find( + updatedAttribute => updatedAttribute.id !== attribute.attribute.id, + ), + ) + .map(attribute => ({ + id: attribute.attribute.id, + values: attribute.values.map(({ name }) => name), + })); + return [...updatedAttributes, ...notUpdatedAttributes]; +}; + const byAvailability = (variant: ProductVariantBulkUpdateInput): boolean => variant.name !== undefined || variant.sku !== undefined ||