-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathfull-text-search-bar.component.ts
96 lines (84 loc) · 2.8 KB
/
full-text-search-bar.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { Component, Input, OnDestroy, OnInit } from "@angular/core";
import { FormsModule, ReactiveFormsModule } from "@angular/forms";
import { MatSelectModule } from "@angular/material/select";
import { MatFormFieldModule } from "@angular/material/form-field";
import { MatInputModule } from "@angular/material/input";
import { MatCardModule } from "@angular/material/card";
import { MatIconModule } from "@angular/material/icon";
import { MatButtonModule } from "@angular/material/button";
import {
fetchDatasetsAction,
fetchFacetCountsAction,
setSearchTermsAction,
setTextFilterAction,
} from "../../../state-management/actions/datasets.actions";
import { Store } from "@ngrx/store";
import { BehaviorSubject, Subject, Subscription } from "rxjs";
import {
debounceTime,
distinctUntilChanged,
withLatestFrom,
} from "rxjs/operators";
import { selectSearchTerms } from "../../../state-management/selectors/datasets.selectors";
@Component({
selector: "full-text-search-bar",
templateUrl: "./full-text-search-bar.component.html",
styleUrls: ["./full-text-search-bar.component.scss"],
standalone: true,
imports: [
MatFormFieldModule,
MatSelectModule,
FormsModule,
ReactiveFormsModule,
MatInputModule,
MatIconModule,
MatCardModule,
MatButtonModule,
],
})
export class FullTextSearchBarComponent implements OnInit, OnDestroy {
@Input() prefilledValue = "";
@Input() placeholder = "Text Search";
@Input() clear: boolean;
searchTerm = "";
searchTermSubject = new BehaviorSubject<string>("");
searchClickSubject = new Subject<void>();
subscriptions: Subscription[] = [];
constructor(private store: Store) {}
ngOnInit(): void {
this.searchTermSubject.next(this.prefilledValue);
this.subscriptions.push(
this.searchTermSubject
.pipe(debounceTime(200), distinctUntilChanged())
.subscribe((terms) => {
console.log(`set terms: ${terms}`);
this.store.dispatch(setSearchTermsAction({ terms }));
this.store.dispatch(setTextFilterAction({ text: terms }));
}),
);
const searchTerms$ = this.store.select(selectSearchTerms);
this.subscriptions.push(
this.searchClickSubject
.pipe(debounceTime(250), withLatestFrom(searchTerms$))
.subscribe(([_, terms]) => {
console.log(`latest terms: ${terms}`);
this.store.dispatch(fetchDatasetsAction());
this.store.dispatch(fetchFacetCountsAction());
}),
);
}
onSearch(): void {
this.searchClickSubject.next();
}
onSearchTermChange(terms: string) {
this.searchTermSubject.next(terms);
}
onClear(): void {
this.searchTerm = "";
this.searchTermSubject.next(undefined);
this.searchClickSubject.next();
}
ngOnDestroy(): void {
this.subscriptions.forEach((s) => s.unsubscribe());
}
}