Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Fix product create

dominik-zeglen23a329819ef8
diff --git a/saleor/static/dashboard-next/products/components/ProductCreatePage/ProductCreatePage.tsx b/saleor/static/dashboard-next/products/components/ProductCreatePage/ProductCreatePage.tsx
index d7ba891fa8..2408a5adb8 100644
--- a/saleor/static/dashboard-next/products/components/ProductCreatePage/ProductCreatePage.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductCreatePage/ProductCreatePage.tsx
@@ -17,8 +17,8 @@ import { SearchCollections_collections_edges_node } from "@saleor/containers/Sea
import useFormset from "@saleor/hooks/useFormset";
import useStateFromProps from "@saleor/hooks/useStateFromProps";
import {
- getAttributeInputFromProductType,
getChoices,
+ ProductAttributeValueChoices,
ProductType
} from "@saleor/products/utils/data";
import createMultiAutocompleteSelectHandler from "@saleor/utils/handlers/multiAutocompleteSelectChangeHandler";
@@ -28,9 +28,13 @@ import { UserError } from "../../../types";
import { ProductCreateData_productTypes_edges_node_productAttributes } from "../../types/ProductCreateData";
import {
createAttributeChangeHandler,
+ createAttributeMultiChangeHandler,
createProductTypeSelectHandler
} from "../../utils/handlers";
-import ProductAttributes, { ProductAttributeInput } from "../ProductAttributes";
+import ProductAttributes, {
+ ProductAttributeInput,
+ ProductAttributeInputData
+} from "../ProductAttributes";
import ProductDetailsForm from "../ProductDetailsForm";
import ProductOrganization from "../ProductOrganization";
import ProductPricing from "../ProductPricing";
@@ -92,9 +96,18 @@ export const ProductCreatePage: React.StatelessComponent<
onBack,
onSubmit
}: ProductCreatePageProps) => {
- const initialDescription = convertToRaw(ContentState.createFromText(""));
+ const {
+ change: changeAttributeData,
+ data: attributes,
+ set: setAttributeData
+ } = useFormset<ProductAttributeInputData>([]);
+
+ const [selectedAttributes, setSelectedAttributes] = useStateFromProps<
+ ProductAttributeValueChoices[]
+ >([]);
+
+ const [selectedCategory, setSelectedCategory] = useStateFromProps("");
- const [selectedCategory, setSelectedCategory] = React.useState("");
const [selectedCollections, setSelectedCollections] = useStateFromProps<
MultiAutocompleteChoiceType[]
>([]);
@@ -106,13 +119,7 @@ export const ProductCreatePage: React.StatelessComponent<
productAttributes: [] as ProductCreateData_productTypes_edges_node_productAttributes[]
});
- const {
- change: changeAttributeData,
- data: attributes,
- set: setAttributeData,
- toggleItemValue: toggleAttributeValue
- } = useFormset(getAttributeInputFromProductType(productType));
-
+ const initialDescription = convertToRaw(ContentState.createFromText(""));
const initialData: FormData = {
basePrice: 0,
category: "",
@@ -168,10 +175,16 @@ export const ProductCreatePage: React.StatelessComponent<
);
const handleAttributeChange = createAttributeChangeHandler(
changeAttributeData,
+ setSelectedAttributes,
+ selectedAttributes,
+ attributes,
triggerChange
);
- const handleAttributeMultiChange = createAttributeChangeHandler(
- toggleAttributeValue,
+ const handleAttributeMultiChange = createAttributeMultiChangeHandler(
+ changeAttributeData,
+ setSelectedAttributes,
+ selectedAttributes,
+ attributes,
triggerChange
);
diff --git a/saleor/static/dashboard-next/products/views/ProductCreate.tsx b/saleor/static/dashboard-next/products/views/ProductCreate.tsx
index 1d4e6efa9c..837811c8b1 100644
--- a/saleor/static/dashboard-next/products/views/ProductCreate.tsx
+++ b/saleor/static/dashboard-next/products/views/ProductCreate.tsx
@@ -64,7 +64,7 @@ export const ProductUpdate: React.StatelessComponent<
variables: {
attributes: formData.attributes.map(attribute => ({
id: attribute.id,
- values: [attribute.value]
+ values: attribute.value
})),
basePrice: decimal(formData.basePrice),
category: formData.category,