Fix throw error when no description data in product list datagrid (#3946)

This commit is contained in:
Paweł Chyła 2023-07-18 14:46:31 +02:00 committed by GitHub
parent 5e6794fa99
commit 9a3c9de817
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 13 deletions

View file

@ -0,0 +1,5 @@
---
"saleor-dashboard": patch
---
Fix throw error when there is not description data in product list datagrid

View file

@ -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 &nbsp", () => {
expect(
getDescriptionValue(
'{"time": 1637142885936, "blocks": [{"data": {"text": " "}, "type": "paragraph"}], "version": "2.20.0"}',
),
).toBe("");
});
});

View file

@ -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(":");