Skip to content

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

Make product create view work

dominik-zeglencea77fc38184
diff --git a/saleor/static/dashboard-next/gql-types.ts b/saleor/static/dashboard-next/gql-types.ts
index d7e9d42bd8..d29f12c302 100644
--- a/saleor/static/dashboard-next/gql-types.ts
+++ b/saleor/static/dashboard-next/gql-types.ts
@@ -633,6 +633,187 @@ export interface ProductUpdateMutation {
} | null,
};
+export interface ProductCreateMutationVariables {
+ attributes?: Array< AttributeValueInput | null > | null,
+ availableOn?: string | null,
+ category: string,
+ chargeTaxes: boolean,
+ collections?: Array< string | null > | null,
+ description?: string | null,
+ isPublished: boolean,
+ isFeatured: boolean,
+ name: string,
+ price?: string | null,
+ productType: string,
+};
+
+export interface ProductCreateMutation {
+ productCreate: {
+ // 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,
+ product: {
+ // The ID of the object.
+ id: string,
+ name: string,
+ description: string,
+ seoTitle: string | null,
+ seoDescription: string | null,
+ category: {
+ // The ID of the object.
+ id: string,
+ name: string,
+ },
+ collections: {
+ edges: Array< {
+ // The item at the end of the edge
+ node: {
+ // The ID of the object.
+ id: string,
+ name: string,
+ },
+ } >,
+ } | null,
+ // The product's base price (without any discounts
+ // applied).
+ price: {
+ // Amount of money.
+ amount: number,
+ // Currency code.
+ currency: string,
+ } | null,
+ margin: {
+ start: number | null,
+ stop: number | null,
+ } | null,
+ purchaseCost: {
+ // Lower bound of a price range.
+ start: {
+ // Amount of money.
+ amount: number,
+ // Currency code.
+ currency: string,
+ } | null,
+ // Upper bound of a price range.
+ stop: {
+ // Amount of money.
+ amount: number,
+ // Currency code.
+ currency: string,
+ } | null,
+ } | null,
+ isPublished: boolean,
+ isFeatured: boolean,
+ chargeTaxes: boolean,
+ availableOn: string | null,
+ // List of product attributes assigned to this product.
+ attributes: Array< {
+ // Name of an attribute
+ attribute: {
+ // The ID of the object.
+ id: string,
+ // Internal representation of an attribute name.
+ slug: string | null,
+ // Visible name for display purposes.
+ name: string | null,
+ // List of attribute's values.
+ values: Array< {
+ // Visible name for display purposes.
+ name: string | null,
+ // Internal representation of an attribute name.
+ slug: string | null,
+ } | null > | null,
+ } | null,
+ // Value of an attribute.
+ value: {
+ // The ID of the object.
+ id: string,
+ // Visible name for display purposes.
+ name: string | null,
+ // Internal representation of an attribute name.
+ slug: string | null,
+ } | null,
+ } | null > | null,
+ // Informs about product's availability in the storefront,
+ // current price and discounts.
+ availability: {
+ available: boolean | null,
+ priceRange: {
+ // Lower bound of a price range.
+ start: {
+ // Amount of money without taxes.
+ net: {
+ // Amount of money.
+ amount: number,
+ // Currency code.
+ currency: string,
+ },
+ } | null,
+ // Upper bound of a price range.
+ stop: {
+ // Amount of money without taxes.
+ net: {
+ // Amount of money.
+ amount: number,
+ // Currency code.
+ currency: string,
+ },
+ } | null,
+ } | null,
+ } | null,
+ images: {
+ edges: Array< {
+ // The item at the end of the edge
+ node: {
+ // The ID of the object.
+ id: string,
+ alt: string,
+ sortOrder: number,
+ url: string,
+ },
+ } >,
+ } | null,
+ variants: {
+ edges: Array< {
+ // The item at the end of the edge
+ node: {
+ // The ID of the object.
+ id: string,
+ sku: string,
+ name: string,
+ // Override the base price of a product if necessary.
+ // A value of `null` indicates that the default product price is used.
+ priceOverride: {
+ // Amount of money.
+ amount: number,
+ // Currency code.
+ currency: string,
+ } | null,
+ // Quantity of a product available for sale.
+ stockQuantity: number,
+ // Gross margin percentage value.
+ margin: number | null,
+ },
+ } >,
+ } | null,
+ productType: {
+ // The ID of the object.
+ id: string,
+ name: string,
+ hasVariants: boolean,
+ },
+ // The storefront URL for the product.
+ url: string,
+ } | null,
+ } | null,
+};
+
export interface VariantDeleteMutationVariables {
id: string,
};
@@ -1110,6 +1291,66 @@ export interface ProductVariantDetailsQuery {
} | null,
};
+export interface ProductCreateDataQuery {
+ // List of the shop's product types.
+ productTypes: {
+ edges: Array< {
+ // The item at the end of the edge
+ node: {
+ // 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,
+ // List of attribute's values.
+ values: Array< {
+ // The ID of the object.
+ id: string,
+ sortOrder: number,
+ // Visible name for display purposes.
+ name: string | null,
+ // Internal representation of an attribute name.
+ slug: string | null,
+ } | null > | null,
+ },
+ } >,
+ } | null,
+ },
+ } >,
+ } | null,
+ // List of the shop's collections.
+ collections: {
+ edges: Array< {
+ // The item at the end of the edge
+ node: {
+ // The ID of the object.
+ id: string,
+ name: string,
+ },
+ } >,
+ } | null,
+ // List of the shop's categories.
+ categories: {
+ edges: Array< {
+ // The item at the end of the edge
+ node: {
+ // The ID of the object.
+ id: string,
+ name: string,
+ },
+ } >,
+ } | null,
+};
+
export interface UserFragment {
// The ID of the object.
id: string,
diff --git a/saleor/static/dashboard-next/products/components/ProductCreatePage/ProductCreatePage.tsx b/saleor/static/dashboard-next/products/components/ProductCreatePage/ProductCreatePage.tsx
index 94ebbdeb5b..ae680642bc 100644
--- a/saleor/static/dashboard-next/products/components/ProductCreatePage/ProductCreatePage.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductCreatePage/ProductCreatePage.tsx
@@ -1,10 +1,8 @@
-import DialogContentText from "@material-ui/core/DialogContentText";
import { withStyles } from "@material-ui/core/styles";
import * as React from "react";
-import { AttributeType, AttributeValueType, MoneyType } from "../../";
+import { AttributeType } from "../../";
import { UserError } from "../../../";
-import ActionDialog from "../../../components/ActionDialog";
import Container from "../../../components/Container";
import Form from "../../../components/Form";
import PageHeader from "../../../components/PageHeader";
@@ -12,15 +10,11 @@ import SaveButtonBar, {
SaveButtonBarState
} from "../../../components/SaveButtonBar/SaveButtonBar";
import SeoForm from "../../../components/SeoForm";
-import Toggle from "../../../components/Toggle";
import i18n from "../../../i18n";
import ProductAvailabilityForm from "../ProductAvailabilityForm";
import ProductDetailsForm from "../ProductDetailsForm";
-import ProductImages from "../ProductImages";
import ProductOrganization from "../ProductOrganization";
import ProductPricing from "../ProductPricing";
-import ProductStock from "../ProductStock";
-import ProductVariants from "../ProductVariants";
interface ProductCreatePageProps {
errors: UserError[];
@@ -32,7 +26,7 @@ interface ProductCreatePageProps {
id: string;
name: string;
}>;
- disabled?: boolean;
+ disabled: boolean;
productTypes?: Array<{
id: string;
name: string;
@@ -112,94 +106,90 @@ export const ProductCreatePage = decorate<ProductCreatePageProps>(
stockQuantity: null
};
return (
- <Toggle>
- {(openedDeleteDialog, { toggle: toggleDeleteDialog }) => (
- <Form onSubmit={onSubmit} errors={userErrors} initial={initialData}>
- {({ change, data, errors, hasChanged, submit }) => (
- <Container width="md">
- <PageHeader title={header} onBack={onBack} />
- <div className={classes.root}>
- <div>
- <ProductDetailsForm
- data={data}
- disabled={disabled}
- errors={errors}
- onChange={change}
- />
- <div className={classes.cardContainer}>
- <ProductPricing
- // FIXME: this should be fetched from API
- currency="USD"
- data={data}
- disabled={disabled}
- onChange={change}
- />
- </div>
- <div className={classes.cardContainer}>
- <SeoForm
- helperText={i18n.t(
- "Add search engine title and description to make this product easier to find"
- )}
- title={data.seoTitle}
- titlePlaceholder={data.name}
- description={data.seoDescription}
- descriptionPlaceholder={data.description}
- loading={disabled}
- onChange={change}
- />
- </div>
- </div>
- <div>
- <ProductOrganization
- category={data.category}
- categories={
- categories !== undefined && categories !== null
- ? categories.map(category => ({
- label: category.name,
- value: category.id
- }))
- : []
- }
- errors={errors}
- productCollections={data.collections}
- collections={
- collections !== undefined && collections !== null
- ? collections.map(collection => ({
- label: collection.name,
- value: collection.id
- }))
- : []
- }
- productTypes={productTypes}
- data={data}
- disabled={disabled}
- onChange={change}
- />
- <div className={classes.cardContainer}>
- <ProductAvailabilityForm
- data={data}
- errors={errors}
- loading={disabled}
- onChange={change}
- />
- </div>
- </div>
+ <Form onSubmit={onSubmit} errors={userErrors} initial={initialData}>
+ {({ change, data, errors, hasChanged, submit }) => (
+ <Container width="md">
+ <PageHeader title={header} onBack={onBack} />
+ <div className={classes.root}>
+ <div>
+ <ProductDetailsForm
+ data={data}
+ disabled={disabled}
+ errors={errors}
+ onChange={change}
+ />
+ <div className={classes.cardContainer}>
+ <ProductPricing
+ // FIXME: this should be fetched from API
+ currency="USD"
+ data={data}
+ disabled={disabled}
+ onChange={change}
+ />
+ </div>
+ <div className={classes.cardContainer}>
+ <SeoForm
+ helperText={i18n.t(
+ "Add search engine title and description to make this product easier to find"
+ )}
+ title={data.seoTitle}
+ titlePlaceholder={data.name}
+ description={data.seoDescription}
+ descriptionPlaceholder={data.description}
+ loading={disabled}
+ onChange={change}
+ />
</div>
- <SaveButtonBar
- labels={{
- delete: i18n.t("Remove product"),
- save: i18n.t("Save product")
- }}
- onDelete={toggleDeleteDialog}
- onSave={submit}
- state={saveButtonBarState}
- disabled={disabled || !onSubmit || !hasChanged}
+ </div>
+ <div>
+ <ProductOrganization
+ category={data.category}
+ categories={
+ categories !== undefined && categories !== null
+ ? categories.map(category => ({
+ label: category.name,
+ value: category.id
+ }))
+ : []
+ }
+ errors={errors}
+ productCollections={data.collections}
+ collections={
+ collections !== undefined && collections !== null
+ ? collections.map(collection => ({
+ label: collection.name,
+ value: collection.id
+ }))
+ : []
+ }
+ productTypes={productTypes}
+ data={data}
+ disabled={disabled}
+ onChange={change}
/>
- </Container>
- )}
- </Form>
+ <div className={classes.cardContainer}>
+ <ProductAvailabilityForm
+ data={data}
+ errors={errors}
+ loading={disabled}
+ onChange={change}
+ />
+ </div>
+ </div>
+ </div>
+ <SaveButtonBar
+ labels={{
+ delete: i18n.t("Remove product"),
+ save: i18n.t("Save product")
+ }}
+ onCancel={onBack}
+ onSave={submit}
+ state={saveButtonBarState}
+ disabled={disabled || !onSubmit || !hasChanged}
+ />
+ </Container>
)}
- </Toggle>
+ </Form>
);
}
);
diff --git a/saleor/static/dashboard-next/products/components/ProductOrganization/ProductOrganization.tsx b/saleor/static/dashboard-next/products/components/ProductOrganization/ProductOrganization.tsx
index 8ee05b662f..5b1db708b2 100644
--- a/saleor/static/dashboard-next/products/components/ProductOrganization/ProductOrganization.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductOrganization/ProductOrganization.tsx
@@ -91,7 +91,7 @@ const ProductOrganization = decorate<ProductOrganizationProps>(
? data.productType.value.productAttributes.edges.map(edge => edge.node)
: [];
const getAttributeName = (slug: string) =>
- unrolledAttributes.filter(a => a.slug === slug)[0].slug;
+ unrolledAttributes.filter(a => a.slug === slug)[0].name;
const getAttributeValue = (slug: string) => {
if (unrolledAttributes && unrolledAttributes.length > 0) {
const value = data.attributes.filter(a => a.slug === slug)[0];
diff --git a/saleor/static/dashboard-next/products/index.tsx b/saleor/static/dashboard-next/products/index.tsx
index 8970f95b4b..bab8003b97 100644
--- a/saleor/static/dashboard-next/products/index.tsx
+++ b/saleor/static/dashboard-next/products/index.tsx
@@ -2,6 +2,7 @@ import { parse as parseQs } from "qs";
import * as React from "react";
import { Route, RouteComponentProps, Switch } from "react-router-dom";
+import ProductCreate from "./views/ProductCreate";
import ProductListComponent from "./views/ProductList";
import ProductUpdateComponent from "./views/ProductUpdate";
import ProductVariantComponent from "./views/ProductVariant";
@@ -37,6 +38,7 @@ const ProductVariant: React.StatelessComponent<RouteComponentProps<any>> = ({
const Component = ({ match }) => (
<Switch>
<Route exact path={match.url} component={ProductList} />
+ <Route exact path={`${match.url}/add/`} component={ProductCreate} />
<Route exact path={`${match.url}/:id/`} component={ProductUpdate} />
<Route
exact
diff --git a/saleor/static/dashboard-next/products/mutations.ts b/saleor/static/dashboard-next/products/mutations.ts
index 45ba4a9d3b..e2213b5932 100644
--- a/saleor/static/dashboard-next/products/mutations.ts
+++ b/saleor/static/dashboard-next/products/mutations.ts
@@ -2,6 +2,8 @@ import gql from "graphql-tag";
import { Mutation, MutationProps } from "react-apollo";
import {
+ ProductCreateMutation,
+ ProductCreateMutationVariables,
ProductDeleteMutation,
ProductDeleteMutationVariables,
ProductImageCreateMutation,
@@ -16,7 +18,11 @@ import {
VariantUpdateMutationVariables
} from "../gql-types";
-import { fragmentProduct, fragmentProductImage, fragmentVariant } from "./queries";
+import {
+ fragmentProduct,
+ fragmentProductImage,
+ fragmentVariant
+} from "./queries";
export const TypedProductImageCreateMutation = Mutation as React.ComponentType<
MutationProps<ProductImageCreateMutation, ProductImageCreateMutationVariables>
@@ -125,6 +131,51 @@ export const productUpdateMutation = gql`
}
`;
+export const TypedProductCreateMutation = Mutation as React.ComponentType<
+ MutationProps<ProductCreateMutation, ProductCreateMutationVariables>
+>;
+
+export const productCreateMutation = gql`
+ ${fragmentProduct}
+ mutation ProductCreate(
+ $attributes: [AttributeValueInput]
+ $availableOn: Date
+ $category: ID!
+ $chargeTaxes: Boolean!
+ $collections: [ID]
+ $description: String
+ $isPublished: Boolean!
+ $isFeatured: Boolean!
+ $name: String!
+ $price: Decimal
+ $productType: ID!
+ ) {
+ productCreate(
+ input: {
+ attributes: $attributes
+ availableOn: $availableOn
+ category: $category
+ chargeTaxes: $chargeTaxes
+ collections: $collections
+ description: $description
+ isPublished: $isPublished
+ isFeatured: $isFeatured
+ name: $name
+ price: $price
+ productType: $productType
+ }
+ ) {
+ errors {
+ field
+ message
+ }
+ product {
+ ...Product
+ }
+ }
+ }
+`;
+
export const TypedVariantDeleteMutation = Mutation as React.ComponentType<
MutationProps<VariantDeleteMutation, VariantDeleteMutationVariables>
>;
diff --git a/saleor/static/dashboard-next/products/queries.ts b/saleor/static/dashboard-next/products/queries.ts
index 2eedeef643..d85a0fa34e 100644
--- a/saleor/static/dashboard-next/products/queries.ts
+++ b/saleor/static/dashboard-next/products/queries.ts
@@ -2,6 +2,7 @@ import gql from "graphql-tag";
import { Query, QueryProps } from "react-apollo";
import {
+ ProductCreateDataQuery,
ProductDetailsQuery,
ProductDetailsQueryVariables,
ProductListQuery,
@@ -265,3 +266,51 @@ export const productVariantQuery = gql`
}
}
`;
+
+export const TypedProductCreateQuery = Query as React.ComponentType<
+ QueryProps<ProductCreateDataQuery>
+>;
+export const productCreateQuery = gql`
+ query ProductCreateData {
+ productTypes {
+ edges {
+ node {
+ id
+ name
+ hasVariants
+ productAttributes {
+ edges {
+ node {
+ id
+ slug
+ name
+ values {
+ id
+ sortOrder
+ name
+ slug
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ collections {
+ edges {
+ node {
+ id
+ name
+ }
+ }
+ }
+ categories {
+ edges {
+ node {
+ id
+ name
+ }
+ }
+ }
+ }
+`;
diff --git a/saleor/static/dashboard-next/products/views/ProductCreate.tsx b/saleor/static/dashboard-next/products/views/ProductCreate.tsx
new file mode 100644
index 0000000000..9cecf145b3
--- /dev/null
+++ b/saleor/static/dashboard-next/products/views/ProductCreate.tsx
@@ -0,0 +1,129 @@
+import * as React from "react";
+
+import ErrorMessageCard from "../../components/ErrorMessageCard";
+import Messages from "../../components/messages";
+import Navigator from "../../components/Navigator";
+import { ProductCreateMutation } from "../../gql-types";
+import i18n from "../../i18n";
+import ProductCreatePage from "../components/ProductCreatePage";
+import { productListUrl, productUrl } from "../index";
+import {
+ productCreateMutation,
+ TypedProductCreateMutation
+} from "../mutations";
+import { productCreateQuery, TypedProductCreateQuery } from "../queries";
+
+interface ProductUpdateProps {
+ id: string;
+}
+
+export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({
+ id
+}) => (
+ <Messages>
+ {pushMessage => {
+ return (
+ <Navigator>
+ {navigate => {
+ const handleAttributesEdit = () => {};
+ const handleBack = () => navigate(productListUrl);
+
+ return (
+ <TypedProductCreateQuery query={productCreateQuery}>
+ {({ data, error, loading }) => {
+ const handleError = () =>
+ pushMessage({ text: i18n.t("Something went wrong") });
+ const handleSuccess = (data: ProductCreateMutation) => {
+ if (data.productCreate.errors.length === 0) {
+ pushMessage({ text: i18n.t("Product created") });
+ navigate(productUrl(data.productCreate.product.id));
+ }
+ };
+
+ if (error) {
+ return (
+ <ErrorMessageCard
+ message={i18n.t("Something went wrong")}
+ />
+ );
+ }
+
+ return (
+ <TypedProductCreateMutation
+ mutation={productCreateMutation}
+ onCompleted={handleSuccess}
+ onError={handleError}
+ >
+ {(
+ productCreate,
+ {
+ data: productCreateData,
+ loading: productCreateDataLoading
+ }
+ ) => {
+ const handleSubmit = formData => {
+ productCreate({
+ variables: {
+ attributes: formData.attributes,
+ availableOn:
+ formData.availableOn !== ""
+ ? formData.availableOn
+ : null,
+ category: formData.category,
+ chargeTaxes: formData.chargeTaxes,
+ collections: formData.collections,
+ description: formData.description,
+ isFeatured: formData.featured,
+ isPublished: formData.available,
+ name: formData.name,
+ price: formData.price,
+ productType: formData.productType.value.id
+ }
+ });
+ };
+
+ const disabled = loading || productCreateDataLoading;
+ return (
+ <ProductCreatePage
+ categories={
+ data && data.categories
+ ? data.categories.edges.map(edge => edge.node)
+ : undefined
+ }
+ collections={
+ data && data.collections
+ ? data.collections.edges.map(edge => edge.node)
+ : undefined
+ }
+ disabled={disabled}
+ errors={
+ productCreateData &&
+ productCreateData.productCreate &&
+ productCreateData.productCreate.errors
+ ? productCreateData.productCreate.errors
+ : []
+ }
+ header={i18n.t("New Product")}
+ productTypes={
+ data && data.productTypes
+ ? data.productTypes.edges.map(edge => edge.node)
+ : undefined
+ }
+ onAttributesEdit={handleAttributesEdit}
+ onBack={handleBack}
+ onSubmit={handleSubmit}
+ />
+ );
+ }}
+ </TypedProductCreateMutation>
+ );
+ }}
+ </TypedProductCreateQuery>
+ );
+ }}
+ </Navigator>
+ );
+ }}
+ </Messages>
+);
+export default ProductUpdate;