Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deletion order drafts #4620

Merged
merged 4 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/green-buses-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"saleor-dashboard": patch
---

Fix deleting draft orders
37 changes: 9 additions & 28 deletions src/orders/views/OrderDraftList/OrderDraftList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -47,6 +46,7 @@ import {
storageUtils,
} from "./filters";
import { getSortQueryVariables } from "./sort";
import { useBulkDeletion } from "./useBulkDeletion";

interface OrderDraftListProps {
params: OrderDraftListUrlQueryParams;
Expand All @@ -70,23 +70,13 @@ export const OrderDraftList: React.FC<OrderDraftListProps> = ({ 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 => {
Expand Down Expand Up @@ -168,15 +158,6 @@ export const OrderDraftList: React.FC<OrderDraftListProps> = ({ 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) {
Expand Down Expand Up @@ -241,7 +222,7 @@ export const OrderDraftList: React.FC<OrderDraftListProps> = ({ params }) => {
<ActionDialog
confirmButtonState={orderDraftBulkDeleteOpts.status}
onClose={closeModal}
onConfirm={onOrderDraftBulkDelete}
onConfirm={() => onOrderDraftBulkDelete(selectedRowIds)}
open={params.action === "remove"}
title={intl.formatMessage({
id: "qbmeUI",
Expand Down
35 changes: 35 additions & 0 deletions src/orders/views/OrderDraftList/useBulkDeletion.test.ts
Original file line number Diff line number Diff line change
@@ -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"],
},
});
});
});
34 changes: 34 additions & 0 deletions src/orders/views/OrderDraftList/useBulkDeletion.ts
Original file line number Diff line number Diff line change
@@ -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 };
};
Loading