🔍 support for search
This commit is contained in:
18
src/application/State/Filter/FilterResult.ts
Normal file
18
src/application/State/Filter/FilterResult.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { IFilterResult } from './IFilterResult';
|
||||
import { IScript } from '@/domain/Script';
|
||||
import { ICategory } from '@/domain/ICategory';
|
||||
|
||||
export class FilterResult implements IFilterResult {
|
||||
constructor(
|
||||
public readonly scriptMatches: ReadonlyArray<IScript>,
|
||||
public readonly categoryMatches: ReadonlyArray<ICategory>,
|
||||
public readonly query: string) {
|
||||
if (!query) { throw new Error('Query is empty or undefined'); }
|
||||
if (!scriptMatches) { throw new Error('Script matches is undefined'); }
|
||||
if (!categoryMatches) { throw new Error('Category matches is undefined'); }
|
||||
}
|
||||
public hasAnyMatches(): boolean {
|
||||
return this.scriptMatches.length > 0
|
||||
|| this.categoryMatches.length > 0;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
import { IScript, ICategory } from '@/domain/ICategory';
|
||||
|
||||
export interface IFilterMatches {
|
||||
readonly scriptMatches: ReadonlyArray<IScript>;
|
||||
export interface IFilterResult {
|
||||
readonly categoryMatches: ReadonlyArray<ICategory>;
|
||||
readonly scriptMatches: ReadonlyArray<IScript>;
|
||||
readonly query: string;
|
||||
hasAnyMatches(): boolean;
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
import { IFilterMatches } from './IFilterMatches';
|
||||
import { IFilterResult } from './IFilterResult';
|
||||
import { ISignal } from '@/infrastructure/Events/Signal';
|
||||
|
||||
export interface IUserFilter {
|
||||
readonly filtered: ISignal<IFilterMatches>;
|
||||
readonly filtered: ISignal<IFilterResult>;
|
||||
readonly filterRemoved: ISignal<void>;
|
||||
setFilter(filter: string): void;
|
||||
removeFilter(): void;
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { IFilterMatches } from './IFilterMatches';
|
||||
import { FilterResult } from './FilterResult';
|
||||
import { IFilterResult } from './IFilterResult';
|
||||
import { Application } from '../../../domain/Application';
|
||||
import { IUserFilter } from './IUserFilter';
|
||||
import { Signal } from '@/infrastructure/Events/Signal';
|
||||
|
||||
export class UserFilter implements IUserFilter {
|
||||
public readonly filtered = new Signal<IFilterMatches>();
|
||||
public readonly filtered = new Signal<IFilterResult>();
|
||||
public readonly filterRemoved = new Signal<void>();
|
||||
|
||||
constructor(private application: Application) {
|
||||
@@ -16,14 +17,17 @@ export class UserFilter implements IUserFilter {
|
||||
throw new Error('Filter must be defined and not empty. Use removeFilter() to remove the filter');
|
||||
}
|
||||
const filteredScripts = this.application.getAllScripts().filter(
|
||||
(script) => script.name.toLowerCase().includes(filter.toLowerCase()) ||
|
||||
(script) =>
|
||||
script.name.toLowerCase().includes(filter.toLowerCase()) ||
|
||||
script.code.toLowerCase().includes(filter.toLowerCase()));
|
||||
const filteredCategories = this.application.getAllCategories().filter(
|
||||
(script) => script.name.toLowerCase().includes(filter.toLowerCase()));
|
||||
|
||||
const matches: IFilterMatches = {
|
||||
scriptMatches: filteredScripts,
|
||||
categoryMatches: null,
|
||||
query: filter,
|
||||
};
|
||||
const matches = new FilterResult(
|
||||
filteredScripts,
|
||||
filteredCategories,
|
||||
filter,
|
||||
);
|
||||
|
||||
this.filtered.notify(matches);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user