Skip to content

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

Add product type creation

dominik-zeglen676709c441f7
diff --git a/saleor/static/dashboard-next/gql-types.ts b/saleor/static/dashboard-next/gql-types.ts
index 40a73efb3e..046de2a8c6 100644
--- a/saleor/static/dashboard-next/gql-types.ts
+++ b/saleor/static/dashboard-next/gql-types.ts
@@ -487,6 +487,57 @@ export interface ProductTypeUpdateMutation {
} | null,
};
+export interface ProductTypeCreateMutationVariables {
+ input: ProductTypeInput,
+};
+
+export interface ProductTypeCreateMutation {
+ productTypeCreate: {
+ // List of errors that occurred executing the mutation.
+ errors: Array< {
+ // Name of a field that caused the error. A value of
+ // `null` indicates that the error isn't associated with a particular
+ // field.
+ field: string | null,
+ // The error message.
+ message: string | null,
+ } | null > | null,
+ productType: {
+ // The ID of the object.
+ id: string,
+ name: string,
+ hasVariants: boolean,
+ productAttributes: {
+ edges: Array< {
+ // The item at the end of the edge
+ node: {
+ // The ID of the object.
+ id: string,
+ // Internal representation of an attribute name.
+ slug: string | null,
+ // Visible name for display purposes.
+ name: string | null,
+ },
+ } >,
+ } | null,
+ variantAttributes: {
+ edges: Array< {
+ // The item at the end of the edge
+ node: {
+ // The ID of the object.
+ id: string,
+ // Internal representation of an attribute name.
+ slug: string | null,
+ // Visible name for display purposes.
+ name: string | null,
+ },
+ } >,
+ } | null,
+ isShippingRequired: boolean,
+ } | null,
+ } | null,
+};
+
export interface ProductTypeListQueryVariables {
after?: string | null,
before?: string | null,
diff --git a/saleor/static/dashboard-next/productTypes/components/ProductTypeDetailsPage/ProductTypeDetailsPage.tsx b/saleor/static/dashboard-next/productTypes/components/ProductTypeDetailsPage/ProductTypeDetailsPage.tsx
index 1a108a1e27..7362cdb8cd 100644
--- a/saleor/static/dashboard-next/productTypes/components/ProductTypeDetailsPage/ProductTypeDetailsPage.tsx
+++ b/saleor/static/dashboard-next/productTypes/components/ProductTypeDetailsPage/ProductTypeDetailsPage.tsx
@@ -28,7 +28,7 @@ export interface ProductTypeForm {
}
interface ProductTypeDetailsPageProps {
productType?: {
- id: string;
+ id?: string;
name?: string;
hasVariants?: boolean;
isShippingRequired?: boolean;
@@ -43,6 +43,7 @@ interface ProductTypeDetailsPageProps {
name: string;
}>;
disabled: boolean;
+ pageTitle: string;
saveButtonBarState: SaveButtonBarState;
searchLoading: boolean;
searchResults: Array<{
@@ -52,7 +53,7 @@ interface ProductTypeDetailsPageProps {
taxRates: string[];
onAttributeSearch: (name: string) => void;
onBack: () => void;
- onDelete: () => void;
+ onDelete?: () => void;
onSubmit: (data: ProductTypeForm) => void;
}
@@ -67,13 +68,14 @@ const ProductTypeDetailsPage = decorate<ProductTypeDetailsPageProps>(
({
classes,
disabled,
- productType,
+ pageTitle,
productAttributes,
- variantAttributes,
+ productType,
saveButtonBarState,
searchLoading,
searchResults,
taxRates,
+ variantAttributes,
onAttributeSearch,
onBack,
onDelete,
@@ -111,10 +113,7 @@ const ProductTypeDetailsPage = decorate<ProductTypeDetailsPageProps>(
>
{({ change, data, hasChanged, submit }) => (
<Container width="md">
- <PageHeader
- title={productType ? productType.name : undefined}
- onBack={onBack}
- />
+ <PageHeader title={pageTitle} onBack={onBack} />
<div className={classes.root}>
<div>
<ProductTypeDetails
diff --git a/saleor/static/dashboard-next/productTypes/index.tsx b/saleor/static/dashboard-next/productTypes/index.tsx
index fbd0e3a98b..45228b39da 100644
--- a/saleor/static/dashboard-next/productTypes/index.tsx
+++ b/saleor/static/dashboard-next/productTypes/index.tsx
@@ -2,11 +2,13 @@ import { parse as parseQs } from "qs";
import * as React from "react";
import { Route, RouteComponentProps, Switch } from "react-router-dom";
+import ProductTypeCreate from "./views/ProductTypeCreate";
import ProductTypeListComponent from "./views/ProductTypeList";
import ProductTypeUpdateComponent from "./views/ProductTypeUpdate";
-export const productTypeListUrl = "/productTypes";
+export const productTypeAddUrl = "/productTypes/add/";
export const productTypeDetailsUrl = (id: string) => `/productTypes/${id}/`;
+export const productTypeListUrl = "/productTypes";
const ProductTypeList: React.StatelessComponent<RouteComponentProps<{}>> = ({
location
@@ -35,6 +37,7 @@ export const ProductTypeRouter: React.StatelessComponent<
> = ({ match }) => (
<Switch>
<Route exact path={match.url} component={ProductTypeList} />
+ <Route exact path={match.url + "/add/"} component={ProductTypeCreate} />
<Route exact path={match.url + "/:id/"} component={ProductTypeUpdate} />
</Switch>
);
diff --git a/saleor/static/dashboard-next/productTypes/mutations.ts b/saleor/static/dashboard-next/productTypes/mutations.ts
index aac1d83ece..6ff5b12126 100644
--- a/saleor/static/dashboard-next/productTypes/mutations.ts
+++ b/saleor/static/dashboard-next/productTypes/mutations.ts
@@ -2,6 +2,8 @@ import gql from "graphql-tag";
import { Mutation, MutationProps } from "react-apollo";
import {
+ ProductTypeCreateMutation,
+ ProductTypeCreateMutationVariables,
ProductTypeDeleteMutation,
ProductTypeDeleteMutationVariables,
ProductTypeUpdateMutation,
@@ -62,3 +64,41 @@ export const productTypeUpdateMutation = gql`
}
}
`;
+
+export const TypedProductTypeCreateMutation = Mutation as React.ComponentType<
+ MutationProps<ProductTypeCreateMutation, ProductTypeCreateMutationVariables>
+>;
+export const productTypeCreateMutation = gql`
+ mutation ProductTypeCreate($input: ProductTypeInput!) {
+ productTypeCreate(input: $input) {
+ errors {
+ field
+ message
+ }
+ productType {
+ id
+ name
+ hasVariants
+ productAttributes {
+ edges {
+ node {
+ id
+ slug
+ name
+ }
+ }
+ }
+ variantAttributes {
+ edges {
+ node {
+ id
+ slug
+ name
+ }
+ }
+ }
+ isShippingRequired
+ }
+ }
+ }
+`;
diff --git a/saleor/static/dashboard-next/productTypes/views/ProductTypeCreate.tsx b/saleor/static/dashboard-next/productTypes/views/ProductTypeCreate.tsx
new file mode 100644
index 0000000000..d908a9a4c0
--- /dev/null
+++ b/saleor/static/dashboard-next/productTypes/views/ProductTypeCreate.tsx
@@ -0,0 +1,113 @@
+import * as React from "react";
+import Navigator from "../../components/Navigator";
+
+import { productTypeDetailsUrl, productTypeListUrl } from "..";
+import Messages from "../../components/messages";
+import { ProductTypeCreateMutation } from "../../gql-types";
+import i18n from "../../i18n";
+import ProductTypeDetailsPage, {
+ ProductTypeForm
+} from "../components/ProductTypeDetailsPage";
+import {
+ productTypeCreateMutation,
+ TypedProductTypeCreateMutation
+} from "../mutations";
+import { searchAttributeQuery, TypedSearchAttributeQuery } from "../queries";
+
+const taxRates = ["standard", "electronics", "food", "apparel"]; // FIXME: delet dis
+
+const formData = {
+ hasVariants: false,
+ isShippingRequired: false,
+ name: "",
+ productAttributes: [],
+ taxRate: undefined,
+ variantAttributes: []
+};
+
+export const ProductTypeUpdate: React.StatelessComponent = () => (
+ <Messages>
+ {pushMessage => (
+ <Navigator>
+ {navigate => (
+ <TypedSearchAttributeQuery query={searchAttributeQuery}>
+ {searchAttribute => {
+ const handleSearchAttribute = (search: string) =>
+ search ? searchAttribute.refetch({ search }) : undefined;
+ const handleCreateSuccess = (
+ updateData: ProductTypeCreateMutation
+ ) => {
+ if (updateData.productTypeCreate.errors.length === 0) {
+ pushMessage({
+ text: i18n.t("Successfully created product type")
+ });
+ navigate(
+ productTypeDetailsUrl(
+ updateData.productTypeCreate.productType.id
+ )
+ );
+ }
+ };
+ return (
+ <TypedProductTypeCreateMutation
+ mutation={productTypeCreateMutation}
+ onCompleted={handleCreateSuccess}
+ >
+ {(
+ createProductType,
+ { loading: loadingCreate, data: createProductTypeData }
+ ) => {
+ const handleCreate = (formData: ProductTypeForm) =>
+ createProductType({
+ variables: {
+ input: {
+ hasVariants: formData.hasVariants,
+ isShippingRequired: formData.isShippingRequired,
+ name: formData.name,
+ productAttributes: formData.productAttributes.map(
+ choice => choice.value
+ ),
+ variantAttributes: formData.variantAttributes.map(
+ choice => choice.value
+ )
+ }
+ }
+ });
+ return (
+ <ProductTypeDetailsPage
+ disabled={loadingCreate}
+ pageTitle={i18n.t("Create Product Type", {
+ context: "page title"
+ })}
+ productType={formData}
+ productAttributes={[]}
+ variantAttributes={[]}
+ saveButtonBarState={
+ loadingCreate ? "loading" : "default"
+ }
+ searchLoading={searchAttribute.loading}
+ searchResults={
+ searchAttribute.data &&
+ searchAttribute.data.attributes
+ ? searchAttribute.data.attributes.edges.map(
+ edge => edge.node
+ )
+ : []
+ }
+ taxRates={taxRates}
+ onAttributeSearch={handleSearchAttribute}
+ onBack={() => navigate(productTypeListUrl)}
+ onSubmit={handleCreate}
+ />
+ );
+ }}
+ </TypedProductTypeCreateMutation>
+ );
+ }}
+ </TypedSearchAttributeQuery>
+ )}
+ </Navigator>
+ )}
+ </Messages>
+);
+export default ProductTypeUpdate;
diff --git a/saleor/static/dashboard-next/productTypes/views/ProductTypeList.tsx b/saleor/static/dashboard-next/productTypes/views/ProductTypeList.tsx
index 9e295ade6f..5fb664d4da 100644
--- a/saleor/static/dashboard-next/productTypes/views/ProductTypeList.tsx
+++ b/saleor/static/dashboard-next/productTypes/views/ProductTypeList.tsx
@@ -1,6 +1,10 @@
import * as React from "react";
-import { productTypeDetailsUrl, productTypeListUrl } from "../";
+import {
+ productTypeAddUrl,
+ productTypeDetailsUrl,
+ productTypeListUrl
+} from "../";
import ErrorMessageCard from "../../components/ErrorMessageCard";
import Navigator from "../../components/Navigator";
import { createPaginationData, createPaginationState } from "../../misc";
@@ -54,7 +58,7 @@ export const ProductTypeList: React.StatelessComponent<
: undefined
}
pageInfo={pageInfo}
- onAdd={() => undefined}
+ onAdd={() => navigate(productTypeAddUrl)}
onNextPage={loadNextPage}
onPreviousPage={loadPreviousPage}
onRowClick={id => () => navigate(productTypeDetailsUrl(id))}
diff --git a/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate.tsx b/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate.tsx
index 791763ef24..1eb2ad025e 100644
--- a/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate.tsx
+++ b/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate.tsx
@@ -101,7 +101,12 @@ export const ProductTypeUpdate: React.StatelessComponent<
const loading = mutationLoading || dataLoading;
return (
<ProductTypeDetailsPage
- disabled={false}
+ disabled={loading}
+ pageTitle={
+ data && data.productType
+ ? data.productType.name
+ : undefined
+ }
productType={data ? data.productType : undefined}
productAttributes={
data &&