Skip to content

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

Give user an ability to remove sale

dominik-zeglenf7d398a7a6e3
diff --git a/saleor/static/dashboard-next/discounts/mutations.ts b/saleor/static/dashboard-next/discounts/mutations.ts
index ce5241b73f..9ad2a740d2 100644
--- a/saleor/static/dashboard-next/discounts/mutations.ts
+++ b/saleor/static/dashboard-next/discounts/mutations.ts
@@ -6,11 +6,12 @@ import {
SaleCataloguesAdd,
SaleCataloguesAddVariables
} from "./types/SaleCataloguesAdd";
-import { SaleCreate, SaleCreateVariables } from "./types/SaleCreate";
import {
SaleCataloguesRemove,
SaleCataloguesRemoveVariables
} from "./types/SaleCataloguesRemove";
+import { SaleCreate, SaleCreateVariables } from "./types/SaleCreate";
+import { SaleDelete, SaleDeleteVariables } from "./types/SaleDelete";
import { SaleUpdate, SaleUpdateVariables } from "./types/SaleUpdate";
const saleUpdate = gql`
@@ -100,3 +101,17 @@ const saleCreate = gql`
export const TypedSaleCreate = TypedMutation<SaleCreate, SaleCreateVariables>(
saleCreate
);
+
+const saleDelete = gql`
+ mutation SaleDelete($id: ID!) {
+ saleDelete(id: $id) {
+ errors {
+ field
+ message
+ }
+ }
+ }
+`;
+export const TypedSaleDelete = TypedMutation<SaleDelete, SaleDeleteVariables>(
+ saleDelete
+);
diff --git a/saleor/static/dashboard-next/discounts/types/SaleDelete.ts b/saleor/static/dashboard-next/discounts/types/SaleDelete.ts
new file mode 100644
index 0000000000..d56afff352
--- /dev/null
+++ b/saleor/static/dashboard-next/discounts/types/SaleDelete.ts
@@ -0,0 +1,25 @@
+/* tslint:disable */
+// This file was automatically generated and should not be edited.
+
+// ====================================================
+// GraphQL mutation operation: SaleDelete
+// ====================================================
+
+export interface SaleDelete_saleDelete_errors {
+ __typename: "Error";
+ field: string | null;
+ message: string | null;
+}
+
+export interface SaleDelete_saleDelete {
+ __typename: "SaleDelete";
+ errors: SaleDelete_saleDelete_errors[] | null;
+}
+
+export interface SaleDelete {
+ saleDelete: SaleDelete_saleDelete | null;
+}
+
+export interface SaleDeleteVariables {
+ id: string;
+}
diff --git a/saleor/static/dashboard-next/discounts/views/SaleDetails/index.tsx b/saleor/static/dashboard-next/discounts/views/SaleDetails/index.tsx
index aded8428a7..ef4a42e53f 100644
--- a/saleor/static/dashboard-next/discounts/views/SaleDetails/index.tsx
+++ b/saleor/static/dashboard-next/discounts/views/SaleDetails/index.tsx
@@ -1,9 +1,11 @@
+import DialogContentText from "@material-ui/core/DialogContentText";
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 ActionDialog from "../../../components/ActionDialog";
import AssignCategoriesDialog from "../../../components/AssignCategoryDialog";
import AssignCollectionDialog from "../../../components/AssignCollectionDialog";
import AssignProductDialog from "../../../components/AssignProductDialog";
@@ -27,11 +29,12 @@ import SaleDetailsPage, {
} from "../../components/SaleDetailsPage";
import {
TypedSaleCataloguesAdd,
- TypedSaleUpdate,
- TypedSaleCataloguesRemove
+ TypedSaleCataloguesRemove,
+ TypedSaleDelete,
+ TypedSaleUpdate
} from "../../mutations";
import { TypedSaleDetails } from "../../queries";
-import { SaleCataloguesAdd } from "../../types/SaleCataloguesAdd";
+import { SaleDelete } from "../../types/SaleDelete";
import { saleListUrl, saleUrl } from "../../urls";
import {
saleAssignCategoriesPath,
@@ -39,7 +42,9 @@ import {
saleAssignCollectionsPath,
saleAssignCollectionsUrl,
saleAssignProductsPath,
- saleAssignProductsUrl
+ saleAssignProductsUrl,
+ saleDeletePath,
+ saleDeleteUrl
} from "./urls";
const PAGINATE_BY = 20;
@@ -85,332 +90,417 @@ export const SaleDetails: React.StatelessComponent<SaleDetailsProps> = ({
})
);
- const handleProductAssign = (data: SaleCataloguesAdd) => {
- if (data.saleCataloguesAdd.errors.length === 0) {
+ const handleSaleDelete = (data: SaleDelete) => {
+ if (data.saleDelete.errors.length === 0) {
pushMessage({
- text: i18n.t("Added product to collection", {
+ text: i18n.t("Removed sale", {
context: "notification"
})
});
- navigate(saleUrl(id), true);
+ navigate(saleListUrl, true);
}
};
return (
<TypedSaleCataloguesRemove>
{(saleCataloguesRemove, saleCataloguesRemoveOpts) => (
- <TypedSaleCataloguesAdd onCompleted={handleProductAssign}>
+ <TypedSaleCataloguesAdd>
{(saleCataloguesAdd, saleCataloguesAddOpts) => (
<TypedSaleUpdate>
{(saleUpdate, saleUpdateOpts) => (
- <TypedSaleDetails
- displayLoader
- variables={{ id, ...paginationState }}
- >
- {({ data, loading }) => {
- const pageInfo =
- params.tab === SaleDetailsPageTab.categories
- ? maybe(
- () => data.sale.categories.pageInfo
+ <TypedSaleDelete onCompleted={handleSaleDelete}>
+ {(saleDelete, saleDeleteOpts) => (
+ <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
)
- : params.tab ===
- SaleDetailsPageTab.collections
- ? maybe(
- () => data.sale.collections.pageInfo
+ );
+ const assignTransitionState = getMutationState(
+ saleCataloguesAddOpts.called,
+ saleCataloguesAddOpts.loading,
+ maybe(
+ () =>
+ saleCataloguesAddOpts.data
+ .saleCataloguesAdd.errors
)
- : 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
- )
- );
+ );
+ const removeTransitionState = getMutationState(
+ saleDeleteOpts.called,
+ saleDeleteOpts.loading,
+ maybe(
+ () =>
+ saleDeleteOpts.data.saleDelete
+ .errors
+ )
+ );
- return (
- <>
- <Paginator
- pageInfo={pageInfo}
- paginationState={paginationState}
- queryString={params}
- >
- {({
- loadNextPage,
- loadPreviousPage,
- pageInfo
- }) => (
- <SaleDetailsPage
- defaultCurrency={maybe(
- () => shop.defaultCurrency
- )}
- sale={maybe(() => data.sale)}
- disabled={
- loading ||
- saleCataloguesRemoveOpts.loading
- }
- errors={maybe(
- () =>
- saleUpdateOpts.data.saleUpdate
- .errors
- )}
- pageInfo={pageInfo}
- onNextPage={loadNextPage}
- onPreviousPage={loadPreviousPage}
- onCategoryAssign={() =>
- navigate(
- saleAssignCategoriesUrl(id),
- false,
- true
- )
- }
- onCategoryClick={id => () =>
- navigate(categoryUrl(id))}
- onCollectionAssign={() =>
- navigate(
- saleAssignCollectionsUrl(id),
- false,
- true
- )
- }
- onCollectionUnassign={collectionId =>
- saleCataloguesRemove({
- variables: {
- ...paginationState,
- id,
- input: {
- collections: [collectionId]
- }
- }
- })
- }
- onCategoryUnassign={categoryId =>
- saleCataloguesRemove({
- variables: {
- ...paginationState,
- id,
- input: {
- categories: [categoryId]
- }
- }
- })
- }
- onCollectionClick={id => () =>
- navigate(collectionUrl(id))}
- onProductAssign={() =>
- navigate(
- saleAssignProductsUrl(id),
- false,
- true
- )
- }
- onProductUnassign={productId =>
- saleCataloguesRemove({
- variables: {
- ...paginationState,
- id,
- input: {
- products: [productId]
- }
- }
- })
- }
- onProductClick={id => () =>
- navigate(productUrl(id))}
- activeTab={params.tab}
- onBack={() => navigate(saleListUrl)}
- onTabClick={changeTab}
- onSubmit={formData =>
- saleUpdate({
- variables: {
- id,
- input: {
- endDate:
- formData.endDate === ""
- ? null
- : formData.endDate,
- name: formData.name,
- startDate:
- formData.startDate,
- type: discountValueTypeEnum(
- formData.type
- ),
- value: formData.value
- }
+ return (
+ <Paginator
+ pageInfo={pageInfo}
+ paginationState={paginationState}
+ queryString={params}
+ >
+ {({
+ loadNextPage,
+ loadPreviousPage,
+ pageInfo
+ }) => (
+ <>
+ <SaleDetailsPage
+ defaultCurrency={maybe(
+ () => shop.defaultCurrency
+ )}
+ sale={maybe(() => data.sale)}
+ disabled={
+ loading ||
+ saleCataloguesRemoveOpts.loading
}
- })
- }
- saveButtonBarState={
- formTransitionState
- }
- />
- )}
- </Paginator>
- <Route
- path={saleAssignProductsPath(":id")}
- render={({ match }) => (
- <SearchProductsProvider>
- {(
- searchProducts,
- searchProductsOpts
- ) => (
- <AssignProductDialog
- confirmButtonState={
- assignTransitionState
+ errors={maybe(
+ () =>
+ saleUpdateOpts.data
+ .saleUpdate.errors
+ )}
+ pageInfo={pageInfo}
+ onNextPage={loadNextPage}
+ onPreviousPage={
+ loadPreviousPage
}
- open={!!match}
- onFetch={searchProducts}
- loading={
- searchProductsOpts.loading
+ onCategoryAssign={() =>
+ navigate(
+ saleAssignCategoriesUrl(id),
+ false,
+ true
+ )
}
- onClose={() =>
+ onCategoryClick={id => () =>
+ navigate(categoryUrl(id))}
+ onCollectionAssign={() =>
navigate(
- saleUrl(id),
- true,
+ saleAssignCollectionsUrl(
+ id
+ ),
+ false,
true
)
}
- onSubmit={formData =>
- saleCataloguesAdd({
+ onCollectionUnassign={collectionId =>
+ saleCataloguesRemove({
variables: {
...paginationState,
id,
input: {
- products: formData.products.map(
- product => product.id
- )
+ collections: [
+ collectionId
+ ]
}
}
})
}
- products={maybe(() =>
- searchProductsOpts.data.products.edges
- .map(edge => edge.node)
- .filter(
- suggestedProduct =>
- suggestedProduct.id
- )
- )}
- />
- )}
- </SearchProductsProvider>
- )}
- />
- <Route
- path={saleAssignCategoriesPath(":id")}
- render={({ match }) => (
- <SearchCategoriesProvider>
- {(
- searchCategories,
- searchCategoriesOpts
- ) => (
- <AssignCategoriesDialog
- categories={maybe(() =>
- searchCategoriesOpts.data.categories.edges
- .map(edge => edge.node)
- .filter(
- suggestedCategory =>
- suggestedCategory.id
- )
- )}
- confirmButtonState={
- assignTransitionState
- }
- open={!!match}
- onFetch={searchCategories}
- loading={
- searchCategoriesOpts.loading
+ onCategoryUnassign={categoryId =>
+ saleCataloguesRemove({
+ variables: {
+ ...paginationState,
+ id,
+ input: {
+ categories: [categoryId]
+ }
+ }
+ })
}
- onClose={() =>
+ onCollectionClick={id => () =>
+ navigate(collectionUrl(id))}
+ onProductAssign={() =>
navigate(
- saleUrl(id),
- true,
+ saleAssignProductsUrl(id),
+ false,
true
)
}
- onSubmit={formData =>
- saleCataloguesAdd({
+ onProductUnassign={productId =>
+ saleCataloguesRemove({
variables: {
...paginationState,
id,
input: {
- categories: formData.categories.map(
- product => product.id
- )
+ products: [productId]
}
}
})
}
- />
- )}
- </SearchCategoriesProvider>
- )}
- />
- <Route
- path={saleAssignCollectionsPath(":id")}
- render={({ match }) => (
- <SearchCollectionsProvider>
- {(
- searchCollections,
- searchCollectionsOpts
- ) => (
- <AssignCollectionDialog
- collections={maybe(() =>
- searchCollectionsOpts.data.collections.edges
- .map(edge => edge.node)
- .filter(
- suggestedCategory =>
- suggestedCategory.id
- )
- )}
- confirmButtonState={
- assignTransitionState
- }
- open={!!match}
- onFetch={searchCollections}
- loading={
- searchCollectionsOpts.loading
- }
- onClose={() =>
- navigate(
- saleUrl(id),
- true,
- true
- )
+ onProductClick={id => () =>
+ navigate(productUrl(id))}
+ activeTab={params.tab}
+ onBack={() =>
+ navigate(saleListUrl)
}
+ onTabClick={changeTab}
onSubmit={formData =>
- saleCataloguesAdd({
+ saleUpdate({
variables: {
- ...paginationState,
id,
input: {
- collections: formData.collections.map(
- product => product.id
- )
+ endDate:
+ formData.endDate ===
+ ""
+ ? null
+ : formData.endDate,
+ name: formData.name,
+ startDate:
+ formData.startDate,
+ type: discountValueTypeEnum(
+ formData.type
+ ),
+ value: formData.value
}
}
})
}
+ onRemove={() =>
+ navigate(saleDeleteUrl(id))
+ }
+ saveButtonBarState={
+ formTransitionState
+ }
+ />
+ <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>
+ )}
+ />
+ <Route
+ path={saleAssignCategoriesPath(
+ ":id"
+ )}
+ render={({ match }) => (
+ <SearchCategoriesProvider>
+ {(
+ searchCategories,
+ searchCategoriesOpts
+ ) => (
+ <AssignCategoriesDialog
+ categories={maybe(() =>
+ searchCategoriesOpts.data.categories.edges
+ .map(
+ edge => edge.node
+ )
+ .filter(
+ suggestedCategory =>
+ suggestedCategory.id
+ )
+ )}
+ confirmButtonState={
+ assignTransitionState
+ }
+ open={!!match}
+ onFetch={
+ searchCategories
+ }
+ loading={
+ searchCategoriesOpts.loading
+ }
+ onClose={() =>
+ navigate(
+ saleUrl(id),
+ true,
+ true
+ )
+ }
+ onSubmit={formData =>
+ saleCataloguesAdd({
+ variables: {
+ ...paginationState,
+ id,
+ input: {
+ categories: formData.categories.map(
+ product =>
+ product.id
+ )
+ }
+ }
+ })
+ }
+ />
+ )}
+ </SearchCategoriesProvider>
+ )}
+ />
+ <Route
+ path={saleAssignCollectionsPath(
+ ":id"
+ )}
+ render={({ match }) => (
+ <SearchCollectionsProvider>
+ {(
+ searchCollections,
+ searchCollectionsOpts
+ ) => (
+ <AssignCollectionDialog
+ collections={maybe(() =>
+ searchCollectionsOpts.data.collections.edges
+ .map(
+ edge => edge.node
+ )
+ .filter(
+ suggestedCategory =>
+ suggestedCategory.id
+ )
+ )}
+ confirmButtonState={
+ assignTransitionState
+ }
+ open={!!match}
+ onFetch={
+ searchCollections
+ }
+ loading={
+ searchCollectionsOpts.loading
+ }
+ onClose={() =>
+ navigate(
+ saleUrl(id),
+ true,
+ true
+ )
+ }
+ onSubmit={formData =>
+ saleCataloguesAdd({
+ variables: {
+ ...paginationState,
+ id,
+ input: {
+ collections: formData.collections.map(
+ product =>
+ product.id
+ )
+ }
+ }
+ })
+ }
+ />
+ )}
+ </SearchCollectionsProvider>
+ )}
+ />
+ <Route
+ path={saleDeletePath(":id")}
+ render={({ match }) => (
+ <ActionDialog
+ open={!!match}
+ title={i18n.t(
+ "Remove Sale"
+ )}
+ confirmButtonState={
+ removeTransitionState
+ }
+ onClose={() =>
+ navigate(saleUrl(id))
+ }
+ variant="delete"
+ onConfirm={() =>
+ saleDelete({
+ variables: { id }
+ })
+ }
+ >
+ <DialogContentText
+ dangerouslySetInnerHTML={{
+ __html: i18n.t(
+ "Are you sure you want to remove <strong>{{ saleName }}</strong>?",
+ {
+ saleName: maybe(
+ () =>
+ data.sale.name
+ )
+ }
+ )
+ }}
+ />
+ </ActionDialog>
+ )}
/>
- )}
- </SearchCollectionsProvider>
- )}
- />
- </>
- );
- }}
- </TypedSaleDetails>
+ </>
+ )}
+ </Paginator>
+ );
+ }}
+ </TypedSaleDetails>
+ )}
+ </TypedSaleDelete>
)}
</TypedSaleUpdate>
)}
diff --git a/saleor/static/dashboard-next/discounts/views/SaleDetails/urls.ts b/saleor/static/dashboard-next/discounts/views/SaleDetails/urls.ts
index 2beb623c50..de66e4e968 100644
--- a/saleor/static/dashboard-next/discounts/views/SaleDetails/urls.ts
+++ b/saleor/static/dashboard-next/discounts/views/SaleDetails/urls.ts
@@ -1,4 +1,5 @@
import * as urlJoin from "url-join";
+
import { salePath } from "../../urls";
export const saleAssignProductsPath = (id: string) =>
@@ -15,3 +16,7 @@ export const saleAssignCollectionsPath = (id: string) =>
urlJoin(salePath(id), "assign-collections");
export const saleAssignCollectionsUrl = (id: string) =>
saleAssignCollectionsPath(encodeURIComponent(id));
+
+export const saleDeletePath = (id: string) => urlJoin(salePath(id), "delete");
+export const saleDeleteUrl = (id: string) =>
+ saleDeletePath(encodeURIComponent(id));