Fix unable to save attribute value (#3814)

This commit is contained in:
Paweł Chyła 2023-06-29 10:57:48 +02:00 committed by GitHub
parent d3791b6a23
commit a08d034e75
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 86 additions and 7 deletions

View file

@ -0,0 +1,5 @@
---
"saleor-dashboard": patch
---
Fix unability to save attribute value when the attribute value is invalid (attribute value should be only send when attribute input type is swatch)

View file

@ -24,6 +24,7 @@ import { FormattedMessage, useIntl } from "react-intl";
import { AttributeValueEditDialogFormData } from "../../utils/data"; import { AttributeValueEditDialogFormData } from "../../utils/data";
import AttributeSwatchField from "../AttributeSwatchField"; import AttributeSwatchField from "../AttributeSwatchField";
import { getAttributeValueFields } from "./utils";
export interface AttributeValueEditDialogProps { export interface AttributeValueEditDialogProps {
attributeValue: AttributeValueEditDialogFormData | null; attributeValue: AttributeValueEditDialogFormData | null;
@ -47,12 +48,12 @@ const AttributeValueEditDialog: React.FC<AttributeValueEditDialogProps> = ({
inputType, inputType,
}) => { }) => {
const intl = useIntl(); const intl = useIntl();
const attributeValueFields = attributeValue?.fileUrl
? { const isSwatch = inputType === AttributeInputTypeEnum.SWATCH;
fileUrl: attributeValue?.fileUrl, const attributeValueFields = getAttributeValueFields(
contentType: attributeValue?.contentType, attributeValue,
} isSwatch,
: { value: attributeValue?.value ?? "" }; );
const initialForm: AttributeValueEditDialogFormData = { const initialForm: AttributeValueEditDialogFormData = {
name: attributeValue?.name ?? "", name: attributeValue?.name ?? "",
@ -60,7 +61,6 @@ const AttributeValueEditDialog: React.FC<AttributeValueEditDialogProps> = ({
}; };
const errors = useModalDialogErrors(apiErrors, open); const errors = useModalDialogErrors(apiErrors, open);
const formErrors = getFormErrors(["name"], errors); const formErrors = getFormErrors(["name"], errors);
const isSwatch = inputType === AttributeInputTypeEnum.SWATCH;
return ( return (
<Dialog onClose={onClose} open={open} fullWidth maxWidth="sm"> <Dialog onClose={onClose} open={open} fullWidth maxWidth="sm">

View file

@ -0,0 +1,54 @@
import { AttributeValueEditDialogFormData } from "@dashboard/attributes/utils/data";
import { getAttributeValueFields } from "./utils";
describe("getAttributeValueFields", () => {
it("should return fileUrl and contentType if attributeValue has fileUrl", () => {
// Arrange
const attributeValue = {
fileUrl: "fileUrl",
contentType: "contentType",
value: "value",
} as AttributeValueEditDialogFormData;
const isSwatch = true;
// Act
const result = getAttributeValueFields(attributeValue, isSwatch);
// Assert
expect(result).toEqual({
fileUrl: "fileUrl",
contentType: "contentType",
});
});
it("should return value when attributeValue has value and is swatch type", () => {
// Arrange
const attributeValue = {
value: "value",
} as AttributeValueEditDialogFormData;
const isSwatch = true;
// Act
const result = getAttributeValueFields(attributeValue, isSwatch);
// Assert
expect(result).toEqual({
value: "value",
});
});
it("should return empty object when attributeValue has value but type is not swatch", () => {
// Arrange
const attributeValue = {
value: "value",
} as AttributeValueEditDialogFormData;
const isSwatch = false;
// Act
const result = getAttributeValueFields(attributeValue, isSwatch);
// Assert
expect(result).toEqual({});
});
});

View file

@ -0,0 +1,20 @@
import { AttributeValueEditDialogFormData } from "@dashboard/attributes/utils/data";
export const getAttributeValueFields = (
attributeValue: AttributeValueEditDialogFormData | null,
isSwatch: boolean,
): Partial<Omit<AttributeValueEditDialogFormData, "name">> => {
if (attributeValue?.fileUrl) {
return {
fileUrl: attributeValue?.fileUrl,
contentType: attributeValue?.contentType,
};
}
// Value should be only use when input type is swatch
if (attributeValue?.value && isSwatch) {
return { value: attributeValue?.value ?? "" };
}
return {};
};