diff --git a/.changeset/heavy-dingos-sell.md b/.changeset/heavy-dingos-sell.md new file mode 100644 index 000000000..c0dffeb2b --- /dev/null +++ b/.changeset/heavy-dingos-sell.md @@ -0,0 +1,5 @@ +--- +"saleor-dashboard": patch +--- + +Fix throw error when there is not description data in product list datagrid diff --git a/src/products/components/ProductListDatagrid/datagrid.test.ts b/src/products/components/ProductListDatagrid/datagrid.test.ts new file mode 100644 index 000000000..67bfa7484 --- /dev/null +++ b/src/products/components/ProductListDatagrid/datagrid.test.ts @@ -0,0 +1,25 @@ +import { getDescriptionValue } from "./datagrid"; + +describe("getDescriptionValue", () => { + it("should return description value", () => { + expect( + getDescriptionValue( + '{"time": 1634014163888, "blocks": [{"data": {"text": "description"}, "type": "paragraph"}], "version": "2.20.0"}', + ), + ).toBe("description"); + }); + + it("should return empty string when no description data", () => { + expect( + getDescriptionValue('{"blocks": [{"data": {}, "type": "paragraph"}]}'), + ).toBe(""); + }); + + it("should return empty string when description contains  ", () => { + expect( + getDescriptionValue( + '{"time": 1637142885936, "blocks": [{"data": {"text": " "}, "type": "paragraph"}], "version": "2.20.0"}', + ), + ).toBe(""); + }); +}); diff --git a/src/products/components/ProductListDatagrid/datagrid.ts b/src/products/components/ProductListDatagrid/datagrid.ts index a98241b62..b715d6c47 100644 --- a/src/products/components/ProductListDatagrid/datagrid.ts +++ b/src/products/components/ProductListDatagrid/datagrid.ts @@ -277,19 +277,7 @@ function getDescriptionCellContent( return readonlyTextCell(""); } - const parsed = JSON.parse(value); - - if (parsed) { - const descriptionFirstParagraph = parsed.blocks.find( - block => block.type === "paragraph", - ); - - if (descriptionFirstParagraph) { - return readonlyTextCell(descriptionFirstParagraph.data.text); - } - } - - return readonlyTextCell(value || ""); + return readonlyTextCell(getDescriptionValue(value)); } function getNameCellContent( @@ -357,6 +345,22 @@ function getAttributeCellContent( return readonlyTextCell(""); } +export function getDescriptionValue(value: string) { + const parsed = JSON.parse(value); + + if (parsed) { + const descriptionFirstParagraph = parsed?.blocks.find( + block => block.type === "paragraph", + ); + + if (descriptionFirstParagraph) { + return (descriptionFirstParagraph.data?.text ?? "").replace(" ", ""); + } + } + + return ""; +} + export function getColumnMetadata(column: string) { if (column.includes(":")) { const [columnName, columnId] = column.split(":");