Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add support for custom values
dominik-zeglenb739ba73f5e0
diff --git a/saleor/static/dashboard-next/products/components/ProductAttributes/ProductAttributes.tsx b/saleor/static/dashboard-next/products/components/ProductAttributes/ProductAttributes.tsxindex 32aab9ecdc..c63e741261 100644--- a/saleor/static/dashboard-next/products/components/ProductAttributes/ProductAttributes.tsx+++ b/saleor/static/dashboard-next/products/components/ProductAttributes/ProductAttributes.tsx@@ -90,6 +90,27 @@ function getMultiChoices(}));}+function getMultiDisplayValue(+ attribute: ProductAttributeInput+): MultiAutocompleteChoiceType[] {+ return attribute.value.map(attributeValue => {+ const definedAttributeValue = attribute.data.values.find(+ definedValue => definedValue.slug === attributeValue+ );+ if (!!definedAttributeValue) {+ return {+ label: definedAttributeValue.name,+ value: definedAttributeValue.slug+ };+ }++ return {+ label: attributeValue,+ value: attributeValue+ };+ });+}+function getSingleChoices(values: ProductDetails_product_attributes_attribute_values[]): SingleAutocompleteChoiceType[] {@@ -133,7 +154,7 @@ const ProductAttributes: React.FC<ProductAttributesProps> = ({/></IconButton></div>- {expanded && (+ {expanded && attributes.length > 0 && (<><Hr />{attributes.map((attribute, attributeIndex) => (@@ -154,7 +175,7 @@ const ProductAttributes: React.FC<ProductAttributesProps> = ({attribute.data.values.find(value => value.slug === attribute.value[0]).name,- ""+ attribute.value[0])}name={`attribute:${attribute.label}`}label={i18n.t("Value")}@@ -162,27 +183,30 @@ const ProductAttributes: React.FC<ProductAttributesProps> = ({onChange={event =>onChange(attribute.id, event.target.value)}+ allowCustomValues/>) : (<MultiAutocompleteSelectFieldchoices={getMultiChoices(attribute.data.values)}- displayValues={attribute.data.values- .filter(- value =>- !!attribute.value.find(attributeValue =>- attributeValue.includes(value.slug)- )- )- .map(value => ({- label: value.name,- value: value.slug- }))}+ // displayValues={attribute.data.values+ // .filter(+ // value =>+ // !!attribute.value.find(attributeValue =>+ // attributeValue.includes(value.slug)+ // )+ // )+ // .map(value => ({+ // label: value.name,+ // value: value.slug+ // }))}+ displayValues={getMultiDisplayValue(attribute)}label={i18n.t("Values")}name={`attribute:${attribute.label}`}value={attribute.value}onChange={event =>onMultiChange(attribute.id, event.target.value)}+ allowCustomValues/>)}</div>diff --git a/saleor/static/dashboard-next/products/components/ProductCreatePage/ProductCreatePage.tsx b/saleor/static/dashboard-next/products/components/ProductCreatePage/ProductCreatePage.tsxindex 2408a5adb8..cd97246884 100644--- a/saleor/static/dashboard-next/products/components/ProductCreatePage/ProductCreatePage.tsx+++ b/saleor/static/dashboard-next/products/components/ProductCreatePage/ProductCreatePage.tsx@@ -96,29 +96,13 @@ export const ProductCreatePage: React.StatelessComponent<onBack,onSubmit}: ProductCreatePageProps) => {+ // Form valuesconst {change: changeAttributeData,data: attributes,set: setAttributeData} = useFormset<ProductAttributeInputData>([]);- const [selectedAttributes, setSelectedAttributes] = useStateFromProps<- ProductAttributeValueChoices[]- >([]);-- const [selectedCategory, setSelectedCategory] = useStateFromProps("");-- const [selectedCollections, setSelectedCollections] = useStateFromProps<- MultiAutocompleteChoiceType[]- >([]);-- const [productType, setProductType] = React.useState<ProductType>({- hasVariants: false,- id: "",- name: "",- productAttributes: [] as ProductCreateData_productTypes_edges_node_productAttributes[]- });-const initialDescription = convertToRaw(ContentState.createFromText(""));const initialData: FormData = {basePrice: 0,@@ -136,6 +120,24 @@ export const ProductCreatePage: React.StatelessComponent<stockQuantity: null};+ // Display values+ const [selectedAttributes, setSelectedAttributes] = useStateFromProps<+ ProductAttributeValueChoices[]+ >([]);++ const [selectedCategory, setSelectedCategory] = useStateFromProps("");++ const [selectedCollections, setSelectedCollections] = useStateFromProps<+ MultiAutocompleteChoiceType[]+ >([]);++ const [productType, setProductType] = React.useState<ProductType>({+ hasVariants: false,+ id: "",+ name: "",+ productAttributes: [] as ProductCreateData_productTypes_edges_node_productAttributes[]+ });+const categories = getChoices(categoryChoiceList);const collections = getChoices(collectionChoiceList);const productTypes = getChoices(productTypeChoiceList);@@ -189,10 +191,11 @@ export const ProductCreatePage: React.StatelessComponent<);const handleProductTypeSelect = createProductTypeSelectHandler(- productTypeChoiceList,- setProductType,change,- setAttributeData+ setAttributeData,+ setSelectedAttributes,+ setProductType,+ productTypeChoiceList);return (diff --git a/saleor/static/dashboard-next/products/utils/data.ts b/saleor/static/dashboard-next/products/utils/data.tsindex 8359943fec..8b3273fb7c 100644--- a/saleor/static/dashboard-next/products/utils/data.ts+++ b/saleor/static/dashboard-next/products/utils/data.ts@@ -76,7 +76,7 @@ export function getAttributeInputFromProductType(},id: attribute.id,label: attribute.name,- value: [""]+ value: []}));}diff --git a/saleor/static/dashboard-next/products/utils/handlers.ts b/saleor/static/dashboard-next/products/utils/handlers.tsindex b821c88d43..7f0dc5071c 100644--- a/saleor/static/dashboard-next/products/utils/handlers.ts+++ b/saleor/static/dashboard-next/products/utils/handlers.ts@@ -1,5 +1,6 @@import { FormChange } from "@saleor/hooks/useForm";import { FormsetChange, FormsetData } from "@saleor/hooks/useFormset";+import { maybe } from "@saleor/misc";import { toggle } from "@saleor/utils/lists";import { ProductAttributeInputData } from "../components/ProductAttributes";import {@@ -21,7 +22,7 @@ export function createAttributeChangeHandler(.data.values.find(attributeValue => attributeValue.slug === value);const valueChoice = {- label: attributeValue.name,+ label: maybe(() => attributeValue.name, value),value};@@ -57,18 +58,18 @@ export function createAttributeMultiChangeHandler(.data.values.find(attributeValue => attributeValue.slug === value);const valueChoice = {- label: attributeValue.name,+ label: attributeValue ? attributeValue.name : value,value};const itemIndex = selectedAttributes.findIndex(item => item.id === attributeId);- const field = selectedAttributes[itemIndex].values;+ const attributeValues = selectedAttributes[itemIndex].values;- const attributeValues = toggle(+ const newAttributeValues = toggle(valueChoice,- field,+ attributeValues,(a, b) => a.value === b.value);@@ -76,22 +77,26 @@ export function createAttributeMultiChangeHandler(...selectedAttributes.slice(0, itemIndex),{...selectedAttributes[itemIndex],- values: attributeValues+ values: newAttributeValues},...selectedAttributes.slice(itemIndex + 1)];setSelectedAttributes(newSelectedAttributes);triggerChange();- changeAttributeData(attributeId, attributeValues.map(({ value }) => value));+ changeAttributeData(+ attributeId,+ newAttributeValues.map(({ value }) => value)+ );};}export function createProductTypeSelectHandler(- productTypeChoiceList: ProductType[],- setProductType: (productType: ProductType) => void,change: FormChange,- set: (data: FormsetData<ProductAttributeInputData>) => void+ setAttributes: (data: FormsetData<ProductAttributeInputData>) => void,+ setSelectedAttributes: (data: ProductAttributeValueChoices[]) => void,+ setProductType: (productType: ProductType) => void,+ productTypeChoiceList: ProductType[]): FormChange {return (event: React.ChangeEvent<any>) => {const id = event.target.value;@@ -101,6 +106,12 @@ export function createProductTypeSelectHandler(setProductType(selectedProductType);change(event);- set(getAttributeInputFromProductType(selectedProductType));+ setAttributes(getAttributeInputFromProductType(selectedProductType));+ setSelectedAttributes(+ selectedProductType.productAttributes.map(attribute => ({+ id: attribute.id,+ values: []+ }))+ );};}