Skip to content

Commit

Permalink
refactor extra select2:select event listener
Browse files Browse the repository at this point in the history
  • Loading branch information
m5r committed Oct 28, 2024
1 parent a2d3fff commit 31d5b6b
Showing 1 changed file with 19 additions and 34 deletions.
53 changes: 19 additions & 34 deletions webapp/src/ts/services/select2-search.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ import { SearchTelemetryService } from '@mm-services/search-telemetry.service';
providedIn: 'root'
})
export class Select2SearchService {
private selectEl;
private currentQuery: string | null = null;
private onContactSelect;

constructor(
private readonly route: ActivatedRoute,
Expand Down Expand Up @@ -64,8 +62,6 @@ export class Select2SearchService {

private query(params, successCb, failureCb, options, types) {
this.currentQuery = params.data.q;
const search = params.data.q;

const searchOptions = {
limit: options.pageSize,
skip: this.calculateSkip(params.data.page, options.pageSize),
Expand All @@ -74,7 +70,7 @@ export class Select2SearchService {

const filters: Filter = {
types: { selected: types },
search,
search: params.data.q,
};
if (options.filterByParent) {
filters.parent = this.getContactId();
Expand All @@ -83,28 +79,10 @@ export class Select2SearchService {
this.searchService
.search('contacts', filters, searchOptions)
.then((documents) => {
if (this.currentQuery !== search) {
if (this.currentQuery !== params.data.q) {
return;
}

if (this.onContactSelect) {
this.selectEl!.off('select2:select', this.onContactSelect);
this.onContactSelect = null;
}

this.onContactSelect = async (event) => {
this.onContactSelect = null;

const docId = event.params?.data?.id;
if (!search || !docId) {
return;
}

const doc = await this.getDoc(docId);
await this.searchTelemetryService.recordContactByTypeSearch(doc, search);
};
this.selectEl!.one('select2:select', this.onContactSelect);

this.currentQuery = null;
successCb({
results: options.sendMessageExtras(this.prepareRows(documents)),
Expand Down Expand Up @@ -205,16 +183,24 @@ export class Select2SearchService {
// Hydrate and re-set real doc on change
// !tags -> only support single values, until there is a use-case
if (!options.tags) {
selectEl.on('select2:select', (e) => {
const docId = e.params && e.params.data && e.params.data.id;
selectEl.on('select2:select', async (event) => {
const docId = event.params?.data?.id;

if (docId) {
this.getDoc(docId)
.then(doc => {
this.setDoc(selectEl, doc);
selectEl.trigger('change');
})
.catch(err => console.error('Select2 failed to get document', err));
if (!docId) {
return;
}

try {
const doc = await this.getDoc(docId);
this.setDoc(selectEl, doc);
selectEl.trigger('change');

const search = selectEl.data('select2').dropdown.$search.val();
if (search) {
await this.searchTelemetryService.recordContactByTypeSearch(doc, search);
}
} catch (error) {
console.error('Select2 failed to get document', error);
}
});
}
Expand Down Expand Up @@ -243,7 +229,6 @@ export class Select2SearchService {
}

async init(selectEl, _types, _options:any = {}) {
this.selectEl = selectEl;
const settings = await this.settingsService.get();
const types = Array.isArray(_types) ? _types : [ _types ];
const options = {
Expand Down

0 comments on commit 31d5b6b

Please sign in to comment.