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
This commit is contained in:
Patryk Andrzejewski 2023-08-03 09:12:16 +02:00 committed by GitHub
parent 504cfaac6e
commit 4c43976270
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 194 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
"saleor-dashboard": patch
---
Remove constraint for given filter when there is no depepdent element

View file

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

View file

@ -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",
},
},
]
`;

View file

@ -87,6 +87,12 @@ export class TokenArray extends Array<string | UrlToken | TokenArray> {
}
return FilterElement.fromUrlToken(el, response);
}).map((element, _, container) => {
if (FilterElement.isCompatible(element) && !element.constraint?.existIn(container)) {
element.clearConstraint()
}
return element
});
}
}