Storing filter value by left operand (#3866)

* Update expression value

* Changeset

* Fix linter
This commit is contained in:
Patryk Andrzejewski 2023-07-05 14:12:16 +02:00 committed by GitHub
parent 97e4401897
commit 52bac3b8f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 24 deletions

View file

@ -0,0 +1,5 @@
---
"saleor-dashboard": patch
---
Update ExpressionValue for fitlers

View file

@ -1,16 +1,49 @@
/* eslint-disable @typescript-eslint/member-ordering */ /* eslint-disable @typescript-eslint/member-ordering */
import { InitialStateResponse } from "../API/InitialStateResponse"; import { InitialStateResponse } from "../API/InitialStateResponse";
import { LeftOperand } from "./../useLeftOperands"; import { LeftOperand } from "./../useLeftOperands";
import { UrlEntry, UrlToken } from "./../ValueProvider/UrlToken"; import { TokenType, UrlEntry, UrlToken } from "./../ValueProvider/UrlToken";
import { Condition } from "./Condition"; import { Condition } from "./Condition";
import { ConditionItem, ConditionOptions } from "./ConditionOptions"; import { ConditionItem, ConditionOptions } from "./ConditionOptions";
import { ConditionSelected } from "./ConditionSelected"; import { ConditionSelected } from "./ConditionSelected";
import { ConditionValue, ItemOption } from "./ConditionValue"; import { ConditionValue, ItemOption } from "./ConditionValue";
interface ExpressionValue {
value: string; class ExpressionValue {
label: string; constructor(
type: string; public value: string,
public label: string,
public type: string
) {}
public static fromLeftOperand(leftOperand: LeftOperand) {
return new ExpressionValue(
leftOperand.slug,
leftOperand.label,
leftOperand.type
)
}
public static fromUrlToken(token: UrlToken) {
return new ExpressionValue(
token.name,
token.name,
token.name
)
}
public static forAttribute(attributeName: string, response: InitialStateResponse) {
const attribute = response.attributeByName(attributeName);
return new ExpressionValue(
attributeName,
attribute.label,
attribute.inputType,
)
}
public static emptyStatic() {
return new ExpressionValue("", "", TokenType.STATIC)
}
} }
export class FilterElement { export class FilterElement {
@ -33,11 +66,7 @@ export class FilterElement {
} }
public updateLeftOperator(leftOperand: LeftOperand) { public updateLeftOperator(leftOperand: LeftOperand) {
this.value = { this.value = ExpressionValue.fromLeftOperand(leftOperand);
value: leftOperand.slug,
label: leftOperand.label,
type: leftOperand.type,
};
this.condition = Condition.emptyFromLeftOperand(leftOperand); this.condition = Condition.emptyFromLeftOperand(leftOperand);
} }
@ -109,7 +138,7 @@ export class FilterElement {
public static createEmpty() { public static createEmpty() {
return new FilterElement( return new FilterElement(
{ value: "", label: "", type: "s" }, ExpressionValue.emptyStatic(),
Condition.createEmpty(), Condition.createEmpty(),
false, false,
); );
@ -118,21 +147,15 @@ export class FilterElement {
public static fromUrlToken(token: UrlToken, response: InitialStateResponse) { public static fromUrlToken(token: UrlToken, response: InitialStateResponse) {
if (token.isStatic()) { if (token.isStatic()) {
return new FilterElement( return new FilterElement(
{ value: token.name, label: token.name, type: token.name }, ExpressionValue.fromUrlToken(token),
Condition.fromUrlToken(token, response), Condition.fromUrlToken(token, response),
false, false,
); );
} }
if (token.isAttribute()) { if (token.isAttribute()) {
const attribute = response.attributeByName(token.name);
return new FilterElement( return new FilterElement(
{ ExpressionValue.forAttribute(token.name, response),
value: token.name,
label: attribute.label,
type: attribute.inputType,
},
Condition.fromUrlToken(token, response), Condition.fromUrlToken(token, response),
false, false,
); );

View file

@ -53,14 +53,17 @@ export class UrlEntry {
tokenSlug: TokenTypeValue tokenSlug: TokenTypeValue
) { ) {
const { conditionValue } = condition; const { conditionValue } = condition;
const slug = slugFromConditionValue(condition.value)
if (!conditionValue) {
return new UrlEntry(tokenSlug, slug)
}
const conditionIndex = CONDITIONS.findIndex( const conditionIndex = CONDITIONS.findIndex(
el => conditionValue && el === conditionValue.label, el => el === conditionValue.label,
); );
return new UrlEntry( return new UrlEntry(`${tokenSlug}${conditionIndex}.${paramName}`, slug)
`${tokenSlug}${conditionIndex}.${paramName}`,
slugFromConditionValue(condition.value)
)
} }
} }