import { Component, Input, OnChanges, SimpleChanges } from '@angular/core'; import { NamedQuery, NamedQueryTypes, NamedQueryUtils, YapDialogService } from '@onepoint-yap/dashboard'; import { SearchFulltextChange } from '../../search-fulltext/search-fulltext/search-fulltext.component'; import { YapSaveQueryComponent } from '../save-query/save-query.component'; @Component({ selector: 'yap-save-query-button', templateUrl: './save-query-button.component.html' }) export class YapSaveQueryButtonComponent implements OnChanges { @Input() editInProgress: boolean | null = false; @Input() currentEdit?: NamedQuery | null; @Input() currentEditType?: NamedQueryTypes | null; @Input() fulltextValid: boolean | null = false; @Input() multiValid: boolean | null = false; @Input() monoValid: boolean | null = false; @Input() isNotFulltext = false; @Input() isMonoSearch = false; @Input() fulltextSearch?: SearchFulltextChange; saveDisabled = true; constructor(private yapDialog: YapDialogService) {} ngOnChanges(changes: SimpleChanges): void { const isEditFulltextValid = this.editInProgress && this.currentEditType === NamedQueryTypes.FULL_TEXT && this.fulltextValid; const isEditMonoValid = this.editInProgress && this.currentEditType === NamedQueryTypes.MONO_CLASS && this.monoValid; const isEditMultiValid = this.editInProgress && this.currentEditType === NamedQueryTypes.MULTI_CLASS && this.multiValid; const isCreateFulltextValid = !this.editInProgress && !this.isNotFulltext && this.fulltextValid; const isCreateMonoValid = !this.editInProgress && this.isNotFulltext && this.isMonoSearch && this.monoValid; const isCreateMultiValid = !this.editInProgress && this.isNotFulltext && !this.isMonoSearch && this.multiValid; this.saveDisabled = !isEditFulltextValid && !isEditMonoValid && !isEditMultiValid && !isCreateFulltextValid && !isCreateMonoValid && !isCreateMultiValid; } openSaveQuery() { if (!this.saveDisabled) { const save = true; const nq = this.currentEdit; const type = nq ? NamedQueryUtils.getTypeFromPayload(nq.request) : this.getNQType(this.isNotFulltext, this.isMonoSearch); const data = type === NamedQueryTypes.FULL_TEXT ? { save, type, payload: this.fulltextSearch } : { save, type }; this.yapDialog.open(YapSaveQueryComponent, { data }); } } private getNQType(isNotFulltext: boolean, isMono: boolean): NamedQueryTypes { return isNotFulltext ? isMono ? NamedQueryTypes.MONO_CLASS : NamedQueryTypes.MULTI_CLASS : NamedQueryTypes.FULL_TEXT; } }