@ducna01120/fleetops-engine
Version:
Fleet & Transport Management Extension for Fleetbase
118 lines (91 loc) • 3.31 kB
JavaScript
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { inject as service } from '@ember/service';
import { action, computed } from '@ember/object';
import { isArray } from '@ember/array';
import { isBlank } from '@ember/utils';
import { timeout } from 'ember-concurrency';
import { task } from 'ember-concurrency-decorators';
export default class FleetVehicleListingComponent extends Component {
store;
appCache;
vehicles = [];
selected = [];
query;
isLoading = false;
isLoaded = false;
get ids() {
return this.selected.map((selection) => selection.id);
}
async setupComponent() {
const { selected, fleet } = this.args;
this.selected = isArray(selected) && selected.length ? selected : [];
this.vehicles = await this.queryFleetVehicles(fleet);
}
setSelected() {
const { selected } = this.args;
if (isArray(selected) && selected.length) {
this.selected = selected;
}
}
select(option, selected) {
if (selected) {
this.selected.pushObject(option);
} else {
this.selected.removeObject(option);
}
option.setProperties({ selected });
if (typeof this.args.onChange === 'function') {
this.args.onChange(this.selected);
}
}
onAdd(vehicle) {
if (typeof this.args.onRemoveVehicle === 'function') {
this.args.onAddVehicle(vehicle);
}
const index = this.vehicles.findIndex((d) => d.id === vehicle.id);
if (index === -1) {
this.vehicles.pushObject(vehicle);
}
}
onRemove(vehicle) {
if (typeof this.args.onRemoveVehicle === 'function') {
this.args.onRemoveVehicle(vehicle);
}
this.vehicles.removeObject(vehicle);
}
queryFleetVehicles(fleet, query = null) {
this.isLoading = true;
const cacheKey = `${fleet.id}:fleetVehicles`;
// const cachedResults = this.appCache.getEmberData(cacheKey, 'vehicle');
// if (cachedResults) {
// this.vehicles = this.toggleSelected(cachedResults);
// this.isLoaded = true;
// }
return new Promise((resolve) => {
this.store.query('vehicle', { query, fleet: fleet.id }).then((vehicles) => {
this.isLoading = false;
this.isLoaded = true;
// save to cache
if (!query) {
this.appCache.setEmberData(cacheKey, vehicles);
}
resolve(this.toggleSelected(vehicles));
});
});
}
toggleSelected(options = []) {
return options.map((option) => {
option.set('selected', this.ids.includes(option.id));
return option;
});
}
*search({ target: { value } }) {
const { fleet } = this.args;
if (isBlank(value)) {
return;
}
yield timeout(300);
this.vehicles = yield this.queryFleetVehicles(fleet, value);
}
}