From b18c85baaab59bd3e3af7d02c971776784924277 Mon Sep 17 00:00:00 2001 From: andrzejewsky Date: Wed, 10 Jan 2024 16:00:36 +0100 Subject: [PATCH 1/3] Fix deletion order drafts --- src/components/ActionDialog/ActionDialog.tsx | 2 +- .../views/OrderDraftList/OrderDraftList.tsx | 37 +++++-------------- .../OrderDraftList/useBulkDeletion.test.ts | 35 ++++++++++++++++++ .../views/OrderDraftList/useBulkDeletion.ts | 34 +++++++++++++++++ 4 files changed, 79 insertions(+), 29 deletions(-) create mode 100644 src/orders/views/OrderDraftList/useBulkDeletion.test.ts create mode 100644 src/orders/views/OrderDraftList/useBulkDeletion.ts diff --git a/src/components/ActionDialog/ActionDialog.tsx b/src/components/ActionDialog/ActionDialog.tsx index 62cef106d4c..e3e2e3a8475 100644 --- a/src/components/ActionDialog/ActionDialog.tsx +++ b/src/components/ActionDialog/ActionDialog.tsx @@ -16,7 +16,7 @@ export interface ActionDialogProps extends DialogProps { title: string; variant?: ActionDialogVariant; backButtonText?: string; - onConfirm: () => any; + onConfirm: () => Promise; } const ActionDialog: React.FC = props => { diff --git a/src/orders/views/OrderDraftList/OrderDraftList.tsx b/src/orders/views/OrderDraftList/OrderDraftList.tsx index b704d956c78..63ab62c7573 100644 --- a/src/orders/views/OrderDraftList/OrderDraftList.tsx +++ b/src/orders/views/OrderDraftList/OrderDraftList.tsx @@ -7,7 +7,6 @@ import DeleteFilterTabDialog from "@dashboard/components/DeleteFilterTabDialog"; import SaveFilterTabDialog from "@dashboard/components/SaveFilterTabDialog"; import { useShopLimitsQuery } from "@dashboard/components/Shop/queries"; import { - useOrderDraftBulkCancelMutation, useOrderDraftCreateMutation, useOrderDraftListQuery, } from "@dashboard/graphql"; @@ -47,6 +46,7 @@ import { storageUtils, } from "./filters"; import { getSortQueryVariables } from "./sort"; +import { useBulkDeletion } from "./useBulkDeletion"; interface OrderDraftListProps { params: OrderDraftListUrlQueryParams; @@ -70,23 +70,13 @@ export const OrderDraftList: React.FC = ({ params }) => { setSelectedRowIds, } = useRowSelection(params); - const [orderDraftBulkDelete, orderDraftBulkDeleteOpts] = - useOrderDraftBulkCancelMutation({ - onCompleted: data => { - if (data.draftOrderBulkDelete.errors.length === 0) { - notify({ - status: "success", - text: intl.formatMessage({ - id: "ra2O4j", - defaultMessage: "Deleted draft orders", - }), - }); - refetch(); - clearRowSelection(); - closeModal(); - } - }, - }); + const { onOrderDraftBulkDelete, orderDraftBulkDeleteOpts } = useBulkDeletion( + () => { + refetch(); + clearRowSelection(); + closeModal(); + }, + ); const [createOrder] = useOrderDraftCreateMutation({ onCompleted: data => { @@ -168,15 +158,6 @@ export const OrderDraftList: React.FC = ({ params }) => { const handleSort = createSortHandler(navigate, orderDraftListUrl, params); - const onOrderDraftBulkDelete = useCallback(async () => { - await orderDraftBulkDelete({ - variables: { - ids: selectedRowIds, - }, - }); - clearRowSelection(); - }, []); - const handleSetSelectedOrderDraftIds = useCallback( (rows: number[], clearSelection: () => void) => { if (!orderDrafts) { @@ -241,7 +222,7 @@ export const OrderDraftList: React.FC = ({ params }) => { onOrderDraftBulkDelete(selectedRowIds)} open={params.action === "remove"} title={intl.formatMessage({ id: "qbmeUI", diff --git a/src/orders/views/OrderDraftList/useBulkDeletion.test.ts b/src/orders/views/OrderDraftList/useBulkDeletion.test.ts new file mode 100644 index 00000000000..f769b8b2ae7 --- /dev/null +++ b/src/orders/views/OrderDraftList/useBulkDeletion.test.ts @@ -0,0 +1,35 @@ +import { useOrderDraftBulkCancelMutation } from "@dashboard/graphql"; +import { renderHook } from "@testing-library/react-hooks"; + +import { useBulkDeletion } from "./useBulkDeletion"; + +jest.mock("react-intl"); +jest.mock("@dashboard/graphql", () => ({ + useOrderDraftBulkCancelMutation: jest.fn(() => [jest.fn(), {}]), +})); + +describe("Order draft list useBulkDeletion", () => { + it("deletes order drafts for by given ids", async () => { + // Arrange + const onComplete = jest.fn(); + const selectedRowIds = ["id1", "id2"]; + const orderDraftBulkDelete = jest.fn(); + const useOrderDraftBulkCancelMutationMock = + useOrderDraftBulkCancelMutation as jest.Mock; + useOrderDraftBulkCancelMutationMock.mockReturnValue([ + orderDraftBulkDelete, + {}, + ]); + + // Act + const { result } = renderHook(() => useBulkDeletion(onComplete)); + await result.current.onOrderDraftBulkDelete(selectedRowIds); + + // Assert + expect(orderDraftBulkDelete).toBeCalledWith({ + variables: { + ids: ["id1", "id2"], + }, + }); + }); +}); diff --git a/src/orders/views/OrderDraftList/useBulkDeletion.ts b/src/orders/views/OrderDraftList/useBulkDeletion.ts new file mode 100644 index 00000000000..a3e95b0842e --- /dev/null +++ b/src/orders/views/OrderDraftList/useBulkDeletion.ts @@ -0,0 +1,34 @@ +import { useOrderDraftBulkCancelMutation } from "@dashboard/graphql"; +import useNotifier from "@dashboard/hooks/useNotifier"; +import { useIntl } from "react-intl"; + +export const useBulkDeletion = (onComplete: () => void) => { + const notify = useNotifier(); + const intl = useIntl(); + const [orderDraftBulkDelete, orderDraftBulkDeleteOpts] = + useOrderDraftBulkCancelMutation({ + onCompleted: data => { + if (data.draftOrderBulkDelete.errors.length === 0) { + notify({ + status: "success", + text: intl.formatMessage({ + id: "ra2O4j", + defaultMessage: "Deleted draft orders", + }), + }); + + onComplete(); + } + }, + }); + + const onOrderDraftBulkDelete = async (selectedRowIds: string[]) => { + await orderDraftBulkDelete({ + variables: { + ids: selectedRowIds, + }, + }); + }; + + return { onOrderDraftBulkDelete, orderDraftBulkDeleteOpts }; +}; From c1e225aa53d6694081678d6670cfafa1dbe88d96 Mon Sep 17 00:00:00 2001 From: andrzejewsky Date: Wed, 10 Jan 2024 16:04:01 +0100 Subject: [PATCH 2/3] Fix deletion order drafts --- src/components/ActionDialog/ActionDialog.tsx | 2 +- src/orders/views/OrderDraftList/useBulkDeletion.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/ActionDialog/ActionDialog.tsx b/src/components/ActionDialog/ActionDialog.tsx index e3e2e3a8475..62cef106d4c 100644 --- a/src/components/ActionDialog/ActionDialog.tsx +++ b/src/components/ActionDialog/ActionDialog.tsx @@ -16,7 +16,7 @@ export interface ActionDialogProps extends DialogProps { title: string; variant?: ActionDialogVariant; backButtonText?: string; - onConfirm: () => Promise; + onConfirm: () => any; } const ActionDialog: React.FC = props => { diff --git a/src/orders/views/OrderDraftList/useBulkDeletion.ts b/src/orders/views/OrderDraftList/useBulkDeletion.ts index a3e95b0842e..0f5d931efae 100644 --- a/src/orders/views/OrderDraftList/useBulkDeletion.ts +++ b/src/orders/views/OrderDraftList/useBulkDeletion.ts @@ -8,7 +8,7 @@ export const useBulkDeletion = (onComplete: () => void) => { const [orderDraftBulkDelete, orderDraftBulkDeleteOpts] = useOrderDraftBulkCancelMutation({ onCompleted: data => { - if (data.draftOrderBulkDelete.errors.length === 0) { + if (data?.draftOrderBulkDelete?.errors.length === 0) { notify({ status: "success", text: intl.formatMessage({ From 8920df23ae4499acc2b247d8debe4cdfa1bbdf87 Mon Sep 17 00:00:00 2001 From: andrzejewsky Date: Wed, 10 Jan 2024 16:06:18 +0100 Subject: [PATCH 3/3] Fix deletion order drafts --- .changeset/green-buses-flash.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/green-buses-flash.md diff --git a/.changeset/green-buses-flash.md b/.changeset/green-buses-flash.md new file mode 100644 index 00000000000..59a3fe2ffb8 --- /dev/null +++ b/.changeset/green-buses-flash.md @@ -0,0 +1,5 @@ +--- +"saleor-dashboard": patch +--- + +Fix deleting draft orders