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:
parent
504cfaac6e
commit
4c43976270
4 changed files with 194 additions and 0 deletions
5
.changeset/nine-bugs-share.md
Normal file
5
.changeset/nine-bugs-share.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
"saleor-dashboard": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Remove constraint for given filter when there is no depepdent element
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { InitialStateResponse } from "../../API/InitialStateResponse";
|
||||||
import { TokenArray } from ".";
|
import { TokenArray } from ".";
|
||||||
|
|
||||||
describe("ConditionalFilter / ValueProvider / TokenArray", () => {
|
describe("ConditionalFilter / ValueProvider / TokenArray", () => {
|
||||||
|
@ -46,4 +47,52 @@ describe("ConditionalFilter / ValueProvider / TokenArray", () => {
|
||||||
productType: ["beer"],
|
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()
|
||||||
|
})
|
||||||
});
|
});
|
||||||
|
|
|
@ -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",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]
|
||||||
|
`;
|
|
@ -87,6 +87,12 @@ export class TokenArray extends Array<string | UrlToken | TokenArray> {
|
||||||
}
|
}
|
||||||
|
|
||||||
return FilterElement.fromUrlToken(el, response);
|
return FilterElement.fromUrlToken(el, response);
|
||||||
|
}).map((element, _, container) => {
|
||||||
|
if (FilterElement.isCompatible(element) && !element.constraint?.existIn(container)) {
|
||||||
|
element.clearConstraint()
|
||||||
|
}
|
||||||
|
|
||||||
|
return element
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue