-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(akita-filter-plugins): split Akita Filters interface into two typ…
…es for Local Filters and Server Filters. (#55) This makes it possible to distinguish between local filters that must have a predicate function, and those that are server type must have a value, but not a predicate function.
- Loading branch information
Showing
10 changed files
with
93 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 2 additions & 38 deletions
40
projects/akita-filters-plugin/src/lib/akita-filters-store.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
projects/akita-filters-plugin/src/lib/akita-filters.model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import {EntityState, getEntityType, guid, HashMap, ID} from '@datorama/akita'; | ||
import {defaultFilter} from './filters-utils'; | ||
|
||
function capitalize(str: string): string { | ||
return str.charAt(0).toUpperCase() + str.substr(1); | ||
} | ||
|
||
export interface AkitaFilterBase<S extends EntityState, E = getEntityType<S>> { | ||
id: ID; | ||
/** A corresponding name for display the filter, by default, it will be ${id): ${value} */ | ||
name?: string; | ||
/** set the order for filter, by default, it is 10 */ | ||
order?: number; | ||
/** If you want to have filter that is not displayed on the list */ | ||
hide?: boolean; | ||
/** The filter value, this will be used to compute name, or getting the current value, to initiate your form */ | ||
value?: any; | ||
} | ||
|
||
export interface AkitaFilterLocal<S extends EntityState, E = getEntityType<S>> extends AkitaFilterBase<S, E> { | ||
/** If you have enabled server filter, specify witch filters will be call to server, default to false. */ | ||
server?: false; | ||
/** The function to apply filters, by default use defaultFilter helpers, that will search the value in the object */ | ||
predicate: (entity: E, index: number, array: E[] | HashMap<E>, filter: AkitaFilter<S>) => boolean; | ||
} | ||
|
||
export interface AkitaFilterServer<S extends EntityState, E = getEntityType<S>> extends AkitaFilterBase<S, E> { | ||
/** If you have enabled server filter, specify witch filters will be call to server, default to false. */ | ||
server: true; | ||
/** The filter value will be sent to withServer function */ | ||
value: any; | ||
} | ||
|
||
export type AkitaFilter<S extends EntityState, E = getEntityType<S>> = AkitaFilterLocal<S, E> | AkitaFilterServer<S, E> | { | ||
/** add any other data you want to add, keep this to get the compatibilities with others versions **/ | ||
[key: string]: any; | ||
}; | ||
|
||
export function createFilter<S extends EntityState, E = getEntityType<S>> | ||
(filterParams: Partial<AkitaFilterLocal<S> | AkitaFilterServer<S>>) { | ||
const id = filterParams.id ? filterParams.id : guid(); | ||
const name = filterParams.name || (filterParams.value && filterParams.id ? | ||
`${capitalize(filterParams.id.toString())}: ${filterParams.value.toString()}` : undefined); | ||
|
||
if (!(filterParams as AkitaFilterLocal<S>)?.predicate && filterParams.value && !filterParams.server) { | ||
/** use default function, if not provided */ | ||
// @ts-ignore | ||
(filterParams as AkitaFilterLocal<S>).predicate = defaultFilter; | ||
} | ||
|
||
return {id, name, hide: false, order: 10, server: false, ...filterParams} as AkitaFilter<S>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters