2022-05-23 20:04:19 +02:00
|
|
|
import { Component, OnDestroy, OnInit } from "@angular/core";
|
2022-07-26 14:48:11 +02:00
|
|
|
import { UntypedFormControl } from "@angular/forms";
|
2022-08-09 21:11:51 +02:00
|
|
|
import { Subscription } from "rxjs";
|
2021-12-15 23:32:00 +01:00
|
|
|
|
2022-06-14 17:10:53 +02:00
|
|
|
import { StateService } from "@bitwarden/common/abstractions/state.service";
|
2022-05-23 20:04:19 +02:00
|
|
|
|
2021-12-15 23:32:00 +01:00
|
|
|
import { SearchBarService, SearchBarState } from "./search-bar.service";
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: "app-search",
|
|
|
|
templateUrl: "search.component.html",
|
|
|
|
})
|
2022-05-23 20:04:19 +02:00
|
|
|
export class SearchComponent implements OnInit, OnDestroy {
|
2021-12-15 23:32:00 +01:00
|
|
|
state: SearchBarState;
|
2022-07-26 14:48:11 +02:00
|
|
|
searchText: UntypedFormControl = new UntypedFormControl(null);
|
2021-12-15 23:32:00 +01:00
|
|
|
|
2022-08-09 21:11:51 +02:00
|
|
|
private activeAccountSubscription: Subscription;
|
|
|
|
|
2022-05-23 20:04:19 +02:00
|
|
|
constructor(private searchBarService: SearchBarService, private stateService: StateService) {
|
2022-08-26 18:09:28 +02:00
|
|
|
// eslint-disable-next-line rxjs-angular/prefer-takeuntil
|
2022-08-09 21:11:51 +02:00
|
|
|
this.searchBarService.state$.subscribe((state) => {
|
2021-12-15 23:32:00 +01:00
|
|
|
this.state = state;
|
|
|
|
});
|
|
|
|
|
2022-08-26 18:09:28 +02:00
|
|
|
// eslint-disable-next-line rxjs-angular/prefer-takeuntil
|
2021-12-15 23:32:00 +01:00
|
|
|
this.searchText.valueChanges.subscribe((value) => {
|
|
|
|
this.searchBarService.setSearchText(value);
|
|
|
|
});
|
|
|
|
}
|
2022-05-23 20:04:19 +02:00
|
|
|
|
|
|
|
ngOnInit() {
|
2022-08-26 18:09:28 +02:00
|
|
|
// eslint-disable-next-line rxjs-angular/prefer-takeuntil
|
2022-08-09 21:11:51 +02:00
|
|
|
this.activeAccountSubscription = this.stateService.activeAccount$.subscribe((value) => {
|
2022-05-23 20:04:19 +02:00
|
|
|
this.searchBarService.setSearchText("");
|
|
|
|
this.searchText.patchValue("");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
2022-08-09 21:11:51 +02:00
|
|
|
this.activeAccountSubscription.unsubscribe();
|
2022-05-23 20:04:19 +02:00
|
|
|
}
|
2021-12-15 23:32:00 +01:00
|
|
|
}
|