Skip to content

Commit

Permalink
Fix product data export input issue (#4793)
Browse files Browse the repository at this point in the history
* map export input based on input type

* changesets

* small refactor

* add simple test

---------

Co-authored-by: Paweł Chyła <chyla1988@gmail.com>
  • Loading branch information
Cloud11PL and poulch committed Apr 23, 2024
1 parent 0365687 commit b233322
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/sweet-baboons-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"saleor-dashboard": patch
---

Fixes an issue where product export threw an error due to invalid input data
19 changes: 11 additions & 8 deletions src/products/views/ProductList/ProductList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import React, { useCallback, useEffect, useState } from "react";
import { FormattedMessage, useIntl } from "react-intl";

import ProductListPage from "../../components/ProductListPage";
import { ProductsExportParameters } from "./export";
import {
getFilterOpts,
getFilterQueryParam,
Expand Down Expand Up @@ -506,17 +507,19 @@ export const ProductList: React.FC<ProductListProps> = ({ params }) => {
warehouses={mapEdgesToItems(warehouses?.data?.warehouses) || []}
channels={availableChannels}
onClose={closeModal}
onSubmit={data =>
onSubmit={data => {
const productsExportParams = new ProductsExportParameters({
...data,
...filterVariables,
ids: selectedRowIds,
});

exportProducts({
variables: {
input: {
...data,
...filterVariables,
ids: selectedRowIds,
},
input: productsExportParams.asExportProductsInput(),
},
})
}
});
}}
/>
<SaveFilterTabDialog
open={params.action === "save-search"}
Expand Down
56 changes: 56 additions & 0 deletions src/products/views/ProductList/export.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {
ExportScope,
FileTypesEnum,
ProductFieldEnum,
} from "@dashboard/graphql";
import { ExportInfoInput } from "@saleor/sdk/dist/apollo/types";

import { ProductsExportParameters } from "./export";
import { getFilterVariables } from "./filters";

const exportParams = {
exportInfo: {
attributes: [],
warehouses: ["warehouse1"],
channels: ["Q2hhbm5lbDoyMjQ0"],
fields: [ProductFieldEnum.CHARGE_TAXES],
} satisfies ExportInfoInput,
fileType: FileTypesEnum.CSV,
filter: undefined,
ids: [],
scope: ExportScope.ALL,
};

const mock = {
...exportParams,
ids: [],
};

const mockWithOverflow = {
...mock,
...getFilterVariables({
isProductListingPageFiltersFlagEnabled: true,
filterContainer: [],
queryParams: {
categories: undefined,
},
isChannelSelected: false,
channelSlug: undefined,
}),
};

describe("Passing input to product export", () => {
it("should return the correct export parameters", () => {
const productExportParams = new ProductsExportParameters(mock);

expect(productExportParams.asExportProductsInput()).toStrictEqual(mock);
});
it("should return the correct export parameters despite overflow of data", () => {
const productExportParams = new ProductsExportParameters(mockWithOverflow);

expect(productExportParams.asExportProductsInput()).toStrictEqual(mock);
expect(productExportParams.asExportProductsInput()).not.toHaveProperty(
"where",
);
});
});
41 changes: 41 additions & 0 deletions src/products/views/ProductList/export.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {
ExportInfoInput,
ExportProductsInput as ExportProductsInputType,
ExportScope,
FileTypesEnum,
InputMaybe,
ProductFilterInput,
Scalars,
} from "@dashboard/graphql";

export class ProductsExportParameters {
private readonly exportInfo?: InputMaybe<ExportInfoInput>;
private readonly fileType: FileTypesEnum;
private readonly filter?: InputMaybe<ProductFilterInput>;
private readonly ids?: InputMaybe<Array<Scalars["ID"]>>;
private readonly scope: ExportScope;

constructor({
exportInfo,
fileType,
filter,
ids,
scope,
}: ExportProductsInputType) {
this.exportInfo = exportInfo;
this.fileType = fileType;
this.filter = filter;
this.ids = ids;
this.scope = scope;
}

asExportProductsInput() {
return {
exportInfo: this.exportInfo,
fileType: this.fileType,
filter: this.filter,
ids: this.ids,
scope: this.scope,
} satisfies ExportProductsInputType;
}
}

0 comments on commit b233322

Please sign in to comment.