From fbf0a2b84289153b416383f5383931e4753b5ed7 Mon Sep 17 00:00:00 2001 From: markuczy Date: Fri, 10 Jan 2025 11:30:56 +0100 Subject: [PATCH] feat: filter strategy abstract class --- .../data-sort-base/data-sort-base.ts | 6 +- .../data-table/data-table.component.html | 4 +- .../data-table/data-table.component.ts | 8 +- .../filter-view/filter-view.component.html | 8 +- .../interactive-data-view.component.spec.ts | 4 +- .../interactive-data-view/storybook-config.ts | 2 +- .../src/lib/model/filter.model.ts | 20 ++++- .../src/lib/utils/filter-strategy.ts | 74 +++++++++++++++++++ 8 files changed, 109 insertions(+), 17 deletions(-) create mode 100644 libs/angular-accelerator/src/lib/utils/filter-strategy.ts diff --git a/libs/angular-accelerator/src/lib/components/data-sort-base/data-sort-base.ts b/libs/angular-accelerator/src/lib/components/data-sort-base/data-sort-base.ts index 42856df1..07ef787b 100644 --- a/libs/angular-accelerator/src/lib/components/data-sort-base/data-sort-base.ts +++ b/libs/angular-accelerator/src/lib/components/data-sort-base/data-sort-base.ts @@ -80,11 +80,13 @@ export class DataSortBase { )?.toString() switch (filter.filterType) { case undefined: - case FilterType.EQUAL: + case FilterType.EQUALS: return value === String(filter.value) - case FilterType.TRUTHY: { + case FilterType.IS_NOT_EMPTY: { return filter.value ? !!value : !value } + default: + return true } }) ) diff --git a/libs/angular-accelerator/src/lib/components/data-table/data-table.component.html b/libs/angular-accelerator/src/lib/components/data-table/data-table.component.html index e9fa0e7d..2ac56695 100644 --- a/libs/angular-accelerator/src/lib/components/data-table/data-table.component.html +++ b/libs/angular-accelerator/src/lib/components/data-table/data-table.component.html @@ -164,7 +164,7 @@ - filter.columnId === currentFilterColumn?.id && currentFilterColumn.filterType === FilterType.TRUTHY + filter.columnId === currentFilterColumn?.id && currentFilterColumn.filterType === FilterType.IS_NOT_EMPTY ) .map((filter) => filter.value) }) @@ -465,7 +465,7 @@ export class DataTableComponent extends DataSortBase implements OnInit, AfterCon .filter( (filter) => filter.columnId === currentFilterColumn?.id && - (!currentFilterColumn.filterType || currentFilterColumn.filterType === FilterType.EQUAL) + (!currentFilterColumn.filterType || currentFilterColumn.filterType === FilterType.EQUALS) ) .map((filter) => filter.value) }) @@ -473,7 +473,7 @@ export class DataTableComponent extends DataSortBase implements OnInit, AfterCon this.currentEqualFilterOptions$ = combineLatest([this._rows$, this.currentFilterColumn$, this._filters$]).pipe( filter( ([_, currentFilterColumn, __]) => - !currentFilterColumn?.filterType || currentFilterColumn.filterType === FilterType.EQUAL + !currentFilterColumn?.filterType || currentFilterColumn.filterType === FilterType.EQUALS ), mergeMap(([rows, currentFilterColumn, filters]) => { if (!currentFilterColumn?.id) { @@ -484,7 +484,7 @@ export class DataTableComponent extends DataSortBase implements OnInit, AfterCon .filter( (filter) => filter.columnId === currentFilterColumn?.id && - (!currentFilterColumn.filterType || currentFilterColumn.filterType === FilterType.EQUAL) + (!currentFilterColumn.filterType || currentFilterColumn.filterType === FilterType.EQUALS) ) .map((filter) => filter.value) diff --git a/libs/angular-accelerator/src/lib/components/filter-view/filter-view.component.html b/libs/angular-accelerator/src/lib/components/filter-view/filter-view.component.html index 17d80fad..e1e3df80 100644 --- a/libs/angular-accelerator/src/lib/components/filter-view/filter-view.component.html +++ b/libs/angular-accelerator/src/lib/components/filter-view/filter-view.component.html @@ -45,7 +45,7 @@ style="white-space: nowrap" class="p-chip-text flex flex-nowrap" >{{column?.nameKey ?? '' | translate }}: - + { diff --git a/libs/angular-accelerator/src/lib/components/interactive-data-view/storybook-config.ts b/libs/angular-accelerator/src/lib/components/interactive-data-view/storybook-config.ts index 47b33c6f..41d6284f 100644 --- a/libs/angular-accelerator/src/lib/components/interactive-data-view/storybook-config.ts +++ b/libs/angular-accelerator/src/lib/components/interactive-data-view/storybook-config.ts @@ -139,7 +139,7 @@ export const defaultInteractiveDataViewArgs = { nameKey: 'Available', sortable: false, filterable: true, - filterType: FilterType.TRUTHY, + filterType: FilterType.IS_NOT_EMPTY, predefinedGroupKeys: ['test2'], }, { diff --git a/libs/angular-accelerator/src/lib/model/filter.model.ts b/libs/angular-accelerator/src/lib/model/filter.model.ts index 8a6c34dd..8ab0aefd 100644 --- a/libs/angular-accelerator/src/lib/model/filter.model.ts +++ b/libs/angular-accelerator/src/lib/model/filter.model.ts @@ -4,7 +4,23 @@ export interface ColumnFilterDataSelectOptions { export type Filter = { columnId: string; value: unknown; filterType?: FilterType } +export interface FilterObject { + type: FilterType + value1: unknown + value2: unknown +} + export enum FilterType { - EQUAL = 'EQUAL', - TRUTHY = 'TRUTHY', + ENDS_WITH = 'endsWith', + STARTS_WITH = 'startsWith', + CONTAINS = 'contains', + NOT_CONTAINS = 'notContains', + EQUALS = 'equals', + NOT_EQUALS = 'notEquals', + LESS_THAN = 'lessThan', + GREATER_THAN = 'greaterThan', + LESS_THAN_OR_EQUAL = 'lessThanOrEqual', + GREATER_THAN_OR_EQUAL = 'greaterThanOrEqual', + IS_EMPTY = 'isEmpty', + IS_NOT_EMPTY = 'isNotEmpty', } diff --git a/libs/angular-accelerator/src/lib/utils/filter-strategy.ts b/libs/angular-accelerator/src/lib/utils/filter-strategy.ts new file mode 100644 index 00000000..c1a40d90 --- /dev/null +++ b/libs/angular-accelerator/src/lib/utils/filter-strategy.ts @@ -0,0 +1,74 @@ +import { FilterObject } from '../model/filter.model' + +/* eslint-disable @typescript-eslint/no-unused-vars */ +export abstract class FilterStrategy { + endsWith(value: unknown, target: unknown): boolean { + console.error('endsWith method not implemented') + return true + } + + startsWith(value: unknown, target: unknown): boolean { + console.error('startsWith method not implemented') + return true + } + + contains(value: unknown, target: unknown): boolean { + console.error('contains method not implemented') + return true + } + + notContains(value: unknown, target: unknown): boolean { + console.error('notContains method not implemented') + return true + } + + equals(value: unknown, target: unknown): boolean { + console.error('equals method not implemented') + return true + } + + notEquals(value: unknown, target: unknown): boolean { + console.error('notEquals method not implemented') + return true + } + + lessThan(value: unknown, target: unknown): boolean { + console.error('lessThan method not implemented') + return true + } + + greaterThan(value: unknown, target: unknown): boolean { + console.error('greaterThan method not implemented') + return true + } + + lessThanOrEqual(value: unknown, target: unknown): boolean { + console.error('lessThanOrEqual method not implemented') + return true + } + + greaterThanOrEqual(value: unknown, target: unknown): boolean { + console.error('greaterThanOrEqual method not implemented') + return true + } + + isEmpty(value: unknown): boolean { + console.error('isEmpty method not implemented') + return true + } + + isNotEmpty(value: unknown): boolean { + console.error('isNotEmpty method not implemented') + return true + } + + compare(a: unknown, b: unknown): number { + console.error('compare method not implemented') + return 0 + } + + filter(hayStack: unknown[], filterObject: FilterObject): unknown[] { + const { type, ...rest } = filterObject + return hayStack.filter((item) => this[type](item, rest)) + } +}