Use translated errors in categories section

This commit is contained in:
dominik-zeglen 2020-03-05 15:59:25 +01:00
parent a7749b6aac
commit 852bd71beb
9 changed files with 54 additions and 46 deletions

View file

@ -11,7 +11,7 @@ import PageHeader from "@saleor/components/PageHeader";
import SaveButtonBar from "@saleor/components/SaveButtonBar"; import SaveButtonBar from "@saleor/components/SaveButtonBar";
import SeoForm from "@saleor/components/SeoForm"; import SeoForm from "@saleor/components/SeoForm";
import { sectionNames } from "@saleor/intl"; import { sectionNames } from "@saleor/intl";
import { UserError } from "../../../types"; import { ProductErrorFragment } from "@saleor/attributes/types/ProductErrorFragment";
import CategoryDetailsForm from "../../components/CategoryDetailsForm"; import CategoryDetailsForm from "../../components/CategoryDetailsForm";
interface FormData { interface FormData {
@ -29,7 +29,7 @@ const initialData: FormData = {
}; };
export interface CategoryCreatePageProps { export interface CategoryCreatePageProps {
errors: UserError[]; errors: ProductErrorFragment[];
disabled: boolean; disabled: boolean;
saveButtonBarState: ConfirmButtonTransitionState; saveButtonBarState: ConfirmButtonTransitionState;
onSubmit(data: FormData); onSubmit(data: FormData);

View file

@ -9,8 +9,8 @@ import CardTitle from "@saleor/components/CardTitle";
import FormSpacer from "@saleor/components/FormSpacer"; import FormSpacer from "@saleor/components/FormSpacer";
import RichTextEditor from "@saleor/components/RichTextEditor"; import RichTextEditor from "@saleor/components/RichTextEditor";
import { commonMessages } from "@saleor/intl"; import { commonMessages } from "@saleor/intl";
import { UserError } from "@saleor/types"; import { getFormErrors, getProductErrorMessage } from "@saleor/utils/errors";
import { getFieldError } from "@saleor/utils/errors"; import { ProductErrorFragment } from "@saleor/attributes/types/ProductErrorFragment";
import { maybe } from "../../../misc"; import { maybe } from "../../../misc";
import { CategoryDetails_category } from "../../types/CategoryDetails"; import { CategoryDetails_category } from "../../types/CategoryDetails";
@ -21,7 +21,7 @@ interface CategoryDetailsFormProps {
description: RawDraftContentState; description: RawDraftContentState;
}; };
disabled: boolean; disabled: boolean;
errors: UserError[]; errors: ProductErrorFragment[];
onChange: (event: React.ChangeEvent<any>) => void; onChange: (event: React.ChangeEvent<any>) => void;
} }
@ -34,6 +34,8 @@ export const CategoryDetailsForm: React.FC<CategoryDetailsFormProps> = ({
}) => { }) => {
const intl = useIntl(); const intl = useIntl();
const formErrors = getFormErrors(["name", "descriptionJson"], errors);
return ( return (
<Card> <Card>
<CardTitle <CardTitle
@ -49,16 +51,16 @@ export const CategoryDetailsForm: React.FC<CategoryDetailsFormProps> = ({
disabled={disabled} disabled={disabled}
value={data && data.name} value={data && data.name}
onChange={onChange} onChange={onChange}
error={!!getFieldError(errors, "name")} error={!!formErrors.name}
helperText={getFieldError(errors, "name")?.message} helperText={getProductErrorMessage(formErrors.name, intl)}
fullWidth fullWidth
/> />
</div> </div>
<FormSpacer /> <FormSpacer />
<RichTextEditor <RichTextEditor
disabled={disabled} disabled={disabled}
error={!!getFieldError(errors, "descriptionJson")} error={!!formErrors.descriptionJson}
helperText={getFieldError(errors, "descriptionJson")?.message} helperText={getProductErrorMessage(formErrors.descriptionJson, intl)}
label={intl.formatMessage({ label={intl.formatMessage({
defaultMessage: "Category Description" defaultMessage: "Category Description"
})} })}

View file

@ -15,8 +15,9 @@ import SaveButtonBar from "@saleor/components/SaveButtonBar";
import SeoForm from "@saleor/components/SeoForm"; import SeoForm from "@saleor/components/SeoForm";
import { Tab, TabContainer } from "@saleor/components/Tab"; import { Tab, TabContainer } from "@saleor/components/Tab";
import { sectionNames } from "@saleor/intl"; import { sectionNames } from "@saleor/intl";
import { ProductErrorFragment } from "@saleor/attributes/types/ProductErrorFragment";
import { maybe } from "../../../misc"; import { maybe } from "../../../misc";
import { TabListActions, UserError } from "../../../types"; import { TabListActions } from "../../../types";
import CategoryDetailsForm from "../../components/CategoryDetailsForm"; import CategoryDetailsForm from "../../components/CategoryDetailsForm";
import CategoryList from "../../components/CategoryList"; import CategoryList from "../../components/CategoryList";
import { import {
@ -44,7 +45,7 @@ export interface CategoryUpdatePageProps
extends TabListActions<"productListToolbar" | "subcategoryListToolbar"> { extends TabListActions<"productListToolbar" | "subcategoryListToolbar"> {
changeTab: (index: CategoryPageTab) => void; changeTab: (index: CategoryPageTab) => void;
currentTab: CategoryPageTab; currentTab: CategoryPageTab;
errors: UserError[]; errors: ProductErrorFragment[];
disabled: boolean; disabled: boolean;
category: CategoryDetails_category; category: CategoryDetails_category;
products: CategoryDetails_category_products_edges_node[]; products: CategoryDetails_category_products_edges_node[];

View file

@ -1,6 +1,7 @@
import gql from "graphql-tag"; import gql from "graphql-tag";
import makeMutation from "@saleor/hooks/makeMutation"; import makeMutation from "@saleor/hooks/makeMutation";
import { productErrorFragment } from "@saleor/attributes/mutations";
import { categoryDetailsFragment } from "./queries"; import { categoryDetailsFragment } from "./queries";
import { import {
CategoryBulkDelete, CategoryBulkDelete,
@ -20,11 +21,11 @@ import {
} from "./types/CategoryUpdate"; } from "./types/CategoryUpdate";
export const categoryDeleteMutation = gql` export const categoryDeleteMutation = gql`
${productErrorFragment}
mutation CategoryDelete($id: ID!) { mutation CategoryDelete($id: ID!) {
categoryDelete(id: $id) { categoryDelete(id: $id) {
errors { errors: productErrors {
field ...ProductErrorFragment
message
} }
} }
} }
@ -36,15 +37,15 @@ export const useCategoryDeleteMutation = makeMutation<
export const categoryCreateMutation = gql` export const categoryCreateMutation = gql`
${categoryDetailsFragment} ${categoryDetailsFragment}
${productErrorFragment}
mutation CategoryCreate($parent: ID, $input: CategoryInput!) { mutation CategoryCreate($parent: ID, $input: CategoryInput!) {
categoryCreate(parent: $parent, input: $input) { categoryCreate(parent: $parent, input: $input) {
errors {
field
message
}
category { category {
...CategoryDetailsFragment ...CategoryDetailsFragment
} }
errors: productErrors {
...ProductErrorFragment
}
} }
} }
`; `;
@ -55,15 +56,15 @@ export const useCategoryCreateMutation = makeMutation<
export const categoryUpdateMutation = gql` export const categoryUpdateMutation = gql`
${categoryDetailsFragment} ${categoryDetailsFragment}
${productErrorFragment}
mutation CategoryUpdate($id: ID!, $input: CategoryInput!) { mutation CategoryUpdate($id: ID!, $input: CategoryInput!) {
categoryUpdate(id: $id, input: $input) { categoryUpdate(id: $id, input: $input) {
errors {
field
message
}
category { category {
...CategoryDetailsFragment ...CategoryDetailsFragment
} }
errors: productErrors {
...ProductErrorFragment
}
} }
} }
`; `;
@ -73,11 +74,11 @@ export const useCategoryUpdateMutation = makeMutation<
>(categoryUpdateMutation); >(categoryUpdateMutation);
export const categoryBulkDeleteMutation = gql` export const categoryBulkDeleteMutation = gql`
${productErrorFragment}
mutation CategoryBulkDelete($ids: [ID]!) { mutation CategoryBulkDelete($ids: [ID]!) {
categoryBulkDelete(ids: $ids) { categoryBulkDelete(ids: $ids) {
errors { errors: productErrors {
field ...ProductErrorFragment
message
} }
} }
} }

View file

@ -2,14 +2,16 @@
/* eslint-disable */ /* eslint-disable */
// This file was automatically generated and should not be edited. // This file was automatically generated and should not be edited.
import { ProductErrorCode } from "./../../types/globalTypes";
// ==================================================== // ====================================================
// GraphQL mutation operation: CategoryBulkDelete // GraphQL mutation operation: CategoryBulkDelete
// ==================================================== // ====================================================
export interface CategoryBulkDelete_categoryBulkDelete_errors { export interface CategoryBulkDelete_categoryBulkDelete_errors {
__typename: "Error"; __typename: "ProductError";
code: ProductErrorCode;
field: string | null; field: string | null;
message: string | null;
} }
export interface CategoryBulkDelete_categoryBulkDelete { export interface CategoryBulkDelete_categoryBulkDelete {

View file

@ -2,18 +2,12 @@
/* eslint-disable */ /* eslint-disable */
// This file was automatically generated and should not be edited. // This file was automatically generated and should not be edited.
import { CategoryInput } from "./../../types/globalTypes"; import { CategoryInput, ProductErrorCode } from "./../../types/globalTypes";
// ==================================================== // ====================================================
// GraphQL mutation operation: CategoryCreate // GraphQL mutation operation: CategoryCreate
// ==================================================== // ====================================================
export interface CategoryCreate_categoryCreate_errors {
__typename: "Error";
field: string | null;
message: string | null;
}
export interface CategoryCreate_categoryCreate_category_backgroundImage { export interface CategoryCreate_categoryCreate_category_backgroundImage {
__typename: "Image"; __typename: "Image";
alt: string | null; alt: string | null;
@ -36,10 +30,16 @@ export interface CategoryCreate_categoryCreate_category {
parent: CategoryCreate_categoryCreate_category_parent | null; parent: CategoryCreate_categoryCreate_category_parent | null;
} }
export interface CategoryCreate_categoryCreate_errors {
__typename: "ProductError";
code: ProductErrorCode;
field: string | null;
}
export interface CategoryCreate_categoryCreate { export interface CategoryCreate_categoryCreate {
__typename: "CategoryCreate"; __typename: "CategoryCreate";
errors: CategoryCreate_categoryCreate_errors[];
category: CategoryCreate_categoryCreate_category | null; category: CategoryCreate_categoryCreate_category | null;
errors: CategoryCreate_categoryCreate_errors[];
} }
export interface CategoryCreate { export interface CategoryCreate {

View file

@ -2,14 +2,16 @@
/* eslint-disable */ /* eslint-disable */
// This file was automatically generated and should not be edited. // This file was automatically generated and should not be edited.
import { ProductErrorCode } from "./../../types/globalTypes";
// ==================================================== // ====================================================
// GraphQL mutation operation: CategoryDelete // GraphQL mutation operation: CategoryDelete
// ==================================================== // ====================================================
export interface CategoryDelete_categoryDelete_errors { export interface CategoryDelete_categoryDelete_errors {
__typename: "Error"; __typename: "ProductError";
code: ProductErrorCode;
field: string | null; field: string | null;
message: string | null;
} }
export interface CategoryDelete_categoryDelete { export interface CategoryDelete_categoryDelete {

View file

@ -2,18 +2,12 @@
/* eslint-disable */ /* eslint-disable */
// This file was automatically generated and should not be edited. // This file was automatically generated and should not be edited.
import { CategoryInput } from "./../../types/globalTypes"; import { CategoryInput, ProductErrorCode } from "./../../types/globalTypes";
// ==================================================== // ====================================================
// GraphQL mutation operation: CategoryUpdate // GraphQL mutation operation: CategoryUpdate
// ==================================================== // ====================================================
export interface CategoryUpdate_categoryUpdate_errors {
__typename: "Error";
field: string | null;
message: string | null;
}
export interface CategoryUpdate_categoryUpdate_category_backgroundImage { export interface CategoryUpdate_categoryUpdate_category_backgroundImage {
__typename: "Image"; __typename: "Image";
alt: string | null; alt: string | null;
@ -36,10 +30,16 @@ export interface CategoryUpdate_categoryUpdate_category {
parent: CategoryUpdate_categoryUpdate_category_parent | null; parent: CategoryUpdate_categoryUpdate_category_parent | null;
} }
export interface CategoryUpdate_categoryUpdate_errors {
__typename: "ProductError";
code: ProductErrorCode;
field: string | null;
}
export interface CategoryUpdate_categoryUpdate { export interface CategoryUpdate_categoryUpdate {
__typename: "CategoryUpdate"; __typename: "CategoryUpdate";
errors: CategoryUpdate_categoryUpdate_errors[];
category: CategoryUpdate_categoryUpdate_category | null; category: CategoryUpdate_categoryUpdate_category | null;
errors: CategoryUpdate_categoryUpdate_errors[];
} }
export interface CategoryUpdate { export interface CategoryUpdate {

View file

@ -99,7 +99,7 @@ export const CategoryDetails: React.FC<CategoryDetailsProps> = ({
); );
if (backgroundImageError) { if (backgroundImageError) {
notify({ notify({
text: backgroundImageError.message text: intl.formatMessage(commonMessages.somethingWentWrong)
}); });
} }
} }