Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix product variant form
dominik-zeglenf61213f1b451
diff --git a/saleor/static/dashboard-next/hooks/useStateFromProps.ts b/saleor/static/dashboard-next/hooks/useStateFromProps.tsindex b5abc44355..c78e0c8f14 100644--- a/saleor/static/dashboard-next/hooks/useStateFromProps.ts+++ b/saleor/static/dashboard-next/hooks/useStateFromProps.ts@@ -1,14 +1,14 @@import isEqual from "lodash-es/isEqual";-import { Dispatch, SetStateAction, useEffect, useState } from "react";+import { Dispatch, SetStateAction, useState } from "react";function useStateFromProps<T>(data: T): [T, Dispatch<SetStateAction<T>>] {const [state, setState] = useState(data);const [prevState, setPrevState] = useState(data);- useEffect(() => {+ if (!isEqual(prevState, data)) {setState(data);setPrevState(data);- }, [!isEqual(prevState, data)]);+ }return [state, setState];}diff --git a/saleor/static/dashboard-next/products/components/ProductVariantAttributes/ProductVariantAttributes.tsx b/saleor/static/dashboard-next/products/components/ProductVariantAttributes/ProductVariantAttributes.tsxindex 1d45faf12e..d8c8dc44cc 100644--- a/saleor/static/dashboard-next/products/components/ProductVariantAttributes/ProductVariantAttributes.tsx+++ b/saleor/static/dashboard-next/products/components/ProductVariantAttributes/ProductVariantAttributes.tsx@@ -31,6 +31,7 @@ interface ProductVariantAttributesProps extends WithStyles<typeof styles> {attributes?: ProductVariant_attributes_attribute[];data: {attributes?: Array<{+ name: string;slug: string;value: string;}>;@@ -56,84 +57,84 @@ const ProductVariantAttributes = withStyles(styles, {data,disabled,onChange- }: ProductVariantAttributesProps) => {- return (- <Card className={classes.card}>- <CardTitle title={i18n.t("General Information")} />- <CardContent className={classes.grid}>- {attributes === undefined ? (- <Skeleton />- ) : (- attributes.map((item, index) => {- const getAttributeValue = (slug: string) => {- const valueMatches = attributes.filter(a => a.slug === slug);- if (valueMatches.length > 0) {- const value = data.attributes.filter(a => a.slug === slug)[0]- .value;- const labelMatches = valueMatches[0].values.filter(- v => v.slug === value- );- const label =- labelMatches.length > 0 ? labelMatches[0].name : value;- return {- label,- value- };- }+ }: ProductVariantAttributesProps) => (+ <Card className={classes.card}>+ <CardTitle title={i18n.t("General Information")} />+ <CardContent className={classes.grid}>+ {attributes === undefined && data.attributes.length === 0 ? (+ <Skeleton />+ ) : (+ data.attributes.map((attribute, attributeIndex) => {+ const getAttributeValue = (slug: string) => {+ const valueMatch = attributes.find(a => a.slug === slug);+ if (valueMatch) {+ const value = data.attributes.find(a => a.slug === slug).value;+ const labelMatch = valueMatch.values.find(+ v => v.slug === value+ );+ const label = labelMatch ? labelMatch.name : value;return {- label: "",- value: ""+ label,+ value};+ }+ return {+ label: "",+ value: ""};- const getAttributeValues = (slug: string) => {- const matches = attributes.filter(a => a.slug === slug);- return matches.length > 0- ? matches[0].values.map(v => ({- label: v.name,- value: v.slug- }))- : [];- };- const handleAttributeValueSelect = (- event: React.ChangeEvent<{- name: string;- value: {- label: string;- value: string;- };- }>- ) =>- onChange({- ...(event as any),- target: {- ...event.target,- name: "attributes",- value: data.attributes.map(a =>- a.slug === event.target.name- ? { slug: a.slug, value: event.target.value.value }- : a- )- }- });+ };+ const getAttributeValues = (slug: string) => {+ const matches = attributes.filter(a => a.slug === slug);+ return matches.length > 0+ ? matches[0].values.map(v => ({+ label: v.name,+ value: v.slug+ }))+ : [];+ };+ const handleAttributeValueSelect = (+ event: React.ChangeEvent<{+ name: string;+ value: {+ label: string;+ value: string;+ };+ }>+ ) =>+ onChange({+ ...(event as any),+ target: {+ ...event.target,+ name: "attributes",+ value: data.attributes.map(a =>+ a.slug === event.target.name+ ? {+ name: a.name,+ slug: a.slug,+ value: event.target.value.value+ }+ : a+ )+ }+ });- return (- <SingleAutocompleteSelectField- key={index}- disabled={disabled}- name={item.slug}- label={item.name}- onChange={handleAttributeValueSelect}- value={getAttributeValue(item.slug)}- choices={getAttributeValues(item.slug)}- custom- />- );- })- )}- </CardContent>- </Card>- );- }+ return (+ <SingleAutocompleteSelectField+ key={attributeIndex}+ disabled={disabled}+ name={attribute.slug}+ label={attribute.name}+ onChange={handleAttributeValueSelect}+ value={getAttributeValue(attribute.slug)}+ choices={getAttributeValues(attribute.slug)}+ custom+ />+ );+ })+ )}+ </CardContent>+ </Card>+ ));ProductVariantAttributes.displayName = "ProductVariantAttributes";export default ProductVariantAttributes;diff --git a/saleor/static/dashboard-next/products/components/ProductVariantPage/ProductVariantPage.tsx b/saleor/static/dashboard-next/products/components/ProductVariantPage/ProductVariantPage.tsxindex 1dd8be01e8..4b30719210 100644--- a/saleor/static/dashboard-next/products/components/ProductVariantPage/ProductVariantPage.tsx+++ b/saleor/static/dashboard-next/products/components/ProductVariantPage/ProductVariantPage.tsx@@ -70,13 +70,15 @@ const ProductVariantPage: React.StatelessComponent<ProductVariantPageProps> = ({<PageHeader title={header} /><Forminitial={{- attributes:- variant && variant.attributes- ? variant.attributes.map(a => ({- slug: a.attribute.slug,- value: a.value.slug- }))- : [],+ attributes: maybe(+ () =>+ variant.attributes.map(a => ({+ name: a.attribute.name,+ slug: a.attribute.slug,+ value: a.value.slug+ })),+ []+ ),costPrice:variant && variant.costPrice? variant.costPrice.amount.toString()diff --git a/saleor/static/dashboard-next/products/views/ProductVariant.tsx b/saleor/static/dashboard-next/products/views/ProductVariant.tsxindex de431f60ec..cf0906bb9a 100644--- a/saleor/static/dashboard-next/products/views/ProductVariant.tsx+++ b/saleor/static/dashboard-next/products/views/ProductVariant.tsx@@ -130,7 +130,12 @@ export const ProductVariant: React.StatelessComponent<ProductUpdateProps> = ({onSubmit={(data: FormData) => {if (variant) {updateVariant.mutate({- attributes: data.attributes ? data.attributes : null,+ attributes: data.attributes+ ? data.attributes.map(attribute => ({+ slug: attribute.slug,+ value: attribute.value+ }))+ : null,costPrice: decimal(data.costPrice),id: variantId,priceOverride: decimal(data.priceOverride),