import { Component, HostBinding, OnDestroy } from '@angular/core'; import { Store } from '@ngrx/store'; import { ClassActions, ClassState, DashboardActions, DEFAULT_NAMED_QUERY_ID, FieldActions, FieldState, NamedQuery, NamedQueryActions, NamedQuerySelectors, NamedQueryState, NamedQueryTypes } from '@onepoint-yap/dashboard'; import { combineLatest, map, Observable, of, Subscription } from 'rxjs'; import { SearchFulltextChange } from '../search-fulltext/search-fulltext/search-fulltext.component'; import { SearchMonoClassActions } from '../search-mono-class/store/search-mono-class.actions'; import { SearchMonoClassSelectors } from '../search-mono-class/store/search-mono-class.selectors'; import { SearchMultiClassActions } from '../search-multi-class/store/search-multi-class.actions'; import { SearchMultiClassState } from '../search-multi-class/store/search-multi-class.reducer'; import { SearchMultiClassSelectors } from '../search-multi-class/store/search-multi-class.selectors'; @Component({ selector: 'yap-search-tools', templateUrl: './search-tools.component.html' }) export class YapSearchToolsComponent implements OnDestroy { @HostBinding('class.o-yap-search-tools-wrapper') class = true; @HostBinding('class.is-close') searchToolsClose = false; searchMultiClassValid$: Observable; searchMonoClassValid$: Observable; loading$: Observable; isRename$: Observable; isSearchCriteria = false; isSearchMono = false; isSearchFullText = false; isSearchFulltextValid = false; isMoreList = false; widgetType: string = 'table'; get isNewMono(): boolean { return this.isSearchCriteria && this.isSearchMono; } get isEditMono$(): Observable { return combineLatest([this.currentEditType$, this.isRename$]).pipe( map(([type, isRename]) => type === NamedQueryTypes.MONO_CLASS && !isRename) ); } getIcon = { caracteristique: 'default', lieu: 'default', meteo: 'default', usager: 'profil', valeur_fonciere: 'default', vehicule: 'car' } as { [clazz: string]: string }; items: Observable = of([ { id: 1, name: '1' }, { id: 2, name: '2' }, { id: 3, name: '3' }, { id: 4, name: '4' } ]); editInProgress$: Observable; currentEdit$: Observable; currentEditType$: Observable; readonly FULL_TEXT = NamedQueryTypes.FULL_TEXT; readonly MONO_CLASS = NamedQueryTypes.MONO_CLASS; readonly MULTI_CLASS = NamedQueryTypes.MULTI_CLASS; searchFulltextTemp?: SearchFulltextChange; private sub = new Subscription(); constructor(private store: Store) { this.loading$ = this.store.select(NamedQuerySelectors.selectFeature).pipe(map((state) => state.isLoading)); this.store.dispatch(NamedQueryActions.load()); this.store.dispatch(FieldActions.load()); this.store.dispatch(ClassActions.load()); this.searchMultiClassValid$ = this.store.select(SearchMultiClassSelectors.searchMultiClassValid); this.searchMonoClassValid$ = this.store.select(SearchMonoClassSelectors.searchMonoClassValid); this.editInProgress$ = this.store.select(NamedQuerySelectors.editInProgress); this.currentEditType$ = this.store.select(NamedQuerySelectors.currentEditType); this.currentEdit$ = this.store.select(NamedQuerySelectors.currentEdit); this.isRename$ = this.store.select(NamedQuerySelectors.isRename); this.sub.add( combineLatest([this.editInProgress$, this.isRename$]).subscribe(([editInProgress, isRename]) => { if (editInProgress && this.searchToolsClose && !isRename) { this.onChangePanel(); } }) ); } ngOnDestroy(): void { this.sub.unsubscribe(); } onSearchCriteria() { this.isSearchCriteria = true; this.store.dispatch(NamedQueryActions.editCurrent({})); } onSearchTools() { this.isSearchCriteria = false; this.store.dispatch(NamedQueryActions.stopEdit()); this.resizeScreen(); } back() { this.store.dispatch(NamedQueryActions.stopEdit()); this.resizeScreen(); } onMonoSearch() { this.isSearchMono = !this.isSearchMono; this.resizeScreen(); } onChangeListView(isMoreList: boolean) { this.isMoreList = !isMoreList; } onSearchFulltextChange(payload: SearchFulltextChange) { setTimeout(() => { this.isSearchFulltextValid = payload.value !== ''; this.searchFulltextTemp = payload; }, 10); } onSearch() { if (this.isSearchMono) { this.store.dispatch(SearchMonoClassActions.search()); } else { this.store.dispatch(SearchMultiClassActions.search()); } this.store.dispatch( DashboardActions.addWidgetToPresentation({ id: DEFAULT_NAMED_QUERY_ID, widgetType: this.widgetType }) ); } onChangePanel() { this.searchToolsClose = !this.searchToolsClose; this.resizeScreen(); } private resizeScreen() { setTimeout(() => window?.dispatchEvent(new Event('resize')), 400); } selectWidget($event: string) { this.widgetType = $event; } reset() { this.store.dispatch(NamedQueryActions.stopEdit()); this.isSearchCriteria = true; } updateVisibility($event: any) { this.store.dispatch(NamedQueryActions.updateCurrentEdit({ currentEdit: { visibility: $event } })); } }