You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Create the type for a generic filter that has everything you need
// This is an interface to describe what your data looks likeinterfaceJob{name: string;salary: number;}// These are generic is a generic interface to describe a filter// You can make more of these for other types.interfaceNumericFilter{equals?: number;greaterThan?: number;lessThan?: number;}interfaceTextFilter{equals?: string;contains?: string;startsWith?: string;}// Utility type to map base types to their corresponding filter typestypeFilterType<T>=Textendsnumber ? NumericFilter[] :
Textendsstring ? TextFilter[] :
never;// Generic Filter interfaceinterfaceFilter<T>{[KinkeyofT]?: FilterType<T[K]>;}// Creating the Person filter using the generic abovetypePersonFilter=Filter<Person>;// PersonFilter is equivelent to the following if you hard coded itinterfacePersonFilter{name: TextFilter[];age: NumericFilter[];}// A valid person could be:constpeople: Person[]=[{name: "John",age: 20},{name: "Jane",age: 30}]// A valid person filter by this description is:constpersonFilter: PersonFilter={name: [{startsWith: "John"}],age: [{greaterThan: 30},{lessThan: 60}]}
Create react state for a filter, and UI to change the filter state
Implement the filter function
// for a person specific filter functionfunctionfilterPeople(people: Person[],filter: PersonFilter): Person[]{
...
}// for a generic filter functionfunctionfilter<T>(elements: T[],filter: Filter<T>): T[]{
...
}// to us the filter functionconstfilteredPeople=filter(people,personFilter)
Creating / updating / deleting jobs while in a sorted / filtered view
creating jobs while in a sorted view
creating jobs while in a filtered view
updating jobs while in a sorted view
updating jobs while in a filtered view
deleting jobs while in a sorted view
deleting jobs while in a filtered view
Optional Steps
handle boolean conditions in filters
handle OR conditions in filters
handle AND conditions in filters
handle combinations of OR and AND conditions in filters
handle recursive types
// There are a few limitations with this as it was quite rushed, you can't handle nested types, only flat types. i.e. // You could not filter on data with the following interface easily:interfaceNested{name: string;occupation: {company: string;yearsTenure: number;}}
generate these generic filter types based on the back end models
The text was updated successfully, but these errors were encountered:
Necessary Steps
Optional Steps
The text was updated successfully, but these errors were encountered: