Skip to content

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

wip

dominik-zeglena2372312c7ec
diff --git a/saleor/static/dashboard-next/products/components/ProductAttributes/ProductAttributes.tsx b/saleor/static/dashboard-next/products/components/ProductAttributes/ProductAttributes.tsx
index 7013af181e..48d57c0bc4 100644
--- a/saleor/static/dashboard-next/products/components/ProductAttributes/ProductAttributes.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductAttributes/ProductAttributes.tsx
@@ -28,7 +28,8 @@ export interface ProductAttributeInputData {
values: ProductDetails_product_attributes_attribute_values[];
}
export type ProductAttributeInput = FormsetAtomicData<
- ProductAttributeInputData
+ ProductAttributeInputData,
+ string[]
>;
export interface ProductAttributesProps {
attributes: ProductAttributeInput[];
@@ -143,13 +144,13 @@ const ProductAttributes: React.FC<ProductAttributesProps> = ({
displayValue={maybe(
() =>
attribute.data.values.find(
- value => value.slug === attribute.value
+ value => value.slug === attribute.value[0]
).name,
""
)}
name={`attribute:${attribute.label}`}
label={i18n.t("Value")}
- value={attribute.value}
+ value={attribute.value[0]}
onChange={event =>
onChange(attribute.id, event.target.value)
}
@@ -157,14 +158,7 @@ const ProductAttributes: React.FC<ProductAttributesProps> = ({
) : (
<MultiAutocompleteSelectField
choices={getMultiChoices(attribute.data.values)}
- displayValues={maybe(
- () =>
- attribute.data.values.map(value => ({
- label: value.name,
- value: value.id
- })),
- []
- )}
+ displayValues={attribute.value}
label={i18n.t("Values")}
name={`attribute:${attribute.label}`}
value={attribute.value}
diff --git a/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx b/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx
index dea9281a01..838374a5e8 100644
--- a/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx
@@ -18,6 +18,8 @@ import useStateFromProps from "@saleor/hooks/useStateFromProps";
import i18n from "@saleor/i18n";
import { maybe } from "@saleor/misc";
import { ListActions, UserError } from "@saleor/types";
+import createMultiAutocompleteSelectHandler from "@saleor/utils/handlers/multiAutocompleteSelectChangeHandler";
+import createSingleAutocompleteSelectHandler from "@saleor/utils/handlers/singleAutocompleteSelectChangeHandler";
import {
ProductDetails_product,
ProductDetails_product_images,
@@ -27,12 +29,14 @@ import {
getAttributeInputFromProduct,
getChoices,
getProductUpdatePageFormData,
+ getSelectedAttributesFromProduct,
ProductUpdatePageFormData
} from "../../utils/data";
import {
createAttributeChangeHandler,
createCategorySelectHandler,
- createCollectionSelectHandler
+ createCollectionSelectHandler,
+ AttributeValueChoice
} from "../../utils/handlers";
import ProductAttributes, { ProductAttributeInput } from "../ProductAttributes";
import ProductDetailsForm from "../ProductDetailsForm";
@@ -41,8 +45,6 @@ import ProductOrganization from "../ProductOrganization";
import ProductPricing from "../ProductPricing";
import ProductStock from "../ProductStock";
import ProductVariants from "../ProductVariants";
-import createMultiAutocompleteSelectHandler from "@saleor/utils/handlers/multiAutocompleteSelectChangeHandler";
-import createSingleAutocompleteSelectHandler from "@saleor/utils/handlers/singleAutocompleteSelectChangeHandler";
interface ProductUpdatePageProps extends ListActions {
errors: UserError[];
@@ -106,9 +108,21 @@ export const ProductUpdatePage: React.FC<ProductUpdatePageProps> = ({
toggleAll,
toolbar
}) => {
- const { change: changeAttributeData, data: attributes } = useFormset(
- getAttributeInputFromProduct(product)
+ const {
+ change: changeAttributeData,
+ data: attributes,
+ toggleItemValue: toggleAttributeValue
+ } = useFormset(getAttributeInputFromProduct(product));
+
+ const [selectedAttributes, setSelectedAttributes] = useStateFromProps<
+ AttributeValueChoice[]
+ >(
+ getSelectedAttributesFromProduct(product).map(attribute => ({
+ id: attribute.id,
+ values: attribute.value
+ }))
);
+
const [selectedCategory, setSelectedCategory] = useStateFromProps(
maybe(() => product.category.name, "")
);
@@ -162,6 +176,12 @@ export const ProductUpdatePage: React.FC<ProductUpdatePageProps> = ({
);
const handleAttributeChange = createAttributeChangeHandler(
changeAttributeData,
+ setSelectedAttributes,
+ selectedAttributes,
+ triggerChange
+ );
+ const handleAttributeMultiChange = createAttributeChangeHandler(
+ toggleAttributeValue,
triggerChange
);
@@ -193,6 +213,7 @@ export const ProductUpdatePage: React.FC<ProductUpdatePageProps> = ({
attributes={attributes}
disabled={disabled}
onChange={handleAttributeChange}
+ onMultiChange={handleAttributeMultiChange}
/>
<CardSpacer />
<ProductPricing
diff --git a/saleor/static/dashboard-next/products/utils/data.ts b/saleor/static/dashboard-next/products/utils/data.ts
index f03bead1ea..209892f864 100644
--- a/saleor/static/dashboard-next/products/utils/data.ts
+++ b/saleor/static/dashboard-next/products/utils/data.ts
@@ -1,5 +1,6 @@
import { RawDraftContentState } from "draft-js";
+import { MultiAutocompleteChoiceType } from "@saleor/components/MultiAutocompleteSelectField";
import { SingleAutocompleteChoiceType } from "@saleor/components/SingleAutocompleteSelectField";
import { maybe } from "@saleor/misc";
import {
@@ -27,7 +28,6 @@ export interface ProductType {
productAttributes: ProductCreateData_productTypes_edges_node_productAttributes[];
}
-// TODO: Take attributes from product type
export function getAttributeInputFromProduct(
product: ProductDetails_product
): ProductAttributeInput[] {
@@ -40,7 +40,21 @@ export function getAttributeInputFromProduct(
},
id: attribute.attribute.id,
label: attribute.attribute.name,
- value: attribute.value.slug
+ value: [attribute.value.slug]
+ })),
+ []
+ );
+}
+
+export function getSelectedAttributesFromProduct(
+ product: ProductDetails_product
+): MultiAutocompleteChoiceType[] {
+ return maybe(
+ (): MultiAutocompleteChoiceType[] =>
+ product.attributes.map(attribute => ({
+ id: attribute.attribute.id,
+ label: attribute.attribute.name,
+ value: [attribute.value.slug]
})),
[]
);
@@ -56,7 +70,7 @@ export function getAttributeInputFromProductType(
},
id: attribute.id,
label: attribute.name,
- value: ""
+ value: [""]
}));
}