@lipagas/storefront-engine
Version:
Headless Commerce & Marketplace Extension for Fleetbase
388 lines (354 loc) • 9.51 kB
JavaScript
import BaseController from '@fleetbase/fleetops-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 ManagementContactsIndexController extends BaseController {
/**
* Inject the `currentUser` service
*
* @var {Service}
*/
store;
/**
* Inject the `notifications` service
*
* @var {Service}
*/
notifications;
/**
* Inject the `intl` service
*
* @var {Service}
*/
intl;
/**
* Inject the `modals-manager` service
*
* @var {Service}
*/
modalsManager;
/**
* Inject the `hostRouter` service
*
* @var {Service}
*/
hostRouter;
/**
* Inject the `crud` service
*
* @var {Service}
*/
crud;
/**
* Inject the `filters` service
*
* @var {Service}
*/
filters;
/**
* Inject the `fetch` service
*
* @var {Service}
*/
fetch;
/**
* Queryable parameters for this controller's model
*
* @var {Array}
*/
queryParams = ['page', 'limit', 'sort', 'query', 'public_id', 'internal_id', 'created_by', 'updated_by', 'status', 'title', 'email', 'phone', 'type'];
/**
* 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 sort the data on, the param with prepended `-` is descending
*
* @var {String}
*/
sort = '-created_at';
/**
* The filterable param `public_id`
*
* @var {String}
*/
public_id;
/**
* The filterable param `internal_id`
*
* @var {String}
*/
internal_id;
/**
* The filterable param `title`
*
* @var {String}
*/
title;
/**
* The filterable param `email`
*
* @var {String}
*/
email;
/**
* The filterable param `phone`
*
* @var {String}
*/
phone;
/**
* The filterable param `email`
*
* @var {Array|String}
*/
type;
/**
* The filterable param `status`
*
* @var {Array}
*/
status;
/**
* All possible contact types
*
* @var {String}
*/
contactTypes = ['contact', 'customer'];
/**
* All columns applicable for orders
*
* @var {Array}
*/
columns = [
{
label: this.intl.t('fleet-ops.common.name'),
valuePath: 'name',
width: '170px',
cellComponent: 'table/cell/media-name',
action: this.viewContact,
resizable: true,
sortable: true,
filterable: true,
filterComponent: 'filter/string',
},
{
label: this.intl.t('fleet-ops.common.id'),
valuePath: 'public_id',
cellComponent: 'click-to-copy',
width: '120px',
resizable: true,
sortable: true,
filterable: true,
filterComponent: 'filter/string',
},
{
label: this.intl.t('fleet-ops.common.internal-id'),
valuePath: 'internal_id',
cellComponent: 'click-to-copy',
width: '130px',
resizable: true,
sortable: true,
filterable: true,
filterComponent: 'filter/string',
},
{
label: this.intl.t('fleet-ops.common.title'),
valuePath: 'title',
cellComponent: 'click-to-copy',
width: '160px',
resizable: true,
sortable: true,
filterable: true,
filterComponent: 'filter/string',
},
{
label: this.intl.t('fleet-ops.common.email'),
valuePath: 'email',
cellComponent: 'click-to-copy',
width: '160px',
resizable: true,
sortable: true,
filterable: true,
filterComponent: 'filter/string',
},
{
label: this.intl.t('fleet-ops.common.phone'),
valuePath: 'phone',
cellComponent: 'click-to-copy',
width: '140px',
resizable: true,
sortable: true,
filterable: true,
filterComponent: 'filter/string',
},
{
label: this.intl.t('fleet-ops.common.type'),
valuePath: 'type',
cellComponent: 'table/cell/status',
width: '100px',
resizable: true,
sortable: true,
filterable: true,
filterComponent: 'filter/multi-option',
filterOptions: ['contact', 'customer'],
},
{
label: this.intl.t('fleet-ops.management.contacts.index.created'),
valuePath: 'createdAt',
sortParam: 'created_at',
width: '130px',
resizable: true,
sortable: true,
filterable: true,
filterComponent: 'filter/date',
},
{
label: this.intl.t('fleet-ops.management.contacts.index.updated'),
valuePath: 'updatedAt',
sortParam: 'updated_at',
width: '130px',
resizable: true,
sortable: true,
hidden: true,
filterable: true,
filterComponent: 'filter/date',
},
{
label: '',
cellComponent: 'table/cell/dropdown',
ddButtonText: false,
ddButtonIcon: 'ellipsis-h',
ddButtonIconPrefix: 'fas',
ddMenuLabel: 'Contact Actions',
cellClassNames: 'overflow-visible',
wrapperClass: 'flex items-center justify-end mx-2',
width: '10%',
actions: [
{
label: this.intl.t('fleet-ops.management.contacts.index.view-contact'),
fn: this.viewContact,
},
{
label: this.intl.t('fleet-ops.management.contacts.index.edit-contact'),
fn: this.editContact,
},
{
separator: true,
},
{
label: this.intl.t('fleet-ops.management.contacts.index.delete-contact'),
fn: this.deleteContact,
},
],
sortable: false,
filterable: false,
resizable: false,
searchable: false,
},
];
/**
* The search task.
*
* @void
*/
*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;
}
/**
* Reload layout view.
*/
reload() {
return this.hostRouter.refresh();
}
/**
* Toggles dialog to export `contact`
*
* @void
*/
exportContacts() {
const selections = this.table.selectedRows.map((_) => _.id);
this.crud.export('contact', { params: { selections } });
}
/**
* View a `contact` details in modal
*
* @param {ContactModel} contact
* @void
*/
viewContact(contact) {
return this.transitionToRoute('management.contacts.index.details', contact);
}
/**
* Create a new `contact` in modal
*
* @void
*/
createContact() {
return this.transitionToRoute('management.contacts.index.new');
}
/**
* Edit a `contact` details
*
* @param {ContactModel} contact
* @void
*/
editContact(contact) {
return this.transitionToRoute('management.contacts.index.edit', contact);
}
/**
* Delete a `contact` via confirm prompt
*
* @param {ContactModel} contact
* @param {Object} options
* @void
*/
deleteContact(contact, options = {}) {
this.crud.delete(contact, {
acceptButtonIcon: 'trash',
onConfirm: () => {
this.hostRouter.refresh();
},
...options,
});
}
/**
* Bulk deletes selected `contacts` via confirm prompt
*
* @param {Array} selected an array of selected models
* @void
*/
bulkDeleteContacts() {
const selected = this.table.selectedRows;
this.crud.bulkDelete(selected, {
modelNamePath: `name`,
acceptButtonText: this.intl.t('fleet-ops.management.contacts.index.delete-button'),
onSuccess: () => {
return this.hostRouter.refresh();
},
});
}
}