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 handle assigning product to collection when no products #5302

Merged
merged 3 commits into from
Dec 5, 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/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
Loading