Skip to content

Commit e6e49dc

Browse files
committed
fix(search): refactored search pillar filters range interface
CLOSES: JOB-912
1 parent 2086171 commit e6e49dc

File tree

4 files changed

+122
-51
lines changed

4 files changed

+122
-51
lines changed

src/search/search.service.ts

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ import {
99
MultiSelectFilter,
1010
PaginatedData,
1111
PillarInfo,
12-
RangeFilter,
1312
ResponseWithOptionalData,
1413
SearchNav,
14+
SearchRangeFilter,
1515
SearchResult,
1616
SearchResultItem,
1717
SearchResultNav,
18-
SelectFilter,
1918
SingleSelectFilter,
2019
} from "src/shared/interfaces";
2120
import { CustomLogger } from "src/shared/utils/custom-logger";
@@ -1367,7 +1366,9 @@ export class SearchService {
13671366
async searchPillarFilters(
13681367
params: SearchPillarFiltersParams,
13691368
community: string | undefined,
1370-
): Promise<ResponseWithOptionalData<(RangeFilter | SelectFilter)[]>> {
1369+
): Promise<
1370+
ResponseWithOptionalData<(SearchRangeFilter | SingleSelectFilter)[]>
1371+
> {
13711372
try {
13721373
const query = NAV_FILTER_CONFIG_QUERY_MAPPINGS[params.nav];
13731374

@@ -1421,7 +1422,7 @@ export class SearchService {
14211422

14221423
const configData = result.records?.map(record => record.get("config"));
14231424

1424-
let data: (RangeFilter | SelectFilter)[];
1425+
let data: (SearchRangeFilter | SingleSelectFilter)[];
14251426

14261427
const sort = createNewSortInstance({
14271428
comparer: new Intl.Collator(undefined, {
@@ -1488,22 +1489,20 @@ export class SearchService {
14881489
return null;
14891490
}
14901491
if (presets.kind === "RANGE") {
1491-
return new RangeFilter({
1492-
value: {
1493-
lowest: {
1494-
value:
1495-
intConverter(
1496-
min(filtered.flatMap(y => y[x]).filter(Boolean)),
1497-
) ?? 0,
1498-
paramKey: FILTER_PARAM_KEY_PRESETS[params.nav][x].lowest,
1499-
},
1500-
highest: {
1501-
value:
1502-
intConverter(
1503-
max(filtered.flatMap(y => y[x]).filter(Boolean)),
1504-
) ?? 0,
1505-
paramKey: FILTER_PARAM_KEY_PRESETS[params.nav][x].highest,
1506-
},
1492+
return new SearchRangeFilter({
1493+
min: {
1494+
value:
1495+
intConverter(
1496+
min(filtered.flatMap(y => y[x]).filter(Boolean)),
1497+
) ?? 0,
1498+
paramKey: FILTER_PARAM_KEY_PRESETS[params.nav][x].lowest,
1499+
},
1500+
max: {
1501+
value:
1502+
intConverter(
1503+
max(filtered.flatMap(y => y[x]).filter(Boolean)),
1504+
) ?? 0,
1505+
paramKey: FILTER_PARAM_KEY_PRESETS[params.nav][x].highest,
15071506
},
15081507
...presets,
15091508
});
@@ -1534,7 +1533,8 @@ export class SearchService {
15341533
.map(x => ({
15351534
label: x,
15361535
value: slugify(x),
1537-
})),
1536+
}))
1537+
.slice(0, 20),
15381538
...presets,
15391539
paramKey: FILTER_PARAM_KEY_PRESETS[params.nav][x],
15401540
});
@@ -1550,22 +1550,20 @@ export class SearchService {
15501550
return null;
15511551
}
15521552
if (presets.kind === "RANGE") {
1553-
return new RangeFilter({
1554-
value: {
1555-
lowest: {
1556-
value:
1557-
intConverter(
1558-
min(configData.flatMap(y => y[x]).filter(Boolean)),
1559-
) ?? 0,
1560-
paramKey: FILTER_PARAM_KEY_PRESETS[params.nav][x].lowest,
1561-
},
1562-
highest: {
1563-
value:
1564-
intConverter(
1565-
max(configData.flatMap(y => y[x]).filter(Boolean)),
1566-
) ?? 0,
1567-
paramKey: FILTER_PARAM_KEY_PRESETS[params.nav][x].highest,
1568-
},
1553+
return new SearchRangeFilter({
1554+
min: {
1555+
value:
1556+
intConverter(
1557+
min(configData.flatMap(y => y[x]).filter(Boolean)),
1558+
) ?? 0,
1559+
paramKey: FILTER_PARAM_KEY_PRESETS[params.nav][x].lowest,
1560+
},
1561+
max: {
1562+
value:
1563+
intConverter(
1564+
max(configData.flatMap(y => y[x]).filter(Boolean)),
1565+
) ?? 0,
1566+
paramKey: FILTER_PARAM_KEY_PRESETS[params.nav][x].highest,
15691567
},
15701568
...presets,
15711569
});
@@ -1600,7 +1598,8 @@ export class SearchService {
16001598
.map(x => ({
16011599
label: x,
16021600
value: slugify(x),
1603-
})),
1601+
}))
1602+
.slice(0, 20),
16041603
...presets,
16051604
paramKey: FILTER_PARAM_KEY_PRESETS[params.nav][x],
16061605
});

src/shared/interfaces/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,4 @@ export * from "./project-with-relations.interface";
6565
export * from "./ats-preferences.interface";
6666
export * from "./ecosystem-activation.interface";
6767
export * from "./defillama-project-prefill.interface";
68+
export * from "./search-filter-configs.interface";
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { isLeft } from "fp-ts/lib/Either";
2+
import * as t from "io-ts";
3+
import { report } from "io-ts-human-reporter";
4+
import { FilterConfigField } from "./filters.interface";
5+
import { ApiProperty } from "@nestjs/swagger";
6+
7+
class RangeFilterValue {
8+
public static readonly RangeFilterValueType = t.strict({
9+
value: t.number,
10+
paramKey: t.string,
11+
});
12+
13+
@ApiProperty()
14+
value: number;
15+
@ApiProperty()
16+
paramKey: string;
17+
18+
constructor(raw: RangeFilterValue) {
19+
const { value, paramKey } = raw;
20+
const result = RangeFilterValue.RangeFilterValueType.decode(raw);
21+
22+
this.value = value;
23+
this.paramKey = paramKey;
24+
25+
if (isLeft(result)) {
26+
report(result).forEach(x => {
27+
throw new Error(x);
28+
});
29+
}
30+
}
31+
}
32+
33+
export class SearchRangeFilter extends FilterConfigField {
34+
public static readonly SearchRangeFilterType = t.intersection([
35+
FilterConfigField.FilterConfigFieldType,
36+
t.strict({
37+
kind: t.literal("RANGE"),
38+
min: RangeFilterValue.RangeFilterValueType,
39+
max: RangeFilterValue.RangeFilterValueType,
40+
prefix: t.union([t.string, t.null]),
41+
}),
42+
]);
43+
44+
@ApiProperty()
45+
kind: string;
46+
@ApiProperty()
47+
prefix: string | null;
48+
@ApiProperty()
49+
min: RangeFilterValue;
50+
@ApiProperty()
51+
max: RangeFilterValue;
52+
53+
constructor(raw: SearchRangeFilter) {
54+
const { kind, prefix, min, max, ...parentProps } = raw;
55+
super(parentProps);
56+
const result = SearchRangeFilter.SearchRangeFilterType.decode(raw);
57+
58+
this.kind = kind;
59+
this.prefix = prefix;
60+
this.min = min;
61+
this.max = max;
62+
63+
if (isLeft(result)) {
64+
report(result).forEach(x => {
65+
throw new Error(x);
66+
});
67+
}
68+
}
69+
}

src/shared/presets/search-filter-configs.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
export enum FilterKind {
22
SINGLE_SELECT = "SINGLE_SELECT",
33
RANGE = "RANGE",
4-
MULTI_SELECT_WITH_SEARCH = "MULTI_SELECT_WITH_SEARCH",
4+
MULTI_SELECT = "MULTI_SELECT",
5+
ORDER = "ORDER",
6+
ORDER_BY = "ORDER_BY",
57
}
68

79
export const SINGLE_SELECT_OPTIONS = {
@@ -84,15 +86,15 @@ export const FILTER_CONFIG_PRESETS = {
8486
label: "Ecosystems",
8587
show: true,
8688
googleAnalyticsEventName: "filter_search_ecosystems",
87-
kind: FilterKind.MULTI_SELECT_WITH_SEARCH,
89+
kind: FilterKind.MULTI_SELECT,
8890
prefix: null,
8991
},
9092
communities: {
9193
position: 2,
9294
label: "Communities",
9395
show: true,
9496
googleAnalyticsEventName: "filter_search_communities",
95-
kind: FilterKind.MULTI_SELECT_WITH_SEARCH,
97+
kind: FilterKind.MULTI_SELECT,
9698
prefix: null,
9799
},
98100
tvl: {
@@ -156,15 +158,15 @@ export const FILTER_CONFIG_PRESETS = {
156158
label: "Order",
157159
show: true,
158160
googleAnalyticsEventName: "filter_search_order",
159-
kind: FilterKind.SINGLE_SELECT,
161+
kind: FilterKind.ORDER,
160162
options: SINGLE_SELECT_OPTIONS.projects.order,
161163
},
162164
orderBy: {
163165
position: 11,
164166
label: "Order By",
165167
show: true,
166168
googleAnalyticsEventName: "filter_search_order_by",
167-
kind: FilterKind.SINGLE_SELECT,
169+
kind: FilterKind.ORDER_BY,
168170
options: SINGLE_SELECT_OPTIONS.projects.orderBy,
169171
},
170172
},
@@ -182,15 +184,15 @@ export const FILTER_CONFIG_PRESETS = {
182184
label: "Communities",
183185
show: true,
184186
googleAnalyticsEventName: "filter_search_communities",
185-
kind: FilterKind.MULTI_SELECT_WITH_SEARCH,
187+
kind: FilterKind.MULTI_SELECT,
186188
prefix: null,
187189
},
188190
ecosystems: {
189191
position: 3,
190192
label: "Ecosystems",
191193
show: true,
192194
googleAnalyticsEventName: "filter_search_ecosystems",
193-
kind: FilterKind.MULTI_SELECT_WITH_SEARCH,
195+
kind: FilterKind.MULTI_SELECT,
194196
prefix: null,
195197
},
196198
hasProjects: {
@@ -214,15 +216,15 @@ export const FILTER_CONFIG_PRESETS = {
214216
label: "Order",
215217
show: true,
216218
googleAnalyticsEventName: "filter_search_order",
217-
kind: FilterKind.SINGLE_SELECT,
219+
kind: FilterKind.ORDER,
218220
options: SINGLE_SELECT_OPTIONS.organizations.order,
219221
},
220222
orderBy: {
221223
position: 7,
222224
label: "Order By",
223225
show: true,
224226
googleAnalyticsEventName: "filter_search_order_by",
225-
kind: FilterKind.SINGLE_SELECT,
227+
kind: FilterKind.ORDER_BY,
226228
options: SINGLE_SELECT_OPTIONS.organizations.orderBy,
227229
},
228230
},
@@ -248,15 +250,15 @@ export const FILTER_CONFIG_PRESETS = {
248250
label: "Order",
249251
show: true,
250252
googleAnalyticsEventName: "filter_search_order",
251-
kind: FilterKind.SINGLE_SELECT,
253+
kind: FilterKind.ORDER,
252254
options: SINGLE_SELECT_OPTIONS.grants.order,
253255
},
254256
orderBy: {
255257
position: 4,
256258
label: "Order By",
257259
show: true,
258260
googleAnalyticsEventName: "filter_search_order_by",
259-
kind: FilterKind.SINGLE_SELECT,
261+
kind: FilterKind.ORDER_BY,
260262
options: SINGLE_SELECT_OPTIONS.grants.orderBy,
261263
},
262264
},
@@ -267,15 +269,15 @@ export const FILTER_CONFIG_PRESETS = {
267269
label: "Order",
268270
show: true,
269271
googleAnalyticsEventName: "filter_search_order",
270-
kind: FilterKind.SINGLE_SELECT,
272+
kind: FilterKind.ORDER,
271273
options: SINGLE_SELECT_OPTIONS.impact.order,
272274
},
273275
orderBy: {
274276
position: 11,
275277
label: "Order By",
276278
show: true,
277279
googleAnalyticsEventName: "filter_search_order_by",
278-
kind: FilterKind.SINGLE_SELECT,
280+
kind: FilterKind.ORDER_BY,
279281
options: SINGLE_SELECT_OPTIONS.impact.orderBy,
280282
},
281283
},

0 commit comments

Comments
 (0)