@lipagas/storefront-engine
Version:
Headless Commerce & Marketplace Extension for Fleetbase
143 lines (122 loc) • 3.06 kB
JavaScript
import BaseController from '@lipagas/storefront-engine/controllers/base-controller';
import { inject as service } from '@ember/service';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { isBlank } from '@ember/utils';
import { timeout } from 'ember-concurrency';
import { task } from 'ember-concurrency-decorators';
export default class ProductsIndexIndexController extends BaseController {
/**
* Inject the `filters` service
*
* @var {Service}
*/
filters;
/**
* Inject the `intl` service
*
* @var {Service}
*/
intl;
/**
* Inject the `currentUser` service
*
* @var {Service}
*/
currentUser;
/**
* Inject the `currentUser` service
*
* @var {Service}
*/
modalsManager;
/**
* Queryable parameters for this controller's model
*
* @var {Array}
*/
queryParams = ['page', 'limit', 'sort', 'query', 'public_id', 'sku', 'created_at', 'updated_at'];
/**
* The current page of data being viewed
*
* @var {Integer}
*/
page = 1;
/**
* The maximum number of items to show per page
*
* @var {Integer}
*/
limit;
/**
* The param to query with.
*
* @var {String}
*/
query;
/**
* The param to sort the data on, the param with prepended `-` is descending
*
* @var {String}
*/
sort;
/**
* The filterable param `public_id`
*
* @var {String}
*/
public_id;
/**
* The filterable param `drivers_license_number`
*
* @var {String}
*/
sku;
/**
* The filterable param `created_at`
*
* @var {String}
*/
created_at;
/**
* The filterable param `updated_at`
*
* @var {String}
*/
updated_at;
/**
* The search task.
*
* @void
*/
({ restartable: true }) *search({ target: { value } }) {
// if no query don't search
if (isBlank(value)) {
this.query = null;
return;
}
// timeout for typing
yield timeout(250);
// reset page for results
if (this.page > 1) {
this.page = 1;
}
// update the query param
this.query = value;
}
viewProduct(product) {
return this.transitionToRoute('products.index.category.edit', product);
}
deleteProduct(product) {
this.modalsManager.confirm({
title: this.intl.t('storefront.products.index.delete-product'),
body: this.intl.t('storefront.products.index.body-warning'),
confirm: (modal) => {
modal.startLoading();
return product.destroyRecord().then(() => {
return this.hostRouter.refresh();
});
},
});
}
}