Skip to content

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

Refactor variant attributes form

dominik-zeglen0bf332e14a9a
diff --git a/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx b/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx
index 108c3a4025..0a8f41cff7 100644
--- a/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx
@@ -155,7 +155,7 @@ export const ProductUpdate = decorate<ProductUpdateProps>(
product && product.attributes
? product.attributes.map(a => ({
slug: a.attribute.slug,
- value: a.value.slug
+ value: a.value ? a.value.slug : null
}))
: undefined,
available: product ? product.isPublished : undefined,
diff --git a/saleor/static/dashboard-next/products/components/ProductVariantAttributes/ProductVariantAttributes.tsx b/saleor/static/dashboard-next/products/components/ProductVariantAttributes/ProductVariantAttributes.tsx
index 6a0354aa69..4fe62f00b9 100644
--- a/saleor/static/dashboard-next/products/components/ProductVariantAttributes/ProductVariantAttributes.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductVariantAttributes/ProductVariantAttributes.tsx
@@ -1,28 +1,33 @@
import Card from "@material-ui/core/Card";
import CardContent from "@material-ui/core/CardContent";
import { withStyles } from "@material-ui/core/styles";
-import Typography from "@material-ui/core/Typography";
import * as React from "react";
-import { AttributeType, AttributeValueType } from "../..";
+import { AttributeType } from "../..";
import CardTitle from "../../../components/CardTitle";
+import { FormSpacer } from "../../../components/FormSpacer";
import SingleAutocompleteSelectField from "../../../components/SingleAutocompleteSelectField";
import Skeleton from "../../../components/Skeleton";
import i18n from "../../../i18n";
interface ProductVariantAttributesProps {
- attributes?: Array<{
- attribute: AttributeType;
- value: AttributeValueType;
- }>;
+ attributes: AttributeType[];
data: {
- [key: string]: {
- label: string;
+ attributes: Array<{
+ slug: string;
value: string;
- };
+ }>;
};
disabled: boolean;
- onChange(event: any);
+ onChange: (
+ event: React.ChangeEvent<{
+ name: string;
+ value: Array<{
+ slug: string;
+ value: string;
+ }>;
+ }>
+ ) => void;
}
const decorate = withStyles(theme => ({
@@ -40,45 +45,87 @@ const decorate = withStyles(theme => ({
}));
const ProductVariantAttributes = decorate<ProductVariantAttributesProps>(
- ({ attributes, classes, data, disabled, onChange }) => (
- <Card className={classes.card}>
- <CardTitle title={i18n.t("General Information")} />
- <CardContent className={classes.grid}>
- {attributes === undefined ? (
- <Skeleton />
- ) : attributes.length > 0 ? (
- attributes.map(item => {
- const { attribute } = item;
- return (
- <>
- <SingleAutocompleteSelectField
- choices={
- attribute.values
- ? attribute.values.map(value => ({
- label: value.name,
- value: value.slug
- }))
- : []
+ ({ attributes, classes, data, disabled, onChange }) => {
+ 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 : "";
+ return {
+ 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 handleProductAttributeValueSelect = (
+ 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
+ )
}
- disabled={disabled}
- label={attribute.name}
- name={attribute.slug}
- value={data[attribute.slug]}
- onChange={onChange}
- custom
- key={attribute.slug}
- />
- <div />
- </>
- );
- })
- ) : (
- <Typography>
- {i18n.t("This product type has no variant attributes.")}
- </Typography>
- )}
- </CardContent>
- </Card>
- )
+ });
+
+ return (
+ <React.Fragment key={index}>
+ <SingleAutocompleteSelectField
+ disabled={disabled}
+ name={item.slug}
+ label={item.name}
+ onChange={handleProductAttributeValueSelect}
+ value={getAttributeValue(item.slug)}
+ choices={getAttributeValues(item.slug)}
+ key={index}
+ custom
+ />
+ <FormSpacer />
+ </React.Fragment>
+ );
+ })
+ )}
+ </CardContent>
+ </Card>
+ );
+ }
);
export default ProductVariantAttributes;
diff --git a/saleor/static/dashboard-next/products/components/ProductVariantPage/ProductVariantPage.tsx b/saleor/static/dashboard-next/products/components/ProductVariantPage/ProductVariantPage.tsx
index bb37e73acd..c2fb274ed1 100644
--- a/saleor/static/dashboard-next/products/components/ProductVariantPage/ProductVariantPage.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductVariantPage/ProductVariantPage.tsx
@@ -107,15 +107,6 @@ const ProductVariantPage = decorate<ProductVariantPageProps>(
onSubmit,
onVariantClick
}) => {
- const attributes = variant
- ? variant.attributes.reduce((prev, curr) => {
- prev[curr.attribute.slug] = {
- label: curr.value.name,
- value: curr.value.slug
- };
- return prev;
- }, {})
- : {};
const variantImages = variant
? variant.images.edges.map(edge => edge.node.id)
: [];
@@ -140,6 +131,13 @@ const ProductVariantPage = decorate<ProductVariantPageProps>(
<PageHeader title={header} onBack={onBack} />
<Form
initial={{
+ attributes:
+ variant && variant.attributes
+ ? variant.attributes.map(a => ({
+ slug: a.attribute.slug,
+ value: a.value.slug
+ }))
+ : [],
costPrice:
variant && variant.costPrice
? variant.costPrice.amount.toString()
@@ -149,9 +147,7 @@ const ProductVariantPage = decorate<ProductVariantPageProps>(
? variant.priceOverride.amount.toString()
: null,
sku: variant && variant.sku,
- stock:
- variant && variant.quantity ? variant.quantity : "",
- ...attributes
+ stock: variant && variant.quantity ? variant.quantity : ""
}}
errors={formErrors}
onSubmit={onSubmit}
@@ -180,7 +176,9 @@ const ProductVariantPage = decorate<ProductVariantPageProps>(
<div>
<ProductVariantAttributes
attributes={
- variant ? variant.attributes : undefined
+ variant && variant.attributes
+ ? variant.attributes.map(a => a.attribute)
+ : undefined
}
data={data}
disabled={loading}
diff --git a/saleor/static/dashboard-next/products/views/ProductVariant.tsx b/saleor/static/dashboard-next/products/views/ProductVariant.tsx
index 2542fe7f3b..3289d72e92 100644
--- a/saleor/static/dashboard-next/products/views/ProductVariant.tsx
+++ b/saleor/static/dashboard-next/products/views/ProductVariant.tsx
@@ -16,6 +16,18 @@ interface ProductUpdateProps {
productId: string;
}
+interface FormData {
+ id: string;
+ attributes?: Array<{
+ slug: string;
+ value: string;
+ }>;
+ costPrice?: string;
+ priceOverride?: string;
+ stock?: number;
+ sku?: string;
+}
+
export const ProductVariant: React.StatelessComponent<ProductUpdateProps> = ({
variantId,
productId
@@ -79,30 +91,12 @@ export const ProductVariant: React.StatelessComponent<ProductUpdateProps> = ({
onBack={handleBack}
onDelete={() => deleteVariant.mutate(variantId)}
onImageSelect={() => {}}
- onSubmit={data => {
+ onSubmit={(data: FormData) => {
if (variant) {
- // fix attributes
- const attributes = variant.attributes
- .map(item => ({
- slug: item.attribute.slug,
- values: item.attribute.values
- }))
- .map(({ slug, values }) => {
- const valueSlug = data[slug].value;
- const value = values.filter(
- item => item.slug === valueSlug
- );
- return {
- slug,
- value:
- value && value[0]
- ? value[0].name
- : valueSlug
- };
- });
-
updateVariant.mutate({
- attributes,
+ attributes: data.attributes
+ ? data.attributes
+ : null,
costPrice: data.costPrice ? data.costPrice : null,
id: variantId,
priceOverride: data.priceOverride