Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add form errors handling
dominik-zeglen1ccb8d1a1354
diff --git a/saleor/static/dashboard-next/components/Form/Form.tsx b/saleor/static/dashboard-next/components/Form/Form.tsxindex d9186244ad..f6ac4cacae 100644--- a/saleor/static/dashboard-next/components/Form/Form.tsx+++ b/saleor/static/dashboard-next/components/Form/Form.tsx@@ -1,4 +1,5 @@import * as React from "react";+import { UserError } from "../../";export interface FormProps<T extends {}> {children:@@ -6,11 +7,13 @@ export interface FormProps<T extends {}> {props: {data: T;hasChanged: boolean;+ errors: { [key: string]: string };change(event: React.ChangeEvent<any>);submit(event: React.FormEvent<any>);}) => React.ReactElement<any>)| React.ReactNode;+ errors?: UserError[];initial?: T;useForm?: boolean;onSubmit?(data: T);@@ -46,7 +49,7 @@ class Form<T extends {} = {}> extends React.Component<FormProps<T>, T> {};render() {- const { children, useForm = true } = this.props;+ const { children, errors, useForm = true } = this.props;let contents = children;@@ -54,6 +57,12 @@ class Form<T extends {} = {}> extends React.Component<FormProps<T>, T> {contents = children({change: this.handleChange,data: this.state,+ errors: errors+ ? errors.reduce(+ (prev, curr) => ({ ...prev, [curr.field]: curr.message }),+ {}+ )+ : {},hasChanged:JSON.stringify(this.props.initial) !== JSON.stringify(this.state),submit: this.handleSubmitdiff --git a/saleor/static/dashboard-next/index.tsx b/saleor/static/dashboard-next/index.tsxindex 1e8faede35..db98a73fc1 100644--- a/saleor/static/dashboard-next/index.tsx+++ b/saleor/static/dashboard-next/index.tsx@@ -69,11 +69,15 @@ export interface PageListProps extends ListProps {onAdd: () => void;}-export interface MutationProviderProps<T extends {}> {+export interface UserError {+ field: string;+ message: string;+}+export interface PartialMutationProviderProps<T extends {}> {onSuccess?: (data: T) => void;onError?: (error: ApolloError) => void;}-export interface MutationProviderPartialOutput<+export interface PartialMutationProviderOutput<TData extends {} = {},TVariables extends {} = {}> {@@ -81,14 +85,8 @@ export interface MutationProviderPartialOutput<loading: boolean;mutate: (variables: TVariables) => void;}-export interface MutationProviderOutput<- TData extends {} = {},- TVariables extends {} = {}-> extends MutationProviderPartialOutput<TData, TVariables> {- error: ApolloError;-}-export interface MutationProviderRenderProps<+export interface PartialMutationProviderRenderProps<TData extends {} = {},TVariables extends {} = {}> {@@ -97,3 +95,7 @@ export interface MutationProviderRenderProps<error?: ApolloError;mutate: MutationFn<TData, TVariables>;}++export type MutationProviderRenderProps<T> = T & {+ errors: UserError[];+};diff --git a/saleor/static/dashboard-next/products/components/ProductAvailabilityForm/ProductAvailabilityForm.tsx b/saleor/static/dashboard-next/products/components/ProductAvailabilityForm/ProductAvailabilityForm.tsxindex ecc57926df..c4215e42c9 100644--- a/saleor/static/dashboard-next/products/components/ProductAvailabilityForm/ProductAvailabilityForm.tsx+++ b/saleor/static/dashboard-next/products/components/ProductAvailabilityForm/ProductAvailabilityForm.tsx@@ -15,6 +15,7 @@ interface ProductAvailabilityFormProps {chargeTaxes: boolean;featured: boolean;};+ errors: { [key: string]: string };loading?: boolean;onChange(event: any);}@@ -28,6 +29,7 @@ export const ProductAvailabilityForm = decorate<ProductAvailabilityFormProps>(({classes,data: { available, availableOn, chargeTaxes, featured },+ errors,loading,onChange}) => (@@ -45,10 +47,12 @@ export const ProductAvailabilityForm = decorate<ProductAvailabilityFormProps>(</div>{available && (<TextField+ error={!!errors.availableOn}disabled={loading}label={i18n.t("Publish product on")}name="availableOn"type="date"+ helperText={errors.availableOn}value={availableOn ? availableOn : ""}onChange={onChange}className={classes.date}diff --git a/saleor/static/dashboard-next/products/components/ProductCategoryAndCollectionsForm/ProductCategoryAndCollectionsForm.tsx b/saleor/static/dashboard-next/products/components/ProductCategoryAndCollectionsForm/ProductCategoryAndCollectionsForm.tsxindex 6e19a45f53..20c903fbb1 100644--- a/saleor/static/dashboard-next/products/components/ProductCategoryAndCollectionsForm/ProductCategoryAndCollectionsForm.tsx+++ b/saleor/static/dashboard-next/products/components/ProductCategoryAndCollectionsForm/ProductCategoryAndCollectionsForm.tsx@@ -10,13 +10,13 @@ import SingleSelectField from "../../../components/SingleSelectField";import i18n from "../../../i18n";interface ProductCategoryAndCollectionsFormProps {- // TODO: TYPE IT- categories?: any[];- collections?: any[];- productCollections?: any[];- category?: any;+ categories?: Array<{ value: string; label: string }>;+ collections?: Array<{ value: string; label: string }>;+ errors: { [key: string]: string };+ productCollections?: string[];+ category?: string;loading?: boolean;- onChange(event: any);+ onChange: (event: React.ChangeEvent<any>) => void;}const decorate = withStyles(theme => ({}));@@ -27,6 +27,7 @@ const ProductCategoryAndCollectionsForm = decorate<classes,categories,collections,+ errors,productCollections,category,loading,@@ -37,6 +38,8 @@ const ProductCategoryAndCollectionsForm = decorate<<CardContent><SingleSelectFielddisabled={loading}+ error={!!errors.category}+ hint={errors.category}label={i18n.t("Category")}choices={loading ? [] : categories}name="category"@@ -46,6 +49,8 @@ const ProductCategoryAndCollectionsForm = decorate<<FormSpacer /><MultiSelectFielddisabled={loading}+ error={!!errors.collections}+ hint={errors.collections}label={i18n.t("Collections")}choices={loading ? [] : collections}name="collections"diff --git a/saleor/static/dashboard-next/products/components/ProductDetailsForm/ProductDetailsForm.tsx b/saleor/static/dashboard-next/products/components/ProductDetailsForm/ProductDetailsForm.tsxindex 8b86ff357e..e84879dec4 100644--- a/saleor/static/dashboard-next/products/components/ProductDetailsForm/ProductDetailsForm.tsx+++ b/saleor/static/dashboard-next/products/components/ProductDetailsForm/ProductDetailsForm.tsx@@ -13,6 +13,7 @@ interface ProductDetailsFormProps {currencySymbol?: string;description?: string;disabled?: boolean;+ errors: { [key: string]: string };name?: string;price?: number;onChange(event: any);@@ -31,6 +32,7 @@ export const ProductDetailsForm = decorate<ProductDetailsFormProps>(currencySymbol,description,disabled,+ errors,name,onChange,price@@ -39,6 +41,8 @@ export const ProductDetailsForm = decorate<ProductDetailsFormProps>(<CardContent><div className={classes.root}><TextField+ error={!!errors.name}+ helperText={errors.name}disabled={disabled}fullWidthkey="nameInput"@@ -50,6 +54,9 @@ export const ProductDetailsForm = decorate<ProductDetailsFormProps>(/><span /><PriceField+ error={!!errors.price}+ name="price"+ hint={errors.price}currencySymbol={currencySymbol}disabled={disabled}key="priceInput"@@ -60,9 +67,14 @@ export const ProductDetailsForm = decorate<ProductDetailsFormProps>(</div><FormSpacer /><RichTextEditor+ error={!!errors.description}disabled={disabled}fullWidth- helperText={i18n.t("Select text to enable text-formatting tools.")}+ helperText={+ errors.description+ ? errors.description+ : i18n.t("Select text to enable text-formatting tools.")+ }key="descriptionInput"label={i18n.t("Description")}name="description"diff --git a/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx b/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsxindex 27ad11f9c8..ac13659e61 100644--- a/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx+++ b/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx@@ -6,6 +6,7 @@ import VisibilityIcon from "@material-ui/icons/Visibility";import * as React from "react";import { AttributeType, AttributeValueType, MoneyType } from "../../";+import { UserError } from "../../../";import ActionDialog from "../../../components/ActionDialog";import Container from "../../../components/Container";import Form from "../../../components/Form";@@ -25,6 +26,7 @@ import ProductPrice from "../ProductPrice/ProductPrice";import ProductVariants from "../ProductVariants";interface ProductUpdateProps {+ errors: UserError[];placeholderImage: string;collections?: Array<{id: string;@@ -128,6 +130,7 @@ export const ProductUpdate = decorate<ProductUpdateProps>(disabled,categories,collections,+ errors: userErrors,images,placeholderImage,product,@@ -170,10 +173,11 @@ export const ProductUpdate = decorate<ProductUpdateProps>(return (<FormonSubmit={onSubmit}+ errors={userErrors}initial={initialData}key={product ? JSON.stringify(product) : "loading"}>- {({ change, data, hasChanged, submit }) => (+ {({ change, data, errors, hasChanged, submit }) => (<Container width="md"><Toggle>{(openedDeleteDialog, { toggle: toggleDeleteDialog }) => (@@ -226,6 +230,7 @@ export const ProductUpdate = decorate<ProductUpdateProps>(<div><ProductDetailsFormonChange={change}+ errors={errors}name={data.name}description={data.description}currencySymbol={@@ -267,6 +272,7 @@ export const ProductUpdate = decorate<ProductUpdateProps>(<div><ProductAvailabilityFormdata={data}+ errors={errors}loading={disabled}onChange={change}/>@@ -287,6 +293,7 @@ export const ProductUpdate = decorate<ProductUpdateProps>(})): []}+ errors={errors}productCollections={data.collections}collections={collections !== undefined && collections !== nulldiff --git a/saleor/static/dashboard-next/products/containers/ProductImagesReorder.tsx b/saleor/static/dashboard-next/products/containers/ProductImagesReorder.tsxindex c322673bb1..41cd0f9638 100644--- a/saleor/static/dashboard-next/products/containers/ProductImagesReorder.tsx+++ b/saleor/static/dashboard-next/products/containers/ProductImagesReorder.tsx@@ -1,6 +1,5 @@import * as React from "react";-import ErrorMessageCard from "../../components/ErrorMessageCard";import {ProductDetailsQuery,ProductImageReorderMutation,@@ -8,14 +7,17 @@ import {} from "../../gql-types";import { productImagesReorder, TypedProductImagesReorder } from "../mutations";-import { MutationProviderProps, MutationProviderRenderProps } from "../..";+import {+ PartialMutationProviderProps,+ PartialMutationProviderRenderProps+} from "../..";import { productDetailsQuery } from "../queries";interface ProductImagesReorderProviderProps- extends MutationProviderProps<ProductImageReorderMutation> {+ extends PartialMutationProviderProps<ProductImageReorderMutation> {productId: string;children: ((- props: MutationProviderRenderProps<+ props: PartialMutationProviderRenderProps<ProductImageReorderMutation,ProductImageReorderMutationVariables>@@ -40,17 +42,14 @@ const ProductImagesReorderProvider: React.StatelessComponent<cache.writeQuery({ query: productDetailsQuery, data });}}>- {(mutate, { data, error, loading }) => {- if (error) {- return <ErrorMessageCard message={error.message} />;- }- return children({+ {(mutate, { data, error, loading }) =>+ children({data,error,loading,mutate- });- }}+ })+ }</TypedProductImagesReorder>);diff --git a/saleor/static/dashboard-next/products/containers/ProductUpdate.tsx b/saleor/static/dashboard-next/products/containers/ProductUpdate.tsxindex 110538be44..ce9d2735b9 100644--- a/saleor/static/dashboard-next/products/containers/ProductUpdate.tsx+++ b/saleor/static/dashboard-next/products/containers/ProductUpdate.tsx@@ -1,7 +1,9 @@import * as React from "react";-import { MutationProviderProps, MutationProviderRenderProps } from "../..";-import ErrorMessageCard from "../../components/ErrorMessageCard";+import {+ PartialMutationProviderProps,+ PartialMutationProviderRenderProps+} from "../..";import {ProductDetailsQuery,ProductUpdateMutation,@@ -14,10 +16,10 @@ import {import { productDetailsQuery } from "../queries";interface ProductUpdateProviderProps- extends MutationProviderProps<ProductUpdateMutation> {+ extends PartialMutationProviderProps<ProductUpdateMutation> {productId: string;children: ((- props: MutationProviderRenderProps<+ props: PartialMutationProviderRenderProps<ProductUpdateMutation,ProductUpdateMutationVariables>@@ -40,17 +42,14 @@ const ProductUpdateProvider: React.StatelessComponent<cache.writeQuery({ query: productDetailsQuery, data });}}>- {(mutate, { data, error, loading }) => {- if (error) {- return <ErrorMessageCard message={error.message} />;- }- return children({+ {(mutate, { data, error, loading }) =>+ children({data,error,loading,mutate- });- }}+ })+ }</TypedProductUpdateMutation>);diff --git a/saleor/static/dashboard-next/products/containers/ProductUpdateOperations.tsx b/saleor/static/dashboard-next/products/containers/ProductUpdateOperations.tsxindex 4fc39cc8a4..10b0f88ba4 100644--- a/saleor/static/dashboard-next/products/containers/ProductUpdateOperations.tsx+++ b/saleor/static/dashboard-next/products/containers/ProductUpdateOperations.tsx@@ -1,11 +1,12 @@import * as React from "react";+import { ApolloError } from "apollo-client";import {- MutationProviderPartialOutput,- MutationProviderProps,- MutationProviderRenderProps+ MutationProviderRenderProps,+ PartialMutationProviderOutput,+ PartialMutationProviderProps,+ PartialMutationProviderRenderProps} from "../..";-import ErrorMessageCard from "../../components/ErrorMessageCard";import {ProductDeleteMutation,ProductDeleteMutationVariables,@@ -28,10 +29,10 @@ import ProductImagesReorderProvider from "./ProductImagesReorder";import ProductUpdateProvider from "./ProductUpdate";interface ProductDeleteProviderProps- extends MutationProviderProps<ProductDeleteMutation> {+ extends PartialMutationProviderProps<ProductDeleteMutation> {productId: string;children: ((- props: MutationProviderRenderProps<+ props: PartialMutationProviderRenderProps<ProductDeleteMutation,ProductDeleteMutationVariables>@@ -47,25 +48,22 @@ const ProductDeleteProvider: React.StatelessComponent<onCompleted={onSuccess}onError={onError}>- {(mutate, { data, error, loading }) => {- if (error) {- return <ErrorMessageCard message={error.message} />;- }- return children({+ {(mutate, { data, error, loading }) =>+ children({data,error,loading,mutate- });- }}+ })+ }</TypedProductDeleteMutation>);interface ProductImageCreateProviderProps- extends MutationProviderProps<ProductImageCreateMutation> {+ extends PartialMutationProviderProps<ProductImageCreateMutation> {productId: string;children: ((- props: MutationProviderRenderProps<+ props: PartialMutationProviderRenderProps<ProductImageCreateMutation,ProductImageCreateMutationVariables>@@ -92,44 +90,39 @@ const ProductImageCreateProvider: React.StatelessComponent<onCompleted={onSuccess}onError={onError}>- {(mutate, { data, error, loading }) => {- if (error) {- return <ErrorMessageCard message={error.message} />;- }- return children({+ {(mutate, { data, error, loading }) =>+ children({data,error,loading,mutate- });- }}+ })+ }</TypedProductImageCreateMutation>);interface ProductUpdateOperationsProps- extends MutationProviderProps<ProductImageCreateMutation> {+ extends PartialMutationProviderProps<ProductImageCreateMutation> {product?: ProductDetailsQuery["product"];children: (- props: {- createProductImage: MutationProviderPartialOutput<+ props: MutationProviderRenderProps<{+ createProductImage: PartialMutationProviderOutput<ProductImageCreateMutation,ProductImageCreateMutationVariables>;- deleteProduct: MutationProviderPartialOutput;- reorderProductImages: Exclude<- MutationProviderPartialOutput<- ProductImageReorderMutation,- ProductImageReorderMutationVariables- >,- { error: any }+ deleteProduct: PartialMutationProviderOutput;+ reorderProductImages: PartialMutationProviderOutput<+ ProductImageReorderMutation,+ ProductImageReorderMutationVariables>;- updateProduct: MutationProviderPartialOutput<+ updateProduct: PartialMutationProviderOutput<ProductUpdateMutation,ProductUpdateMutationVariables>;- }+ }>) => React.ReactElement<any>;onDelete?: (data: ProductDeleteMutation) => void;+ onError?: (error: ApolloError) => void;onImageCreate?: (data: ProductImageCreateMutation) => void;onImageReorder?: (data: ProductImageReorderMutation) => void;onUpdate?: (data: ProductUpdateMutation) => void;@@ -141,26 +134,34 @@ const ProductUpdateOperations: React.StatelessComponent<product,children,onDelete,+ onError,onImageCreate,onImageReorder,onUpdate}) => {const productId = product ? product.id : "";return (- <ProductUpdateProvider productId={productId} onSuccess={onUpdate}>+ <ProductUpdateProvider+ productId={productId}+ onError={onError}+ onSuccess={onUpdate}+ >{updateProduct => (<ProductImagesReorderProviderproductId={productId}+ onError={onError}onSuccess={onImageReorder}>{reorderProductImages => (<ProductImageCreateProviderproductId={productId}+ onError={onError}onSuccess={onImageCreate}>{createProductImage => (<ProductDeleteProviderproductId={productId}+ onError={onError}onSuccess={onDelete}>{deleteProduct =>@@ -176,6 +177,12 @@ const ProductUpdateOperations: React.StatelessComponent<loading: deleteProduct.loading,mutate: deleteProduct.mutate},+ errors:+ updateProduct &&+ updateProduct.data &&+ updateProduct.data.productUpdate+ ? updateProduct.data.productUpdate.errors+ : [],reorderProductImages: {data: reorderProductImages.data,loading: reorderProductImages.loading,diff --git a/saleor/static/dashboard-next/products/views/ProductUpdate.tsx b/saleor/static/dashboard-next/products/views/ProductUpdate.tsxindex 35c97f512b..d434723e93 100644--- a/saleor/static/dashboard-next/products/views/ProductUpdate.tsx+++ b/saleor/static/dashboard-next/products/views/ProductUpdate.tsx@@ -11,6 +11,7 @@ import ProductUpdatePage from "../components/ProductUpdatePage";import ProductUpdateOperations from "../containers/ProductUpdateOperations";import { productListUrl, productVariantEditUrl } from "../index";import { productDetailsQuery, TypedProductDetailsQuery } from "../queries";+import { ApolloError } from "apollo-client";interface ProductUpdateProps {id: string;@@ -39,6 +40,10 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({pushMessage({ text: i18n.t("Product removed") });navigate(categoryShowUrl(data.product.category.id));};+ const handleError = (error: ApolloError) => {+ console.error(error.message);+ pushMessage({ text: i18n.t("Something went wrong") });+ };const handleUpdate = () =>pushMessage({ text: i18n.t("Saved changes") });const handleImageCreate = () =>@@ -58,17 +63,18 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({data && data.product? data.product.images.edges.map(edge => edge.node): undefined;-return (<ProductUpdateOperationsproduct={product}onDelete={handleDelete}+ onError={handleError}onImageCreate={handleImageCreate}onUpdate={handleUpdate}>{({createProductImage,deleteProduct,+ errors,reorderProductImages,updateProduct}) => {@@ -85,6 +91,7 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({categories={allCategories}collections={allCollections}disabled={disableFormSave}+ errors={errors}saveButtonBarState={formSubmitState}images={images}placeholderImage={placeholderImg}