From 4c439762707081a11ef1ae06550cbdfed9f55f78 Mon Sep 17 00:00:00 2001 From: Patryk Andrzejewski Date: Thu, 3 Aug 2023 09:12:16 +0200 Subject: [PATCH] Remove constraint for given filter when there is no depepdent element (#4052) * Remove constraint when there is no dependency * Remove constraint when there is no dependency * Remove constraint when there is no dependency * Remove constraint when there is no dependency * Remove constraint when there is no dependency --- .changeset/nine-bugs-share.md | 5 + .../TokenArray/TokenArray.test.ts | 49 +++++++ .../__snapshots__/TokenArray.test.ts.snap | 134 ++++++++++++++++++ .../ValueProvider/TokenArray/index.ts | 6 + 4 files changed, 194 insertions(+) create mode 100644 .changeset/nine-bugs-share.md create mode 100644 src/components/ConditionalFilter/ValueProvider/TokenArray/__snapshots__/TokenArray.test.ts.snap diff --git a/.changeset/nine-bugs-share.md b/.changeset/nine-bugs-share.md new file mode 100644 index 000000000..4e4de7c0d --- /dev/null +++ b/.changeset/nine-bugs-share.md @@ -0,0 +1,5 @@ +--- +"saleor-dashboard": patch +--- + +Remove constraint for given filter when there is no depepdent element diff --git a/src/components/ConditionalFilter/ValueProvider/TokenArray/TokenArray.test.ts b/src/components/ConditionalFilter/ValueProvider/TokenArray/TokenArray.test.ts index 8ecaed833..c1ba8e4ef 100644 --- a/src/components/ConditionalFilter/ValueProvider/TokenArray/TokenArray.test.ts +++ b/src/components/ConditionalFilter/ValueProvider/TokenArray/TokenArray.test.ts @@ -1,3 +1,4 @@ +import { InitialStateResponse } from "../../API/InitialStateResponse"; import { TokenArray } from "."; describe("ConditionalFilter / ValueProvider / TokenArray", () => { @@ -46,4 +47,52 @@ describe("ConditionalFilter / ValueProvider / TokenArray", () => { productType: ["beer"], }); }); + + it("should create filter container from a given response", () => { + // Arrange + const params = new URLSearchParams({ + "0[s0.price]": "123", + "1": "AND", + "2[s0.channel]": "channel-pln", + }); + const response = new InitialStateResponse([{ + label: "Cat1", + value: "cat-1-id", + slug: "cat1" + }], {}, [{ + label: "Channel PLN", + value: "channel-pln", + slug: "channel-pln" + }]) + + // Act + const tokenArray = new TokenArray(params.toString()); + const container = tokenArray.asFilterValuesFromResponse(response) + + // Assert + expect(container).toMatchSnapshot() + }) + + it("creates filter container with removing constrain if there is no depepdent rows", () => { + // Arrange + const params = new URLSearchParams({ + "0[s0.channel]": "channel-pln", + }); + const response = new InitialStateResponse([{ + label: "Cat1", + value: "cat-1-id", + slug: "cat1" + }], {}, [{ + label: "Channel PLN", + value: "channel-pln", + slug: "channel-pln" + }]) + + // Act + const tokenArray = new TokenArray(params.toString()); + const container = tokenArray.asFilterValuesFromResponse(response) + + // Assert + expect(container).toMatchSnapshot() + }) }); diff --git a/src/components/ConditionalFilter/ValueProvider/TokenArray/__snapshots__/TokenArray.test.ts.snap b/src/components/ConditionalFilter/ValueProvider/TokenArray/__snapshots__/TokenArray.test.ts.snap new file mode 100644 index 000000000..75a346f83 --- /dev/null +++ b/src/components/ConditionalFilter/ValueProvider/TokenArray/__snapshots__/TokenArray.test.ts.snap @@ -0,0 +1,134 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`ConditionalFilter / ValueProvider / TokenArray creates filter container with removing constrain if there is no depepdent rows 1`] = ` +TokenArray [ + FilterElement { + "condition": Condition { + "loading": false, + "options": ConditionOptions [ + Object { + "label": "is", + "type": "select", + "value": "input-5", + }, + ], + "selected": ConditionSelected { + "conditionValue": Object { + "label": "is", + "type": "select", + "value": "input-5", + }, + "loading": false, + "options": Array [], + "value": Object { + "label": "Channel PLN", + "slug": "channel-pln", + "value": "channel-pln", + }, + }, + }, + "constraint": undefined, + "loading": false, + "value": ExpressionValue { + "label": "Channel", + "type": "channel", + "value": "channel", + }, + }, +] +`; + +exports[`ConditionalFilter / ValueProvider / TokenArray should create filter container from a given response 1`] = ` +TokenArray [ + FilterElement { + "condition": Condition { + "loading": false, + "options": ConditionOptions [ + Object { + "label": "is", + "type": "number", + "value": "input-1", + }, + Object { + "label": "lower", + "type": "number", + "value": "input-2", + }, + Object { + "label": "greater", + "type": "number", + "value": "input-3", + }, + Object { + "label": "between", + "type": "number.range", + "value": "input-4", + }, + ], + "selected": ConditionSelected { + "conditionValue": Object { + "label": "is", + "type": "number", + "value": "input-1", + }, + "loading": false, + "options": Array [], + "value": "123", + }, + }, + "constraint": undefined, + "loading": false, + "value": ExpressionValue { + "label": "Price", + "type": "price", + "value": "price", + }, + }, + "AND", + FilterElement { + "condition": Condition { + "loading": false, + "options": ConditionOptions [ + Object { + "label": "is", + "type": "select", + "value": "input-5", + }, + ], + "selected": ConditionSelected { + "conditionValue": Object { + "label": "is", + "type": "select", + "value": "input-5", + }, + "loading": false, + "options": Array [], + "value": Object { + "label": "Channel PLN", + "slug": "channel-pln", + "value": "channel-pln", + }, + }, + }, + "constraint": Constraint { + "dependsOn": Array [ + "price", + "isVisibleInListing", + "isAvailable", + "isPublished", + ], + "disabled": Array [ + "left", + "condition", + ], + "removable": false, + }, + "loading": false, + "value": ExpressionValue { + "label": "Channel", + "type": "channel", + "value": "channel", + }, + }, +] +`; diff --git a/src/components/ConditionalFilter/ValueProvider/TokenArray/index.ts b/src/components/ConditionalFilter/ValueProvider/TokenArray/index.ts index eb01b6685..3d061545c 100644 --- a/src/components/ConditionalFilter/ValueProvider/TokenArray/index.ts +++ b/src/components/ConditionalFilter/ValueProvider/TokenArray/index.ts @@ -87,6 +87,12 @@ export class TokenArray extends Array { } return FilterElement.fromUrlToken(el, response); + }).map((element, _, container) => { + if (FilterElement.isCompatible(element) && !element.constraint?.existIn(container)) { + element.clearConstraint() + } + + return element }); } }