Skip to content

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

Fix variant forms errors

dominik-zeglen7d228a47e8b9
diff --git a/saleor/static/dashboard-next/products/components/ProductVariantCreatePage/ProductVariantCreatePage.tsx b/saleor/static/dashboard-next/products/components/ProductVariantCreatePage/ProductVariantCreatePage.tsx
index f6e0835535..d653433bda 100644
--- a/saleor/static/dashboard-next/products/components/ProductVariantCreatePage/ProductVariantCreatePage.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductVariantCreatePage/ProductVariantCreatePage.tsx
@@ -23,7 +23,7 @@ interface FormData {
costPrice?: string;
images?: string[];
priceOverride?: string;
- stock?: number;
+ quantity?: number;
sku?: string;
}
interface ProductVariantCreatePageProps {
@@ -108,8 +108,8 @@ const ProductVariantCreatePage = decorate<ProductVariantCreatePageProps>(
? product.images.edges.map(edge => edge.node.id)
: undefined,
priceOverride: "",
- sku: "",
- stock: 0
+ quantity: 0,
+ sku: ""
};
return (
<Container width="md">
@@ -166,7 +166,7 @@ const ProductVariantCreatePage = decorate<ProductVariantCreatePageProps>(
<ProductVariantStock
errors={errors}
sku={data.sku}
- stock={data.stock}
+ quantity={data.quantity}
loading={loading}
onChange={change}
/>
diff --git a/saleor/static/dashboard-next/products/components/ProductVariantPage/ProductVariantPage.tsx b/saleor/static/dashboard-next/products/components/ProductVariantPage/ProductVariantPage.tsx
index 97d20f7d5c..7ecbfef612 100644
--- a/saleor/static/dashboard-next/products/components/ProductVariantPage/ProductVariantPage.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductVariantPage/ProductVariantPage.tsx
@@ -141,8 +141,9 @@ const ProductVariantPage = decorate<ProductVariantPageProps>(
variant && variant.priceOverride
? variant.priceOverride.amount.toString()
: null,
- sku: variant && variant.sku,
- stock: variant && variant.quantity ? variant.quantity : ""
+ quantity:
+ variant && variant.quantity ? variant.quantity : "",
+ sku: variant && variant.sku
}}
errors={formErrors}
onSubmit={onSubmit}
@@ -200,7 +201,7 @@ const ProductVariantPage = decorate<ProductVariantPageProps>(
<ProductVariantStock
errors={errors}
sku={data.sku}
- stock={data.stock}
+ quantity={data.quantity}
stockAllocated={
variant ? variant.quantityAllocated : undefined
}
diff --git a/saleor/static/dashboard-next/products/components/ProductVariantStock/ProductVariantStock.tsx b/saleor/static/dashboard-next/products/components/ProductVariantStock/ProductVariantStock.tsx
index bcafd79379..3ac05fd064 100644
--- a/saleor/static/dashboard-next/products/components/ProductVariantStock/ProductVariantStock.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductVariantStock/ProductVariantStock.tsx
@@ -8,9 +8,12 @@ import CardTitle from "../../../components/CardTitle";
import i18n from "../../../i18n";
interface ProductVariantStockProps {
- errors: { [key: string]: string };
- sku?: string;
- stock?: number;
+ errors: {
+ quantity?: string;
+ sku?: string;
+ };
+ sku: string;
+ quantity: number;
stockAllocated?: number;
loading?: boolean;
onChange(event: any);
@@ -31,24 +34,24 @@ const decorate = withStyles(theme => ({
}));
const ProductVariantStock = decorate<ProductVariantStockProps>(
- ({ classes, errors, sku, stock, stockAllocated, loading, onChange }) => (
+ ({ classes, errors, sku, quantity, stockAllocated, loading, onChange }) => (
<Card className={classes.root}>
<CardTitle title={i18n.t("Stock")} />
<CardContent>
<div className={classes.grid}>
<div>
<TextField
- error={!!errors.stock}
- name="stock"
- value={stock}
+ error={!!errors.quantity}
+ name="quantity"
+ value={quantity}
label={i18n.t("Inventory")}
helperText={
- errors.stock
- ? errors.stock
+ errors.quantity
+ ? errors.quantity
: !!stockAllocated
- ? i18n.t("Allocated: {{ stock }}", {
+ ? i18n.t("Allocated: {{ quantity }}", {
context: "variant allocated stock",
- stock: stockAllocated
+ quantity: stockAllocated
})
: undefined
}
diff --git a/saleor/static/dashboard-next/products/views/ProductVariant.tsx b/saleor/static/dashboard-next/products/views/ProductVariant.tsx
index 865048561f..e2ebf5eec6 100644
--- a/saleor/static/dashboard-next/products/views/ProductVariant.tsx
+++ b/saleor/static/dashboard-next/products/views/ProductVariant.tsx
@@ -6,10 +6,11 @@ import ErrorMessageCard from "../../components/ErrorMessageCard";
import Messages from "../../components/messages";
import Navigator from "../../components/Navigator";
import i18n from "../../i18n";
-import { decimal } from "../../misc";
+import { decimal, maybe } from "../../misc";
import ProductVariantPage from "../components/ProductVariantPage";
import ProductVariantOperations from "../containers/ProductVariantOperations";
import { productVariantQuery, TypedProductVariantQuery } from "../queries";
+import { VariantUpdate } from "../types/VariantUpdate";
interface ProductUpdateProps {
variantId: string;
@@ -24,8 +25,8 @@ interface FormData {
}>;
costPrice?: string;
priceOverride?: string;
- stock?: number;
- sku?: string;
+ quantity: number;
+ sku: string;
}
export const ProductVariant: React.StatelessComponent<ProductUpdateProps> = ({
@@ -51,8 +52,11 @@ export const ProductVariant: React.StatelessComponent<ProductUpdateProps> = ({
pushMessage({ text: i18n.t("Variant removed") });
navigate(productUrl(productId));
};
- const handleUpdate = () =>
- pushMessage({ text: i18n.t("Changes saved") });
+ const handleUpdate = (data: VariantUpdate) => {
+ if (!maybe(() => data.productVariantUpdate.errors.length)) {
+ pushMessage({ text: i18n.t("Changes saved") });
+ }
+ };
return (
<ProductVariantOperations
@@ -64,6 +68,7 @@ export const ProductVariant: React.StatelessComponent<ProductUpdateProps> = ({
{({
assignImage,
deleteVariant,
+ errors,
updateVariant,
unassignImage
}) => {
@@ -99,14 +104,7 @@ export const ProductVariant: React.StatelessComponent<ProductUpdateProps> = ({
return (
<ProductVariantPage
- errors={
- updateVariant &&
- updateVariant.data &&
- updateVariant.data.productVariantUpdate &&
- updateVariant.data.productVariantUpdate.errors
- ? updateVariant.data.productVariantUpdate.errors
- : []
- }
+ errors={errors}
saveButtonBarState={formSubmitState}
loading={disableFormSave}
placeholderImage={placeholderImg}
@@ -126,7 +124,7 @@ export const ProductVariant: React.StatelessComponent<ProductUpdateProps> = ({
costPrice: decimal(data.costPrice),
id: variantId,
priceOverride: decimal(data.priceOverride),
- quantity: data.stock,
+ quantity: data.quantity || null,
sku: data.sku,
trackInventory: true // FIXME: missing in UI
});
diff --git a/saleor/static/dashboard-next/products/views/ProductVariantCreate.tsx b/saleor/static/dashboard-next/products/views/ProductVariantCreate.tsx
index a551b9c6aa..c683e7daed 100644
--- a/saleor/static/dashboard-next/products/views/ProductVariantCreate.tsx
+++ b/saleor/static/dashboard-next/products/views/ProductVariantCreate.tsx
@@ -25,8 +25,8 @@ interface FormData {
}>;
costPrice?: string;
priceOverride?: string;
- stock?: number;
- sku?: string;
+ quantity: number;
+ sku: string;
}
export const ProductVariant: React.StatelessComponent<ProductUpdateProps> = ({
@@ -81,7 +81,7 @@ export const ProductVariant: React.StatelessComponent<ProductUpdateProps> = ({
costPrice: decimal(formData.costPrice),
priceOverride: decimal(formData.priceOverride),
product: productId,
- quantity: formData.stock,
+ quantity: formData.quantity || null,
sku: formData.sku,
trackInventory: true
}