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
This commit is contained in:
Lê Hoàng Phương 2023-08-17 15:22:49 +04:00 committed by GitHub
parent 931be66667
commit f6fa909696
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
"saleor-dashboard": patch
---
Fix can not edit non-required attribute on a variant that has other required attributes

View file

@ -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<typeof getAttributeData> =
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 ||