Skip to content

Commit

Permalink
Fix handle assigning product to collection when no products (#5302)
Browse files Browse the repository at this point in the history
* Fix assign product to collection

* Add changeset
  • Loading branch information
poulch authored Dec 5, 2024
1 parent bf001d7 commit d5a6731
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/violet-tigers-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"saleor-dashboard": patch
---

Assigning product to collection no more cause error
38 changes: 38 additions & 0 deletions src/collections/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { SearchProductsQuery } from "@dashboard/graphql";

import { getProductsFromSearchResults } from "./utils";

describe("getProductsFromSearchResults", () => {
it("should return empty array when searchResults is undefined", () => {
// Arrange
const searchResults = undefined;

// Act
const result = getProductsFromSearchResults(searchResults);

// Assert
expect(result).toEqual([]);
});

it("should return products from search results", () => {
// Arrange
const searchResults = {
search: {
edges: [
{
node: { id: 1 },
},
{
node: { id: 2 },
},
],
},
} as unknown as SearchProductsQuery;

// Act
const result = getProductsFromSearchResults(searchResults);

// Assert
expect(result).toEqual([{ id: 1 }, { id: 2 }]);
});
});
9 changes: 9 additions & 0 deletions src/collections/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ChannelCollectionData } from "@dashboard/channels/utils";
import { CollectionDetailsQuery, SearchProductsQuery } from "@dashboard/graphql";
import { mapEdgesToItems } from "@dashboard/utils/maps";

export const createChannelsChangeHandler =
(
Expand Down Expand Up @@ -36,3 +37,11 @@ export const getAssignedProductIdsToCollection = (
.map(e => ({ [e.node.id]: true }))
.reduce((p, c) => ({ ...p, ...c }), {});
};

export const getProductsFromSearchResults = (searchResults: SearchProductsQuery | undefined) => {
if (!searchResults?.search) {
return [];
}

return mapEdgesToItems(searchResults.search)?.filter(suggestedProduct => suggestedProduct.id);
};
7 changes: 2 additions & 5 deletions src/collections/views/CollectionDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import useProductSearch from "@dashboard/searches/useProductSearch";
import { arrayDiff } from "@dashboard/utils/arrays";
import createDialogActionHandlers from "@dashboard/utils/handlers/dialogActionHandlers";
import createMetadataUpdateHandler from "@dashboard/utils/handlers/metadataUpdateHandler";
import { mapEdgesToItems } from "@dashboard/utils/maps";
import { getParsedDataForJsonStringField } from "@dashboard/utils/richText/misc";
import React from "react";
import { FormattedMessage, useIntl } from "react-intl";
Expand All @@ -47,7 +46,7 @@ import {
CollectionUrlDialog,
CollectionUrlQueryParams,
} from "../urls";
import { getAssignedProductIdsToCollection } from "../utils";
import { getAssignedProductIdsToCollection, getProductsFromSearchResults } from "../utils";
import { COLLECTION_DETAILS_FORM_ID } from "./consts";

interface CollectionDetailsProps {
Expand Down Expand Up @@ -342,9 +341,7 @@ export const CollectionDetails: React.FC<CollectionDetailsProps> = ({ id, params
loading={result.loading}
onClose={closeModal}
onSubmit={handleAssignationChange}
products={mapEdgesToItems(result?.data?.search)?.filter(
suggestedProduct => suggestedProduct.id,
)}
products={getProductsFromSearchResults(result?.data)}
/>

<ActionDialog
Expand Down

0 comments on commit d5a6731

Please sign in to comment.