Skip to content

Commit cfb7076

Browse files
authored
[3.22] Fix filter for boolean attributes (#6523)
1 parent e8507ff commit cfb7076

4 files changed

Lines changed: 44 additions & 2 deletions

File tree

.changeset/purple-glasses-try.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"saleor-dashboard": patch
3+
---
4+
5+
Fixed filtering by attributes of type boolean

src/components/ConditionalFilter/API/Handler.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { type ApolloClient } from "@apollo/client";
2+
import { createBooleanOptions } from "@dashboard/components/ConditionalFilter/constants";
23
import {
34
_GetAttributeChoicesDocument,
45
type _GetAttributeChoicesQuery,
@@ -40,6 +41,7 @@ import {
4041
_GetWarehouseChoicesDocument,
4142
type _GetWarehouseChoicesQuery,
4243
type _GetWarehouseChoicesQueryVariables,
44+
AttributeInputTypeEnum,
4345
ChannelCurrenciesDocument,
4446
type ChannelCurrenciesQuery,
4547
type ChannelCurrenciesQueryVariables,
@@ -120,9 +122,18 @@ export class AttributeChoicesHandler implements Handler {
120122
public client: ApolloClient<unknown>,
121123
public attributeSlug: string,
122124
public query: string,
125+
public type: string,
123126
) {}
124127

125128
fetch = async () => {
129+
/**
130+
* Entity-specific handlers don't work with Boolean type because it doesn't have options declared to fetch.
131+
* It's just true/false, so statically use it for Booleans, no matter the entity type.
132+
*/
133+
if (this.type === AttributeInputTypeEnum.BOOLEAN) {
134+
return createBooleanOptions();
135+
}
136+
126137
const { client, attributeSlug, query } = this;
127138
const { data } = await client.query<
128139
_GetAttributeChoicesQuery,

src/components/ConditionalFilter/FiltersQueryBuilder/queryVarsBuilders/AttributeQueryVarsBuilder.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,32 @@ describe("AttributeQueryVarsBuilder", () => {
181181
expect(handler).toBeInstanceOf(AttributeChoicesHandler);
182182
});
183183

184+
it("should return static boolean options without API call for BOOLEAN attributes", async () => {
185+
// Arrange
186+
const mockQuery = jest.fn();
187+
const mockClient = { query: mockQuery } as unknown as ApolloClient<unknown>;
188+
const element = new FilterElement(
189+
baseElement.value,
190+
baseElement.condition,
191+
false,
192+
undefined,
193+
new ExpressionValue("bool-attr", "BoolAttr", AttributeInputTypeEnum.BOOLEAN),
194+
);
195+
const def = new AttributeQueryVarsBuilder();
196+
197+
// Act
198+
const handler = def.createOptionFetcher(mockClient, inputValue, element);
199+
const options = await handler.fetch();
200+
201+
// Assert
202+
expect(handler).toBeInstanceOf(AttributeChoicesHandler);
203+
expect(mockQuery).not.toHaveBeenCalled();
204+
expect(options).toEqual([
205+
{ label: "Yes", value: "true", slug: "true", type: undefined },
206+
{ label: "No", value: "false", slug: "false", type: undefined },
207+
]);
208+
});
209+
184210
it("should handle gracefully if attribute is not selected", () => {
185211
// Arrange
186212
const element = new FilterElement(

src/components/ConditionalFilter/FiltersQueryBuilder/queryVarsBuilders/AttributeQueryVarsBuilder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class AttributeQueryVarsBuilder
3737
inputValue: string,
3838
element: FilterElement,
3939
): Handler {
40-
const { entityType, value: id } = element.selectedAttribute || element.value;
40+
const { entityType, value: id, type } = element.selectedAttribute || element.value;
4141

4242
switch (entityType) {
4343
case AttributeEntityTypeEnum.PAGE:
@@ -51,7 +51,7 @@ export class AttributeQueryVarsBuilder
5151
case AttributeEntityTypeEnum.COLLECTION:
5252
return new CollectionHandler(client, inputValue);
5353
default:
54-
return new AttributeChoicesHandler(client, id, inputValue);
54+
return new AttributeChoicesHandler(client, id, inputValue, type);
5555
}
5656
}
5757

0 commit comments

Comments
 (0)