Skip to content

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

Add error handling to variant details view

dominik-zeglen8fc7896cc06b
diff --git a/saleor/static/dashboard-next/products/components/ProductVariantPage/ProductVariantPage.tsx b/saleor/static/dashboard-next/products/components/ProductVariantPage/ProductVariantPage.tsx
index 257f9b7a28..3370370642 100644
--- a/saleor/static/dashboard-next/products/components/ProductVariantPage/ProductVariantPage.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductVariantPage/ProductVariantPage.tsx
@@ -10,6 +10,7 @@ import {
MoneyType,
ProductImageType
} from "../../";
+import { UserError } from "../../..";
import Container from "../../../components/Container";
import Form from "../../../components/Form";
import PageHeader from "../../../components/PageHeader";
@@ -66,6 +67,7 @@ interface ProductVariantPageProps {
quantity: number;
quantityAllocated: number;
};
+ errors: UserError[];
saveButtonBarState?: SaveButtonBarState;
loading?: boolean;
placeholderImage?: string;
@@ -94,14 +96,15 @@ const decorate = withStyles(theme => ({
const ProductVariantPage = decorate<ProductVariantPageProps>(
({
classes,
- variant,
+ errors: formErrors,
loading,
placeholderImage,
+ saveButtonBarState,
+ variant,
onBack,
onDelete,
- saveButtonBarState,
- onSubmit,
onImageSelect,
+ onSubmit,
onVariantClick
}) => {
const attributes = variant
@@ -154,10 +157,11 @@ const ProductVariantPage = decorate<ProductVariantPageProps>(
variant && variant.quantity ? variant.quantity : "",
...attributes
}}
+ errors={formErrors}
onSubmit={onSubmit}
key={variant ? CRC.str(JSON.stringify(variant)) : "loading"}
>
- {({ change, data, hasChanged, submit }) => (
+ {({ change, data, errors, hasChanged, submit }) => (
<>
<div className={classes.root}>
<div>
@@ -196,6 +200,7 @@ const ProductVariantPage = decorate<ProductVariantPageProps>(
loading={loading}
/>
<ProductVariantPrice
+ errors={errors}
priceOverride={data.priceOverride}
currencySymbol={
variant && variant.priceOverride
@@ -207,6 +212,7 @@ const ProductVariantPage = decorate<ProductVariantPageProps>(
onChange={change}
/>
<ProductVariantStock
+ errors={errors}
sku={data.sku}
stock={data.stock}
stockAllocated={
diff --git a/saleor/static/dashboard-next/products/components/ProductVariantPrice/ProductVariantPrice.tsx b/saleor/static/dashboard-next/products/components/ProductVariantPrice/ProductVariantPrice.tsx
index 7344d07ebd..7ade85b912 100644
--- a/saleor/static/dashboard-next/products/components/ProductVariantPrice/ProductVariantPrice.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductVariantPrice/ProductVariantPrice.tsx
@@ -12,6 +12,7 @@ interface ProductVariantPriceProps {
currencySymbol?: string;
priceOverride?: number;
costPrice?: number;
+ errors: { [key: string]: string };
loading?: boolean;
onChange(event: any);
}
@@ -34,6 +35,7 @@ const ProductVariantPrice = decorate<ProductVariantPriceProps>(
classes,
currencySymbol,
costPrice,
+ errors,
priceOverride,
loading,
onChange
@@ -44,9 +46,14 @@ const ProductVariantPrice = decorate<ProductVariantPriceProps>(
<div className={classes.grid}>
<div>
<PriceField
+ error={!!errors.price_override}
name="priceOverride"
label={i18n.t("Selling price override")}
- hint={i18n.t("Optional")}
+ hint={
+ errors.price_override
+ ? errors.price_override
+ : i18n.t("Optional")
+ }
value={priceOverride}
currencySymbol={currencySymbol}
onChange={onChange}
@@ -55,9 +62,10 @@ const ProductVariantPrice = decorate<ProductVariantPriceProps>(
</div>
<div>
<PriceField
+ error={!!errors.cost_price}
name="costPrice"
label={i18n.t("Cost price override")}
- hint={i18n.t("Optional")}
+ hint={errors.cost_price ? errors.cost_price : i18n.t("Optional")}
value={costPrice}
currencySymbol={currencySymbol}
onChange={onChange}
diff --git a/saleor/static/dashboard-next/products/components/ProductVariantStock/ProductVariantStock.tsx b/saleor/static/dashboard-next/products/components/ProductVariantStock/ProductVariantStock.tsx
index 01e789130c..39bbaa2293 100644
--- a/saleor/static/dashboard-next/products/components/ProductVariantStock/ProductVariantStock.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductVariantStock/ProductVariantStock.tsx
@@ -9,6 +9,7 @@ import PageHeader from "../../../components/PageHeader";
import i18n from "../../../i18n";
interface ProductVariantStockProps {
+ errors: { [key: string]: string };
sku?: string;
stock?: number;
stockAllocated?: number;
@@ -34,18 +35,23 @@ const decorate = withStyles(theme => ({
}));
const ProductVariantStock = decorate<ProductVariantStockProps>(
- ({ classes, sku, stock, stockAllocated, loading, onChange }) => (
+ ({ classes, errors, sku, stock, stockAllocated, loading, onChange }) => (
<Card className={classes.root}>
<PageHeader title={i18n.t("Stock")} />
<CardContent>
<div className={classes.grid}>
<div>
<TextField
+ error={!!errors.stock}
name="stock"
value={stock}
label={i18n.t("In stock")}
helperText={
- loading ? "" : `${i18n.t("Allocated:")} ${stockAllocated}`
+ errors.stock
+ ? errors.stock
+ : loading
+ ? ""
+ : `${i18n.t("Allocated:")} ${stockAllocated}`
}
onChange={onChange}
disabled={loading}
@@ -54,6 +60,8 @@ const ProductVariantStock = decorate<ProductVariantStockProps>(
</div>
<div>
<TextField
+ error={!!errors.sku}
+ helperText={errors.sku}
name="sku"
value={sku}
label={i18n.t("SKU")}
diff --git a/saleor/static/dashboard-next/products/containers/ProductVariantDelete.tsx b/saleor/static/dashboard-next/products/containers/ProductVariantDelete.tsx
index 920fa895c3..2369f5d8b1 100644
--- a/saleor/static/dashboard-next/products/containers/ProductVariantDelete.tsx
+++ b/saleor/static/dashboard-next/products/containers/ProductVariantDelete.tsx
@@ -1,31 +1,42 @@
import * as React from "react";
-import { Redirect } from "react-router-dom";
-import ErrorMessageCard from "../../components/ErrorMessageCard";
-import { productUrl } from "../index";
-import { TypedVariantDeleteMutation, variantDeleteMutation } from "../mutations";
+import {
+ PartialMutationProviderProps,
+ PartialMutationProviderRenderProps
+} from "../..";
+import {
+ VariantDeleteMutation,
+ VariantDeleteMutationVariables
+} from "../../gql-types";
+import {
+ TypedVariantDeleteMutation,
+ variantDeleteMutation
+} from "../mutations";
-interface VariantDeleteProviderProps {
- productId: string;
- variantId: string;
- children: ((deleteVariant: () => void) => React.ReactElement<any>);
+interface VariantDeleteProviderProps extends PartialMutationProviderProps {
+ id: string;
+ children: PartialMutationProviderRenderProps<
+ VariantDeleteMutation,
+ VariantDeleteMutationVariables
+ >;
}
const VariantDeleteProvider: React.StatelessComponent<
VariantDeleteProviderProps
-> = ({ productId, variantId, children }) => (
+> = ({ id, children, onError, onSuccess }) => (
<TypedVariantDeleteMutation
mutation={variantDeleteMutation}
- variables={{ id: variantId }}
+ variables={{ id }}
+ onCompleted={onSuccess}
+ onError={onError}
>
- {(deleteVariant, { called, loading, error }) => {
- if (called && !loading) {
- return <Redirect to={productUrl(productId)} push={false} />;
- }
- if (error) {
- return <ErrorMessageCard message={error.message} />;
- }
- return children(() => deleteVariant());
+ {(mutate, { data, loading, error }) => {
+ return children({
+ data,
+ error,
+ loading,
+ mutate
+ });
}}
</TypedVariantDeleteMutation>
);
diff --git a/saleor/static/dashboard-next/products/containers/ProductVariantOperations.tsx b/saleor/static/dashboard-next/products/containers/ProductVariantOperations.tsx
index 25d148e922..1beb3d6bda 100644
--- a/saleor/static/dashboard-next/products/containers/ProductVariantOperations.tsx
+++ b/saleor/static/dashboard-next/products/containers/ProductVariantOperations.tsx
@@ -1,31 +1,58 @@
import * as React from "react";
-import { VariantUpdateMutationVariables } from "../../gql-types";
+import {
+ MutationProviderProps,
+ MutationProviderRenderProps,
+ PartialMutationProviderOutput
+} from "../..";
+import {
+ VariantDeleteMutation,
+ VariantUpdateMutation,
+ VariantUpdateMutationVariables
+} from "../../gql-types";
import VariantDeleteProvider from "./ProductVariantDelete";
import VariantUpdateProvider from "./ProductVariantUpdate";
-interface VariantDeleteOperationsProps {
+interface VariantDeleteOperationsProps extends MutationProviderProps {
productId: string;
- variantId: string;
- children: (
- mutations: {
- deleteVariant(): void;
- updateVariant(variables: VariantUpdateMutationVariables): void;
- }
- ) => React.ReactElement<any>;
+ id: string;
+ children: MutationProviderRenderProps<{
+ deleteVariant: PartialMutationProviderOutput;
+ updateVariant: PartialMutationProviderOutput<
+ VariantUpdateMutation,
+ VariantUpdateMutationVariables
+ >;
+ }>;
+ onDelete?: (data: VariantDeleteMutation) => void;
+ onUpdate?: (data: VariantUpdateMutation) => void;
}
const VariantDeleteOperations: React.StatelessComponent<
VariantDeleteOperationsProps
-> = ({ productId, variantId, children }) => {
+> = ({ id, children, onError, onDelete, onUpdate }) => {
return (
- <VariantUpdateProvider variantId={variantId}>
+ <VariantUpdateProvider id={id} onError={onError} onSuccess={onUpdate}>
{updateVariant => (
- <VariantDeleteProvider productId={productId} variantId={variantId}>
+ <VariantDeleteProvider id={id} onError={onError} onSuccess={onDelete}>
{deleteVariant =>
children({
- deleteVariant,
- updateVariant: variables => updateVariant({ variables })
+ deleteVariant: {
+ data: deleteVariant.data,
+ loading: deleteVariant.loading,
+ mutate: variables => deleteVariant.mutate({ variables: { id } })
+ },
+ errors:
+ updateVariant &&
+ updateVariant.data &&
+ updateVariant.data.productVariantUpdate &&
+ updateVariant.data.productVariantUpdate.errors
+ ? updateVariant.data.productVariantUpdate.errors
+ : [],
+ updateVariant: {
+ data: updateVariant.data,
+ loading: updateVariant.loading,
+ mutate: variables => updateVariant.mutate({ variables })
+ }
})
}
</VariantDeleteProvider>
diff --git a/saleor/static/dashboard-next/products/containers/ProductVariantUpdate.tsx b/saleor/static/dashboard-next/products/containers/ProductVariantUpdate.tsx
index 731bb78f3c..7ce6b01669 100644
--- a/saleor/static/dashboard-next/products/containers/ProductVariantUpdate.tsx
+++ b/saleor/static/dashboard-next/products/containers/ProductVariantUpdate.tsx
@@ -1,37 +1,52 @@
import * as React from "react";
-import ErrorMessageCard from "../../components/ErrorMessageCard";
-import { ProductVariantDetailsQuery } from "../../gql-types";
+import {
+ PartialMutationProviderProps,
+ PartialMutationProviderRenderProps
+} from "../..";
+import {
+ ProductVariantDetailsQuery,
+ VariantUpdateMutation,
+ VariantUpdateMutationVariables
+} from "../../gql-types";
import {
TypedVariantUpdateMutation,
variantUpdateMutation
} from "../mutations";
import { productVariantQuery } from "../queries";
-interface ProductVariantUpdateProviderProps {
- children: any;
- variantId: string;
+interface ProductVariantUpdateProviderProps
+ extends PartialMutationProviderProps<VariantUpdateMutation> {
+ children: PartialMutationProviderRenderProps<
+ VariantUpdateMutation,
+ VariantUpdateMutationVariables
+ >;
+ id: string;
}
const ProductVariantUpdateProvider: React.StatelessComponent<
ProductVariantUpdateProviderProps
-> = ({ variantId, children }) => (
+> = ({ id, children, onError, onSuccess }) => (
<TypedVariantUpdateMutation
mutation={variantUpdateMutation}
update={(cache, { data: { productVariantUpdate } }) => {
const data: ProductVariantDetailsQuery = cache.readQuery({
query: productVariantQuery,
- variables: { id: variantId }
+ variables: { id }
});
data.productVariant = productVariantUpdate.productVariant;
cache.writeQuery({ query: productVariantQuery, data });
}}
+ onCompleted={onSuccess}
+ onError={onError}
>
- {(updateVariant, { error }) => {
- if (error) {
- return <ErrorMessageCard message={error.message} />;
- }
- return children(updateVariant);
+ {(mutate, { data, error, loading }) => {
+ return children({
+ data,
+ error,
+ loading,
+ mutate
+ });
}}
</TypedVariantUpdateMutation>
);
diff --git a/saleor/static/dashboard-next/products/views/ProductVariant.tsx b/saleor/static/dashboard-next/products/views/ProductVariant.tsx
index 38fde68be5..1d5054fdfe 100644
--- a/saleor/static/dashboard-next/products/views/ProductVariant.tsx
+++ b/saleor/static/dashboard-next/products/views/ProductVariant.tsx
@@ -1,9 +1,12 @@
import * as React from "react";
+import { ApolloError } from "apollo-client";
import { productUrl, productVariantEditUrl } from "..";
import * as placeholderImg from "../../../images/placeholder255x255.png";
import ErrorMessageCard from "../../components/ErrorMessageCard";
+import Messages from "../../components/messages";
import Navigator from "../../components/Navigator";
+import i18n from "../../i18n";
import ProductVariantPage from "../components/ProductVariantPage";
import ProductVariantOperations from "../containers/ProductVariantOperations";
import { productVariantQuery, TypedProductVariantQuery } from "../queries";
@@ -18,78 +21,110 @@ export const ProductVariant: React.StatelessComponent<ProductUpdateProps> = ({
productId
}) => (
<Navigator>
- {navigate => {
- return (
- <TypedProductVariantQuery
- query={productVariantQuery}
- variables={{ id: variantId }}
- fetchPolicy="network-only"
- >
- {({ data, loading, error, fetchMore }) => {
- if (error) {
- return <ErrorMessageCard message="Something went wrong" />;
- }
- const variant = data ? data.productVariant : undefined;
- return (
- <ProductVariantOperations
- productId={productId}
- variantId={variant ? variant.id : ""}
- >
- {({ deleteVariant, updateVariant }) => (
- <ProductVariantPage
- loading={loading}
- placeholderImage={placeholderImg}
- variant={variant}
- onBack={() => {
- navigate(productUrl(productId));
- }}
- onDelete={deleteVariant}
- onImageSelect={() => {}}
- onSubmit={data => {
- 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];
- const value = values.filter(
- item => item.slug === valueSlug
- );
- return {
- slug,
- value:
- value && value[0] ? value[0].name : valueSlug
- };
- });
+ {navigate => (
+ <Messages>
+ {pushMessage => (
+ <TypedProductVariantQuery
+ query={productVariantQuery}
+ variables={{ id: variantId }}
+ fetchPolicy="network-only"
+ >
+ {({ data, loading, error }) => {
+ if (error) {
+ return <ErrorMessageCard message="Something went wrong" />;
+ }
+ const variant = data ? data.productVariant : undefined;
+ const handleBack = () => navigate(productUrl(productId));
+ const handleDelete = () => {
+ pushMessage({ text: i18n.t("Variant removed") });
+ navigate(productUrl(productId));
+ };
+ const handleError = (error: ApolloError) => {
+ console.error(error.message);
+ pushMessage({ text: i18n.t("Something went wrong") });
+ };
+ const handleUpdate = () =>
+ pushMessage({ text: i18n.t("Changes saved") });
+ return (
+ <ProductVariantOperations
+ productId={productId}
+ id={variantId}
+ onDelete={handleDelete}
+ onError={handleError}
+ onUpdate={handleUpdate}
+ >
+ {({ deleteVariant, updateVariant }) => {
+ const disableFormSave =
+ loading || deleteVariant.loading || updateVariant.loading;
+ const formSubmitState = disableFormSave
+ ? "loading"
+ : "idle";
+ return (
+ <ProductVariantPage
+ errors={
+ updateVariant &&
+ updateVariant.data &&
+ updateVariant.data.productVariantUpdate &&
+ updateVariant.data.productVariantUpdate.errors
+ ? updateVariant.data.productVariantUpdate.errors
+ : []
+ }
+ saveButtonBarState={formSubmitState}
+ loading={disableFormSave}
+ placeholderImage={placeholderImg}
+ variant={variant}
+ onBack={handleBack}
+ onDelete={() => deleteVariant.mutate(variantId)}
+ onImageSelect={() => {}}
+ onSubmit={data => {
+ 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];
+ const value = values.filter(
+ item => item.slug === valueSlug
+ );
+ return {
+ slug,
+ value:
+ value && value[0]
+ ? value[0].name
+ : valueSlug
+ };
+ });
- updateVariant({
- attributes,
- costPrice: data.costPrice ? data.costPrice : null,
- id: variant.id,
- priceOverride: data.priceOverride
- ? data.priceOverride
- : null,
- product: productId,
- quantity: data.stock,
- sku: data.sku,
- trackInventory: true // FIXME: missing in UI
- });
- }
- }}
- onVariantClick={variantId => {
- navigate(productVariantEditUrl(productId, variantId));
- }}
- />
- )}
- </ProductVariantOperations>
- );
- }}
- </TypedProductVariantQuery>
- );
- }}
+ updateVariant.mutate({
+ attributes,
+ costPrice: data.costPrice ? data.costPrice : null,
+ id: variantId,
+ priceOverride: data.priceOverride
+ ? data.priceOverride
+ : null,
+ product: productId,
+ quantity: data.stock,
+ sku: data.sku,
+ trackInventory: true // FIXME: missing in UI
+ });
+ }
+ }}
+ onVariantClick={variantId => {
+ navigate(productVariantEditUrl(productId, variantId));
+ }}
+ />
+ );
+ }}
+ </ProductVariantOperations>
+ );
+ }}
+ </TypedProductVariantQuery>
+ )}
+ </Messages>
+ )}
</Navigator>
);
export default ProductVariant;