@nu-art/search 0.401.5 → 0.401.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,7 @@ import { Logger } from '@nu-art/ts-common';
2
2
  import { SearchAddOn, SearchAddOnDef, SearchResult } from './SearchAddOn.js';
3
3
  import { SearchItem } from './SearchItem.js';
4
4
  import { SearchSorter } from './SearchSorter.js';
5
+ import { OnSyncStatusChangedListener } from '@nu-art/thunderstorm-frontend/core/db-api-gen/types';
5
6
  export interface SearchAddOnRenderer {
6
7
  __onSearchFilterChanged: VoidFunction;
7
8
  readonly addOn: SearchAddOn<any>;
@@ -9,7 +10,7 @@ export interface SearchAddOnRenderer {
9
10
  export interface SearchResultsRenderer {
10
11
  __onSearchResultsChanged: VoidFunction;
11
12
  }
12
- export declare class SearchContext extends Logger {
13
+ export declare class SearchContext extends Logger implements OnSyncStatusChangedListener<any> {
13
14
  private searchItems;
14
15
  private activeSearchItems;
15
16
  private addOns;
@@ -17,20 +18,26 @@ export declare class SearchContext extends Logger {
17
18
  private sorters;
18
19
  private minimumRequiredActiveAddons;
19
20
  private readonly searchDebouncer;
21
+ private saveFilterDebounce;
20
22
  private readonly filterDictionary;
21
23
  private readonly _filterChangeListeners;
22
24
  private searchResults?;
23
25
  private searchTime?;
24
26
  private readonly _searchResultChangeListeners;
27
+ private _retainFilters;
28
+ private readonly filterStorage;
25
29
  constructor(key: string);
30
+ __onSyncStatusChanged: () => void;
26
31
  setSearchItems: (items: SearchItem<any, any>[]) => this;
27
32
  setAddOns: (addOns: SearchAddOn<any>[]) => this;
28
33
  setSorters: (sorters: SearchSorter<any>[]) => this;
29
34
  setMinimumRequiredActiveAddOns: (num: number) => this;
35
+ retainFilters: () => this;
30
36
  private setActiveAddOns;
31
37
  private setActiveSearchItems;
32
38
  private getInitialSearchResults;
33
39
  private search;
40
+ private saveFilters;
34
41
  getActiveSearchItems: () => Readonly<{
35
42
  module: import("@nu-art/thunderstorm-frontend").ModuleFE_BaseDB<any, import("@nu-art/thunderstorm-frontend/core/db-api-gen/db-def").DBApiFEConfig<any>>;
36
43
  entityLabel: string;
@@ -1,4 +1,5 @@
1
1
  import { arrayIncludesAll, BadImplementationException, Debounce, Logger, removeItemFromArray } from '@nu-art/ts-common';
2
+ import { StorageKey, Thunder } from '@nu-art/thunderstorm-frontend';
2
3
  export class SearchContext extends Logger {
3
4
  searchItems;
4
5
  activeSearchItems;
@@ -7,11 +8,14 @@ export class SearchContext extends Logger {
7
8
  sorters;
8
9
  minimumRequiredActiveAddons = 0;
9
10
  searchDebouncer = new Debounce(() => this.search(), 200, 500);
10
- filterDictionary = {};
11
+ saveFilterDebounce;
12
+ filterDictionary;
11
13
  _filterChangeListeners = [];
12
14
  searchResults;
13
15
  searchTime;
14
16
  _searchResultChangeListeners = [];
17
+ _retainFilters = false;
18
+ filterStorage;
15
19
  constructor(key) {
16
20
  super(`SearchContext-${key}`);
17
21
  this.searchItems = [];
@@ -19,7 +23,19 @@ export class SearchContext extends Logger {
19
23
  this.addOns = [];
20
24
  this.activeAddOns = [];
21
25
  this.sorters = [];
26
+ this.filterStorage = new StorageKey(`search-context-data__${key}`);
27
+ this.filterDictionary = this.filterStorage.get({});
28
+ //Register listeners with a delay to let Thunder instance be created
29
+ setTimeout(() => {
30
+ // @ts-ignore
31
+ Thunder.getInstance().addUIListener(this);
32
+ this.searchItems.forEach(searchItem => {
33
+ // @ts-ignore
34
+ this[searchItem.module.defaultDispatcher.method] = () => this.searchDebouncer.trigger();
35
+ });
36
+ });
22
37
  }
38
+ __onSyncStatusChanged = () => this.searchDebouncer.trigger();
23
39
  //######################### Factory Logic #########################
24
40
  setSearchItems = (items) => {
25
41
  this.searchItems = items;
@@ -41,6 +57,11 @@ export class SearchContext extends Logger {
41
57
  this.minimumRequiredActiveAddons = num;
42
58
  return this;
43
59
  };
60
+ retainFilters = () => {
61
+ this._retainFilters = true;
62
+ this.saveFilterDebounce = new Debounce(() => this.saveFilters(), 200, 500);
63
+ return this;
64
+ };
44
65
  //######################### Internal Logic #########################
45
66
  setActiveAddOns = () => {
46
67
  this.activeAddOns = this.addOns.filter(addOn => {
@@ -100,6 +121,11 @@ export class SearchContext extends Logger {
100
121
  this.searchTime = Date.now() - startTime;
101
122
  this._searchResultChangeListeners.forEach(listener => listener.__onSearchResultsChanged());
102
123
  };
124
+ saveFilters = () => {
125
+ if (!this._retainFilters)
126
+ return;
127
+ this.filterStorage.set({ ...this.filterDictionary });
128
+ };
103
129
  //######################### Public Logic #########################
104
130
  getActiveSearchItems = () => [...this.activeSearchItems];
105
131
  getSearchResults = () => this.searchResults;
@@ -108,6 +134,8 @@ export class SearchContext extends Logger {
108
134
  filter = {
109
135
  set: (key, value) => {
110
136
  this.filterDictionary[key] = value;
137
+ this.saveFilterDebounce?.trigger();
138
+ this.saveFilters();
111
139
  //Re-calculate active addons and search items
112
140
  this.setActiveAddOns();
113
141
  this.setActiveSearchItems();
@@ -10,8 +10,8 @@ type State = {
10
10
  placeholder?: string;
11
11
  };
12
12
  export declare class Component_AddOn_SearchTerms extends Component_SearchAddOn<AddOnDef_SearchTerms, Props, State> {
13
- protected deriveStateFromProps(nextProps: InferProps<this>, state: InferState<this>): InferState<this>;
14
13
  addOn: SearchAddOn<AddOnDef_SearchTerms>;
14
+ protected deriveStateFromProps(nextProps: InferProps<this>, state: InferState<this>): InferState<this>;
15
15
  render(): import("react/jsx-runtime").JSX.Element;
16
16
  }
17
17
  export {};
@@ -5,11 +5,11 @@ import { AddOn_SearchTerms } from './types.js';
5
5
  import './Component_AddOn_SearchTerms.scss';
6
6
  import { TS_Icons } from '@nu-art/ts-styles';
7
7
  export class Component_AddOn_SearchTerms extends Component_SearchAddOn {
8
+ addOn = AddOn_SearchTerms;
8
9
  deriveStateFromProps(nextProps, state) {
9
10
  state.placeholder = nextProps.placeholder;
10
11
  return state;
11
12
  }
12
- addOn = AddOn_SearchTerms;
13
13
  render() {
14
14
  return _jsxs(LL_H_C, { className: 'search-add-on__search-terms', children: [_jsx(TS_Input, { type: 'text', value: this.state.value, placeholder: this.state.placeholder, onChange: val => this.setValue(val.trimStart()) }), _jsx(TS_Icons.Search.component, {})] });
15
15
  }
@@ -9,6 +9,7 @@ export class Component_SearchAddOn extends ComponentSync {
9
9
  }
10
10
  componentDidMount() {
11
11
  this.props.context.filterChangeListeners.register(this);
12
+ this.setState({ value: this.props.context.filter.get(this.addOn.key) });
12
13
  }
13
14
  componentWillUnmount() {
14
15
  this.props.context.filterChangeListeners.unregister(this);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nu-art/search",
3
- "version": "0.401.5",
3
+ "version": "0.401.6",
4
4
  "description": "Search",
5
5
  "keywords": [
6
6
  "TacB0sS",
@@ -37,9 +37,9 @@
37
37
  "linkDirectory": true
38
38
  },
39
39
  "dependencies": {
40
- "@nu-art/ts-common": "0.401.5",
41
- "@nu-art/ts-styles": "0.401.5",
42
- "@nu-art/thunderstorm-frontend": "0.401.5",
40
+ "@nu-art/ts-common": "0.401.6",
41
+ "@nu-art/ts-styles": "0.401.6",
42
+ "@nu-art/thunderstorm-frontend": "0.401.6",
43
43
  "react": "^18.0.0"
44
44
  },
45
45
  "devDependencies": {