@fleetbase/fleetops-engine
Version:
Fleet & Transport Management Extension for Fleetbase
51 lines (43 loc) • 1.42 kB
JavaScript
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
import { isArray } from '@ember/array';
import { debug } from '@ember/debug';
import { next } from '@ember/runloop';
import { task, timeout } from 'ember-concurrency';
export default class GlobalSearchComponent extends Component {
store;
globalSearch;
orderActions;
results = [];
query = '';
setupComponent(element) {
next(() => {
element.querySelector('input').focus();
});
}
onInput(e) {
this.search.perform(e.target.value);
}
onKeydown(e) {
if (e.key === 'Escape') this.globalSearch.hide();
}
// for now we only search and display orders but in the future it will perform
// system wide resource search
({ restartable: true }) *search() {
yield timeout(300);
const query = this.query;
if (!query) {
this.results = [];
return;
}
try {
const results = yield this.store.query('order', { query });
this.results = isArray(results) ? results : [];
} catch (err) {
debug('Search failed: ' + err.message);
this.results = [];
}
}
}