Skip to content

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

Add collection creating

dominik-zeglenae2db9deed8f
diff --git a/saleor/static/dashboard-next/collections/components/CollectionCreatePage/CollectionCreatePage.tsx b/saleor/static/dashboard-next/collections/components/CollectionCreatePage/CollectionCreatePage.tsx
index 23924b950e..5fe0dae47a 100644
--- a/saleor/static/dashboard-next/collections/components/CollectionCreatePage/CollectionCreatePage.tsx
+++ b/saleor/static/dashboard-next/collections/components/CollectionCreatePage/CollectionCreatePage.tsx
@@ -14,8 +14,13 @@ import SaveButtonBar from "../../../components/SaveButtonBar";
import SeoForm from "../../../components/SeoForm";
import i18n from "../../../i18n";
import CollectionDetails from "../CollectionDetails/CollectionDetails";
+import { CollectionImage } from "../CollectionImage/CollectionImage";
export interface CollectionCreatePageFormData {
+ backgroundImage: {
+ url: string;
+ value: string;
+ };
name: string;
isPublished: boolean;
seoDescription: string;
@@ -30,6 +35,10 @@ export interface CollectionCreatePageProps {
}
const initialForm: CollectionCreatePageFormData = {
+ backgroundImage: {
+ url: null,
+ value: null
+ },
isPublished: false,
name: "",
seoDescription: "",
@@ -63,6 +72,36 @@ const CollectionCreatePage = decorate<CollectionCreatePageProps>(
onChange={change}
/>
<CardSpacer />
+ <CollectionImage
+ image={
+ data.backgroundImage.url
+ ? { __typename: "Image", url: data.backgroundImage.url }
+ : null
+ }
+ onImageDelete={() =>
+ change({
+ target: {
+ name: "backgroundImage",
+ value: {
+ url: null,
+ value: null
+ }
+ }
+ } as any)
+ }
+ onImageUpload={event =>
+ change({
+ target: {
+ name: "backgroundImage",
+ value: {
+ url: URL.createObjectURL(event.target.files[0]),
+ value: event.target.files[0]
+ }
+ }
+ } as any)
+ }
+ />
+ <CardSpacer />
<SeoForm
description={data.seoDescription}
disabled={disabled}
diff --git a/saleor/static/dashboard-next/collections/index.tsx b/saleor/static/dashboard-next/collections/index.tsx
index 10d6495825..16b8aafc96 100644
--- a/saleor/static/dashboard-next/collections/index.tsx
+++ b/saleor/static/dashboard-next/collections/index.tsx
@@ -2,7 +2,8 @@ import { parse as parseQs } from "qs";
import * as React from "react";
import { Route, RouteComponentProps, Switch } from "react-router-dom";
-import { collectionListUrl, collectionUrl } from "./urls";
+import { collectionAddUrl, collectionListUrl, collectionUrl } from "./urls";
+import CollectionCreate from "./views/CollectionCreate";
import CollectionDetailsView from "./views/CollectionDetails";
import CollectionListView from "./views/CollectionList";
@@ -39,7 +40,7 @@ const CollectionDetails: React.StatelessComponent<
const Component = () => (
<Switch>
<Route exact path={collectionListUrl} component={CollectionList} />
- {/* <Route exact path={`${match.url}/add/`} component={CategoryCreate} /> */}
+ <Route exact path={collectionAddUrl} component={CollectionCreate} />
<Route path={collectionUrl(":id")} component={CollectionDetails} />
</Switch>
);
diff --git a/saleor/static/dashboard-next/collections/mutations.ts b/saleor/static/dashboard-next/collections/mutations.ts
index cd72d9afa6..dc3a022da9 100644
--- a/saleor/static/dashboard-next/collections/mutations.ts
+++ b/saleor/static/dashboard-next/collections/mutations.ts
@@ -14,6 +14,10 @@ import {
CollectionUpdate,
CollectionUpdateVariables
} from "./types/CollectionUpdate";
+import {
+ CreateCollection,
+ CreateCollectionVariables
+} from "./types/CreateCollection";
const collectionUpdate = gql`
${collectionDetailsFragment}
@@ -68,3 +72,22 @@ export const TypedCollectionAssignProductMutation = TypedMutation<
CollectionAssignProduct,
CollectionAssignProductVariables
>(assignCollectionProduct);
+
+const createCollection = gql`
+ ${collectionDetailsFragment}
+ mutation CreateCollection($input: CollectionInput!) {
+ collectionCreate(input: $input) {
+ errors {
+ field
+ message
+ }
+ collection {
+ ...CollectionDetailsFragment
+ }
+ }
+ }
+`;
+export const TypedCollectionCreateMutation = TypedMutation<
+ CreateCollection,
+ CreateCollectionVariables
+>(createCollection);
diff --git a/saleor/static/dashboard-next/collections/types/CreateCollection.ts b/saleor/static/dashboard-next/collections/types/CreateCollection.ts
new file mode 100644
index 0000000000..c240e2c5e5
--- /dev/null
+++ b/saleor/static/dashboard-next/collections/types/CreateCollection.ts
@@ -0,0 +1,43 @@
+/* tslint:disable */
+// This file was automatically generated and should not be edited.
+
+import { CollectionInput } from "./../../types/globalTypes";
+
+// ====================================================
+// GraphQL mutation operation: CreateCollection
+// ====================================================
+
+export interface CreateCollection_collectionCreate_errors {
+ __typename: "Error";
+ field: string | null;
+ message: string | null;
+}
+
+export interface CreateCollection_collectionCreate_collection_backgroundImage {
+ __typename: "Image";
+ url: string;
+}
+
+export interface CreateCollection_collectionCreate_collection {
+ __typename: "Collection";
+ id: string;
+ isPublished: boolean;
+ name: string;
+ backgroundImage: CreateCollection_collectionCreate_collection_backgroundImage | null;
+ seoDescription: string | null;
+ seoTitle: string | null;
+}
+
+export interface CreateCollection_collectionCreate {
+ __typename: "CollectionCreate";
+ errors: (CreateCollection_collectionCreate_errors | null)[] | null;
+ collection: CreateCollection_collectionCreate_collection | null;
+}
+
+export interface CreateCollection {
+ collectionCreate: CreateCollection_collectionCreate | null;
+}
+
+export interface CreateCollectionVariables {
+ input: CollectionInput;
+}
diff --git a/saleor/static/dashboard-next/collections/views/CollectionCreate.tsx b/saleor/static/dashboard-next/collections/views/CollectionCreate.tsx
new file mode 100644
index 0000000000..52986fdb91
--- /dev/null
+++ b/saleor/static/dashboard-next/collections/views/CollectionCreate.tsx
@@ -0,0 +1,69 @@
+import * as React from "react";
+
+import Messages from "../../components/messages";
+import Navigator from "../../components/Navigator";
+import i18n from "../../i18n";
+import { maybe } from "../../misc";
+import CollectionCreatePage from "../components/CollectionCreatePage/CollectionCreatePage";
+import { TypedCollectionCreateMutation } from "../mutations";
+import { CreateCollection } from "../types/CreateCollection";
+import { collectionListUrl, collectionUrl } from "../urls";
+
+export const CollectionCreate: React.StatelessComponent<{}> = () => (
+ <Messages>
+ {pushMessage => (
+ <Navigator>
+ {navigate => {
+ const handleCollectionCreateSuccess = (data: CreateCollection) => {
+ if (
+ data.collectionCreate.errors === null ||
+ data.collectionCreate.errors.length === 0
+ ) {
+ pushMessage({
+ text: i18n.t("Created collection", {
+ context: "notification"
+ })
+ });
+ navigate(
+ collectionUrl(
+ encodeURIComponent(data.collectionCreate.collection.id)
+ )
+ );
+ }
+ };
+ return (
+ <TypedCollectionCreateMutation
+ onCompleted={handleCollectionCreateSuccess}
+ >
+ {(createCollection, { data, loading }) => (
+ <CollectionCreatePage
+ errors={maybe(() => data.collectionCreate.errors, [])}
+ onBack={() => navigate(collectionListUrl)}
+ disabled={loading}
+ onSubmit={formData =>
+ createCollection({
+ variables: {
+ input: {
+ backgroundImage: formData.backgroundImage.value,
+ isPublished: formData.isPublished,
+ name: formData.name,
+ seo: {
+ description: formData.seoDescription,
+ title: formData.seoTitle
+ },
+ // FIXME: remove after closing #3142
+ slug: formData.name.toLowerCase()
+ }
+ }
+ })
+ }
+ />
+ )}
+ </TypedCollectionCreateMutation>
+ );
+ }}
+ </Navigator>
+ )}
+ </Messages>
+);
+export default CollectionCreate;
diff --git a/saleor/static/dashboard-next/storybook/stories/collections/CollectionCreatePage.tsx b/saleor/static/dashboard-next/storybook/stories/collections/CollectionCreatePage.tsx
index f9b99cd468..955173c347 100644
--- a/saleor/static/dashboard-next/storybook/stories/collections/CollectionCreatePage.tsx
+++ b/saleor/static/dashboard-next/storybook/stories/collections/CollectionCreatePage.tsx
@@ -8,6 +8,7 @@ import Decorator from "../../Decorator";
const props: CollectionCreatePageProps = {
disabled: false,
+ errors: [],
onBack: () => undefined,
onSubmit: () => undefined
};