import { Component, Inject, OnInit } from '@angular/core'; import { Store } from '@ngrx/store'; import { ConfigActions, DEFAULT_NAMED_QUERY_ID, EXPLORE_NAMED_QUERY_ID, NamedQuery, NamedQueryActions, NamedQuerySelectors, NamedQueryTypes, YAP_DIALOG_DATA, YapDialogRef, YapVisibility, YapVisibilityType } from '@onepoint-yap/dashboard'; import { Observable, take } from 'rxjs'; import { v4 as uuidv4 } from 'uuid'; import { SearchFulltextChange } from '../../search-fulltext/search-fulltext/search-fulltext.component'; import { searchFulltextActions } from '../../search-fulltext/store/search-fulltext.actions'; import { SearchMonoClassActions } from '../../search-mono-class/store/search-mono-class.actions'; import { SearchMultiClassActions } from '../../search-multi-class/store/search-multi-class.actions'; @Component({ selector: 'yap-save-query', templateUrl: './save-query.component.html' }) export class YapSaveQueryComponent implements OnInit { value: string = ''; opened = false; currentEdit$: Observable; isEdit = false; id?: string; name: string = ''; description: string = ''; visibility: YapVisibility = { type: YapVisibilityType.PRIVATE, users: [] }; savedNameQuery?: NamedQuery; get modalSave(): boolean { return this.data.save; } get saveDisabled(): boolean { return (this.name ?? '') === '' || (this.savedNameQuery?.name === this.name && !this.savedNameQuery.owner); } constructor( private store: Store, private dialog: YapDialogRef, @Inject(YAP_DIALOG_DATA) private data: { save: boolean; type: NamedQueryTypes; payload?: SearchFulltextChange; } ) { this.currentEdit$ = this.store.select(NamedQuerySelectors.currentEdit); } ngOnInit(): void { this.currentEdit$.pipe(take(1)).subscribe((nq) => { if (nq) { this.savedNameQuery = nq; this.id = nq.id; this.isEdit = true; this.name = nq.name; this.description = nq.description ?? ''; } }); } onSave() { const id = this.id === DEFAULT_NAMED_QUERY_ID || this.id === EXPLORE_NAMED_QUERY_ID || !this.id || this.savedNameQuery?.name !== this.name ? uuidv4() : this.id; if (this.name !== '') { const descriptionQuery = { name: this.name, description: this.description ?? undefined, visibility: this.visibility }; switch (this.data.type) { case NamedQueryTypes.FULL_TEXT: const classes = this.data.payload?.classes ?? []; const search = this.data.payload?.value ?? ''; this.store.dispatch(searchFulltextActions.save({ descriptionQuery, classes, search, id })); break; case NamedQueryTypes.MONO_CLASS: this.store.dispatch(SearchMonoClassActions.save({ descriptionQuery, id })); break; case NamedQueryTypes.MULTI_CLASS: this.store.dispatch(SearchMultiClassActions.save({ descriptionQuery, id })); break; } this.onClose(); } } onClose() { this.store.dispatch(ConfigActions.closeOverlay({ id: 'YapSaveQueryComponent' })); this.store.dispatch(NamedQueryActions.stopRename()); if (this.isEdit) { this.store.dispatch(NamedQueryActions.stopEdit()); } this.isEdit = false; this.dialog.close(); } }