Skip to content

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

Add product assigning

dominik-zeglen2bd47b01a67d
diff --git a/saleor/static/dashboard-next/discounts/index.tsx b/saleor/static/dashboard-next/discounts/index.tsx
index 976f452567..585f4c7212 100644
--- a/saleor/static/dashboard-next/discounts/index.tsx
+++ b/saleor/static/dashboard-next/discounts/index.tsx
@@ -53,7 +53,7 @@ export const DiscountSection: React.StatelessComponent<{}> = () => (
<WindowTitle title={i18n.t("Discounts")} />
<Switch>
<Route exact path={saleListPath} component={SaleListView} />
- <Route exact path={salePath(":id")} component={SaleDetailsView} />
+ <Route path={salePath(":id")} component={SaleDetailsView} />
<Route exact path={voucherListPath} component={VoucherListView} />
</Switch>
</>
diff --git a/saleor/static/dashboard-next/discounts/mutations.ts b/saleor/static/dashboard-next/discounts/mutations.ts
index 97e93aca8d..94836b7f6e 100644
--- a/saleor/static/dashboard-next/discounts/mutations.ts
+++ b/saleor/static/dashboard-next/discounts/mutations.ts
@@ -1,7 +1,11 @@
import gql from "graphql-tag";
import { TypedMutation } from "../mutations";
-import { saleFragment } from "./queries";
+import { saleDetailsFragment, saleFragment } from "./queries";
+import {
+ SaleCataloguesAdd,
+ SaleCataloguesAddVariables
+} from "./types/SaleCataloguesAdd";
import { SaleUpdate, SaleUpdateVariables } from "./types/SaleUpdate";
const saleUpdate = gql`
@@ -21,3 +25,29 @@ const saleUpdate = gql`
export const TypedSaleUpdate = TypedMutation<SaleUpdate, SaleUpdateVariables>(
saleUpdate
);
+
+const saleCataloguesAdd = gql`
+ ${saleDetailsFragment}
+ mutation SaleCataloguesAdd(
+ $input: CatalogueInput!
+ $id: ID!
+ $after: String
+ $before: String
+ $first: Int
+ $last: Int
+ ) {
+ saleCataloguesAdd(id: $id, input: $input) {
+ errors {
+ field
+ message
+ }
+ sale {
+ ...SaleDetailsFragment
+ }
+ }
+ }
+`;
+export const TypedSaleCataloguesAdd = TypedMutation<
+ SaleCataloguesAdd,
+ SaleCataloguesAddVariables
+>(saleCataloguesAdd);
diff --git a/saleor/static/dashboard-next/discounts/types/SaleCataloguesAdd.ts b/saleor/static/dashboard-next/discounts/types/SaleCataloguesAdd.ts
new file mode 100644
index 0000000000..515d5939cc
--- /dev/null
+++ b/saleor/static/dashboard-next/discounts/types/SaleCataloguesAdd.ts
@@ -0,0 +1,147 @@
+/* tslint:disable */
+// This file was automatically generated and should not be edited.
+
+import { CatalogueInput, SaleType } from "./../../types/globalTypes";
+
+// ====================================================
+// GraphQL mutation operation: SaleCataloguesAdd
+// ====================================================
+
+export interface SaleCataloguesAdd_saleCataloguesAdd_errors {
+ __typename: "Error";
+ field: string | null;
+ message: string | null;
+}
+
+export interface SaleCataloguesAdd_saleCataloguesAdd_sale_products_edges_node_productType {
+ __typename: "ProductType";
+ id: string;
+ name: string;
+}
+
+export interface SaleCataloguesAdd_saleCataloguesAdd_sale_products_edges_node_thumbnail {
+ __typename: "Image";
+ url: string;
+}
+
+export interface SaleCataloguesAdd_saleCataloguesAdd_sale_products_edges_node {
+ __typename: "Product";
+ id: string;
+ name: string;
+ isPublished: boolean;
+ productType: SaleCataloguesAdd_saleCataloguesAdd_sale_products_edges_node_productType;
+ thumbnail: SaleCataloguesAdd_saleCataloguesAdd_sale_products_edges_node_thumbnail | null;
+}
+
+export interface SaleCataloguesAdd_saleCataloguesAdd_sale_products_edges {
+ __typename: "ProductCountableEdge";
+ node: SaleCataloguesAdd_saleCataloguesAdd_sale_products_edges_node;
+}
+
+export interface SaleCataloguesAdd_saleCataloguesAdd_sale_products_pageInfo {
+ __typename: "PageInfo";
+ endCursor: string | null;
+ hasNextPage: boolean;
+ hasPreviousPage: boolean;
+ startCursor: string | null;
+}
+
+export interface SaleCataloguesAdd_saleCataloguesAdd_sale_products {
+ __typename: "ProductCountableConnection";
+ edges: SaleCataloguesAdd_saleCataloguesAdd_sale_products_edges[];
+ pageInfo: SaleCataloguesAdd_saleCataloguesAdd_sale_products_pageInfo;
+}
+
+export interface SaleCataloguesAdd_saleCataloguesAdd_sale_categories_edges_node_products {
+ __typename: "ProductCountableConnection";
+ totalCount: number | null;
+}
+
+export interface SaleCataloguesAdd_saleCataloguesAdd_sale_categories_edges_node {
+ __typename: "Category";
+ id: string;
+ name: string;
+ products: SaleCataloguesAdd_saleCataloguesAdd_sale_categories_edges_node_products | null;
+}
+
+export interface SaleCataloguesAdd_saleCataloguesAdd_sale_categories_edges {
+ __typename: "CategoryCountableEdge";
+ node: SaleCataloguesAdd_saleCataloguesAdd_sale_categories_edges_node;
+}
+
+export interface SaleCataloguesAdd_saleCataloguesAdd_sale_categories_pageInfo {
+ __typename: "PageInfo";
+ endCursor: string | null;
+ hasNextPage: boolean;
+ hasPreviousPage: boolean;
+ startCursor: string | null;
+}
+
+export interface SaleCataloguesAdd_saleCataloguesAdd_sale_categories {
+ __typename: "CategoryCountableConnection";
+ edges: SaleCataloguesAdd_saleCataloguesAdd_sale_categories_edges[];
+ pageInfo: SaleCataloguesAdd_saleCataloguesAdd_sale_categories_pageInfo;
+}
+
+export interface SaleCataloguesAdd_saleCataloguesAdd_sale_collections_edges_node_products {
+ __typename: "ProductCountableConnection";
+ totalCount: number | null;
+}
+
+export interface SaleCataloguesAdd_saleCataloguesAdd_sale_collections_edges_node {
+ __typename: "Collection";
+ id: string;
+ name: string;
+ products: SaleCataloguesAdd_saleCataloguesAdd_sale_collections_edges_node_products | null;
+}
+
+export interface SaleCataloguesAdd_saleCataloguesAdd_sale_collections_edges {
+ __typename: "CollectionCountableEdge";
+ node: SaleCataloguesAdd_saleCataloguesAdd_sale_collections_edges_node;
+}
+
+export interface SaleCataloguesAdd_saleCataloguesAdd_sale_collections_pageInfo {
+ __typename: "PageInfo";
+ endCursor: string | null;
+ hasNextPage: boolean;
+ hasPreviousPage: boolean;
+ startCursor: string | null;
+}
+
+export interface SaleCataloguesAdd_saleCataloguesAdd_sale_collections {
+ __typename: "CollectionCountableConnection";
+ edges: SaleCataloguesAdd_saleCataloguesAdd_sale_collections_edges[];
+ pageInfo: SaleCataloguesAdd_saleCataloguesAdd_sale_collections_pageInfo;
+}
+
+export interface SaleCataloguesAdd_saleCataloguesAdd_sale {
+ __typename: "Sale";
+ id: string;
+ name: string;
+ type: SaleType;
+ startDate: any;
+ endDate: any | null;
+ value: number;
+ products: SaleCataloguesAdd_saleCataloguesAdd_sale_products | null;
+ categories: SaleCataloguesAdd_saleCataloguesAdd_sale_categories | null;
+ collections: SaleCataloguesAdd_saleCataloguesAdd_sale_collections | null;
+}
+
+export interface SaleCataloguesAdd_saleCataloguesAdd {
+ __typename: "SaleAddCatalogues";
+ errors: SaleCataloguesAdd_saleCataloguesAdd_errors[] | null;
+ sale: SaleCataloguesAdd_saleCataloguesAdd_sale | null;
+}
+
+export interface SaleCataloguesAdd {
+ saleCataloguesAdd: SaleCataloguesAdd_saleCataloguesAdd | null;
+}
+
+export interface SaleCataloguesAddVariables {
+ input: CatalogueInput;
+ id: string;
+ after?: string | null;
+ before?: string | null;
+ first?: number | null;
+ last?: number | null;
+}
diff --git a/saleor/static/dashboard-next/discounts/views/SaleDetails/index.tsx b/saleor/static/dashboard-next/discounts/views/SaleDetails/index.tsx
new file mode 100644
index 0000000000..a6fc171a09
--- /dev/null
+++ b/saleor/static/dashboard-next/discounts/views/SaleDetails/index.tsx
@@ -0,0 +1,243 @@
+import { stringify as stringifyQs } from "qs";
+import * as React from "react";
+import { Route } from "react-router-dom";
+
+import { categoryUrl } from "../../../categories/urls";
+import { collectionUrl } from "../../../collections/urls";
+import AssignProductDialog from "../../../components/AssignProductDialog";
+import Messages from "../../../components/messages";
+import Navigator from "../../../components/Navigator";
+import {
+ createPaginationState,
+ Paginator
+} from "../../../components/Paginator";
+import Shop from "../../../components/Shop";
+import { WindowTitle } from "../../../components/WindowTitle";
+import { SearchProductsProvider } from "../../../containers/SearchProducts";
+import i18n from "../../../i18n";
+import { getMutationState, maybe } from "../../../misc";
+import { productUrl } from "../../../products/urls";
+import { DiscountValueTypeEnum, SaleType } from "../../../types/globalTypes";
+import SaleDetailsPage, {
+ SaleDetailsPageTab
+} from "../../components/SaleDetailsPage";
+import { TypedSaleCataloguesAdd, TypedSaleUpdate } from "../../mutations";
+import { TypedSaleDetails } from "../../queries";
+import { SaleCataloguesAdd } from "../../types/SaleCataloguesAdd";
+import { saleListUrl, saleUrl } from "../../urls";
+import { saleAssignProductsPath, saleAssignProductsUrl } from "./urls";
+
+const PAGINATE_BY = 20;
+
+export type SaleDetailsQueryParams = Partial<{
+ after: string;
+ before: string;
+ tab: SaleDetailsPageTab;
+}>;
+
+interface SaleDetailsProps {
+ id: string;
+ params: SaleDetailsQueryParams;
+}
+
+function discountValueTypeEnum(type: SaleType): DiscountValueTypeEnum {
+ return type.toString() === DiscountValueTypeEnum.FIXED
+ ? DiscountValueTypeEnum.FIXED
+ : DiscountValueTypeEnum.PERCENTAGE;
+}
+
+export const SaleDetails: React.StatelessComponent<SaleDetailsProps> = ({
+ id,
+ params
+}) => (
+ <>
+ <WindowTitle title={i18n.t("Sales")} />
+ <Shop>
+ {shop => (
+ <Messages>
+ {pushMessage => (
+ <Navigator>
+ {navigate => {
+ const paginationState = createPaginationState(
+ PAGINATE_BY,
+ params
+ );
+ const changeTab = (tab: SaleDetailsPageTab) =>
+ navigate(
+ "?" +
+ stringifyQs({
+ tab
+ })
+ );
+
+ const handleProductAssign = (data: SaleCataloguesAdd) => {
+ if (data.saleCataloguesAdd.errors.length === 0) {
+ pushMessage({
+ text: i18n.t("Added product to collection", {
+ context: "notification"
+ })
+ });
+ navigate(saleUrl(id), true);
+ }
+ };
+
+ return (
+ <TypedSaleCataloguesAdd onCompleted={handleProductAssign}>
+ {(saleCataloguesAdd, saleCataloguesAddOpts) => (
+ <TypedSaleUpdate>
+ {(saleUpdate, saleUpdateOpts) => (
+ <TypedSaleDetails
+ displayLoader
+ variables={{ id, ...paginationState }}
+ >
+ {({ data, loading }) => {
+ const pageInfo =
+ params.tab === SaleDetailsPageTab.categories
+ ? maybe(() => data.sale.categories.pageInfo)
+ : params.tab ===
+ SaleDetailsPageTab.collections
+ ? maybe(() => data.sale.collections.pageInfo)
+ : maybe(() => data.sale.products.pageInfo);
+ const formTransitionState = getMutationState(
+ saleUpdateOpts.called,
+ saleUpdateOpts.loading,
+ maybe(
+ () => saleUpdateOpts.data.saleUpdate.errors
+ )
+ );
+ const assignTransitionState = getMutationState(
+ saleCataloguesAddOpts.called,
+ saleCataloguesAddOpts.loading,
+ maybe(
+ () =>
+ saleCataloguesAddOpts.data.saleCataloguesAdd
+ .errors
+ )
+ );
+
+ return (
+ <>
+ <Paginator
+ pageInfo={pageInfo}
+ paginationState={paginationState}
+ queryString={params}
+ >
+ {({
+ loadNextPage,
+ loadPreviousPage,
+ pageInfo
+ }) => (
+ <SaleDetailsPage
+ defaultCurrency={maybe(
+ () => shop.defaultCurrency
+ )}
+ sale={maybe(() => data.sale)}
+ disabled={loading}
+ pageInfo={pageInfo}
+ onNextPage={loadNextPage}
+ onPreviousPage={loadPreviousPage}
+ onCategoryClick={id => () =>
+ navigate(categoryUrl(id))}
+ onCollectionClick={id => () =>
+ navigate(collectionUrl(id))}
+ onProductAssign={() =>
+ navigate(
+ saleAssignProductsUrl(id),
+ false,
+ true
+ )
+ }
+ onProductClick={id => () =>
+ navigate(productUrl(id))}
+ activeTab={params.tab}
+ onBack={() => navigate(saleListUrl)}
+ onTabClick={changeTab}
+ onSubmit={formData =>
+ saleUpdate({
+ variables: {
+ id,
+ input: {
+ endDate:
+ formData.endDate === ""
+ ? null
+ : new Date(
+ formData.endDate
+ ),
+ name: formData.name,
+ startDate: new Date(
+ formData.startDate
+ ),
+ type: discountValueTypeEnum(
+ formData.type
+ ),
+ value: formData.value
+ }
+ }
+ })
+ }
+ saveButtonBarState={formTransitionState}
+ />
+ )}
+ </Paginator>
+ <Route
+ path={saleAssignProductsPath(":id")}
+ render={({ match }) => (
+ <SearchProductsProvider>
+ {(
+ searchProducts,
+ searchProductsOpts
+ ) => (
+ <AssignProductDialog
+ confirmButtonState={
+ assignTransitionState
+ }
+ open={!!match}
+ onFetch={searchProducts}
+ loading={searchProductsOpts.loading}
+ onClose={() =>
+ navigate(saleUrl(id), true, true)
+ }
+ onSubmit={formData =>
+ saleCataloguesAdd({
+ variables: {
+ ...paginationState,
+ id,
+ input: {
+ products: formData.products.map(
+ product => product.id
+ )
+ }
+ }
+ })
+ }
+ products={maybe(() =>
+ searchProductsOpts.data.products.edges
+ .map(edge => edge.node)
+ .filter(
+ suggestedProduct =>
+ suggestedProduct.id
+ )
+ )}
+ />
+ )}
+ </SearchProductsProvider>
+ )}
+ />
+ </>
+ );
+ }}
+ </TypedSaleDetails>
+ )}
+ </TypedSaleUpdate>
+ )}
+ </TypedSaleCataloguesAdd>
+ );
+ }}
+ </Navigator>
+ )}
+ </Messages>
+ )}
+ </Shop>
+ </>
+);
+export default SaleDetails;
diff --git a/saleor/static/dashboard-next/discounts/views/SaleDetails/urls.ts b/saleor/static/dashboard-next/discounts/views/SaleDetails/urls.ts
new file mode 100644
index 0000000000..251db32b7d
--- /dev/null
+++ b/saleor/static/dashboard-next/discounts/views/SaleDetails/urls.ts
@@ -0,0 +1,7 @@
+import * as urlJoin from "url-join";
+import { salePath } from "../../urls";
+
+export const saleAssignProductsPath = (id: string) =>
+ urlJoin(salePath(id), "assign-product");
+export const saleAssignProductsUrl = (id: string) =>
+ saleAssignProductsPath(encodeURIComponent(id));