Experimental filters: additional empty values & full support for ranges (#4028)
This commit is contained in:
parent
d4991e56c7
commit
f3a52d2e08
5 changed files with 39 additions and 7 deletions
5
.changeset/tricky-olives-jog.md
Normal file
5
.changeset/tricky-olives-jog.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
"saleor-dashboard": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Experimental filters: additional empty values & full support for ranges
|
|
@ -1,6 +1,6 @@
|
||||||
import { getDefaultByControlName } from "../controlsType";
|
import { getDefaultByControlName } from "../controlsType";
|
||||||
import { ConditionItem } from "./ConditionOptions";
|
import { ConditionItem } from "./ConditionOptions";
|
||||||
import { ConditionValue } from "./ConditionValue";
|
import { ConditionValue, isItemOptionArray, isTuple } from "./ConditionValue";
|
||||||
|
|
||||||
export class ConditionSelected {
|
export class ConditionSelected {
|
||||||
private constructor(
|
private constructor(
|
||||||
|
@ -11,7 +11,11 @@ export class ConditionSelected {
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public isEmpty() {
|
public isEmpty() {
|
||||||
return this.value === ""
|
return (
|
||||||
|
this.value === "" ||
|
||||||
|
(isItemOptionArray(this.value) && this.value.length === 0) ||
|
||||||
|
(isTuple(this.value) && this.value.includes(""))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static empty() {
|
public static empty() {
|
||||||
|
|
|
@ -92,8 +92,14 @@ export const ATTRIBUTE_INPUT_TYPE_CONDITIONS = {
|
||||||
{ type: "number", label: "greater", value: "input-3" },
|
{ type: "number", label: "greater", value: "input-3" },
|
||||||
{ type: "number.range", label: "between", value: "input-4" },
|
{ type: "number.range", label: "between", value: "input-4" },
|
||||||
],
|
],
|
||||||
DATE_TIME: [{ type: "date", label: "is", value: "input-1" }],
|
DATE_TIME: [
|
||||||
DATE: [{ type: "date", label: "is", value: "input-1" }],
|
{ type: "datetime", label: "is", value: "input-1" },
|
||||||
|
{ type: "datetime.range", label: "between", value: "input-4" },
|
||||||
|
],
|
||||||
|
DATE: [
|
||||||
|
{ type: "date", label: "is", value: "input-1" },
|
||||||
|
{ type: "date.range", label: "between", value: "input-4" },
|
||||||
|
],
|
||||||
SWATCH: [{ type: "multiselect", label: "in", value: "input-2" }],
|
SWATCH: [{ type: "multiselect", label: "in", value: "input-2" }],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,10 @@ export const CONTROL_DEFAULTS = {
|
||||||
multiselect: [] as ConditionValue,
|
multiselect: [] as ConditionValue,
|
||||||
select: "",
|
select: "",
|
||||||
combobox: "",
|
combobox: "",
|
||||||
|
date: "",
|
||||||
|
datetime: "",
|
||||||
|
"date.range": ["", ""] as [string, string],
|
||||||
|
"datetime.range": ["", ""] as [string, string],
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getDefaultByControlName = (name: string): ConditionValue =>
|
export const getDefaultByControlName = (name: string): ConditionValue =>
|
||||||
|
|
|
@ -58,13 +58,27 @@ const createStaticQueryPart = (
|
||||||
return value;
|
return value;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getRangeQueryPartByType = (value: [string, string], type: string) => {
|
||||||
|
const [lte, gte] = value;
|
||||||
|
|
||||||
|
switch (type) {
|
||||||
|
case "datetime.range":
|
||||||
|
return { dateTime: { lte, gte } };
|
||||||
|
case "date.range":
|
||||||
|
return { date: { lte, gte } };
|
||||||
|
case "number.range":
|
||||||
|
default:
|
||||||
|
return { valuesRange: { lte: parseFloat(lte), gte: parseFloat(gte) } };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const createAttributeQueryPart = (
|
const createAttributeQueryPart = (
|
||||||
attributeSlug: string,
|
attributeSlug: string,
|
||||||
selected: ConditionSelected,
|
selected: ConditionSelected,
|
||||||
): AttributeInput => {
|
): AttributeInput => {
|
||||||
if (!selected.conditionValue) return { slug: attributeSlug };
|
if (!selected.conditionValue) return { slug: attributeSlug };
|
||||||
|
|
||||||
const { label } = selected.conditionValue;
|
const { label, type } = selected.conditionValue;
|
||||||
const { value } = selected;
|
const { value } = selected;
|
||||||
|
|
||||||
if (label === "lower" && typeof value === "string") {
|
if (label === "lower" && typeof value === "string") {
|
||||||
|
@ -76,10 +90,9 @@ const createAttributeQueryPart = (
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isTuple(value) && label === "between") {
|
if (isTuple(value) && label === "between") {
|
||||||
const [lte, gte] = value;
|
|
||||||
return {
|
return {
|
||||||
slug: attributeSlug,
|
slug: attributeSlug,
|
||||||
valuesRange: { lte: parseFloat(lte), gte: parseFloat(gte) },
|
...getRangeQueryPartByType(value, type),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue