@lipagas/fleetops-engine
Version:
Fleet & Transport Management Extension for Fleetbase
185 lines (160 loc) • 4.69 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 { task } from 'ember-concurrency';
import getWithDefault from '@lipagas/ember-core/utils/get-with-default';
import contextComponentCallback from '@lipagas/ember-core/utils/context-component-callback';
import applyContextComponentArguments from '@lipagas/ember-core/utils/apply-context-component-arguments';
import getIssueTypes from '../utils/get-issue-types';
import getIssueCategories from '../utils/get-issue-categories';
export default class IssueFormPanelComponent extends Component {
/**
* @service store
*/
store;
/**
* @service fetch
*/
fetch;
/**
* @service intl
*/
intl;
/**
* @service notifications
*/
notifications;
/**
* @service hostRouter
*/
hostRouter;
/**
* @service contextPanel
*/
contextPanel;
/**
* Overlay context.
* @type {any}
*/
context;
/**
* All possible issue types
*
* @var {String}
*/
issueTypes = getIssueTypes();
/**
* The subcategories for issue types.
*
* @var {Object}
*/
issueCategoriesByType = getIssueCategories({ fullObject: true });
/**
* Selectable issue categories.
*
* @memberof IssueFormPanelComponent
*/
issueCategories = [];
/**
* Issue status options.
*
* @memberof IssueFormPanelComponent
*/
issueStatusOptions = ['pending', 'in-progress', 'backlogged', 'requires-update', 'in-review', 're-opened', 'duplicate', 'pending-review', 'escalated', 'completed', 'canceled'];
/**
* Issue priorty options.
*
* @memberof IssueFormPanelComponent
*/
issuePriorityOptions = ['low', 'medium', 'high', 'critical', 'scheduled-maintenance', 'operational-suggestion'];
/**
* Constructs the component and applies initial state.
*/
constructor() {
super(...arguments);
this.issue = this.args.issue;
this.issueCategories = getWithDefault(this.issueCategoriesByType, getWithDefault(this.issue, 'type', 'operational'), []);
applyContextComponentArguments(this);
}
/**
* Sets the overlay context.
*
* @action
* @param {OverlayContextObject} overlayContext
*/
setOverlayContext(overlayContext) {
this.context = overlayContext;
contextComponentCallback(this, 'onLoad', ...arguments);
}
/**
* Task to save issue.
*
* @return {void}
* @memberof IssueFormPanelComponent
*/
*save() {
contextComponentCallback(this, 'onBeforeSave', this.issue);
try {
this.issue = yield this.issue.save();
} catch (error) {
this.notifications.serverError(error);
return;
}
this.notifications.success(this.intl.t('fleet-ops.component.issue-form-panel.success-message', { publicId: this.issue.public_id }));
contextComponentCallback(this, 'onAfterSave', this.issue);
}
/**
* Trigger when the issue type is selected.
*
* @param {String} type
* @memberof IssueFormPanelComponent
*/
onSelectIssueType(type) {
this.issue.type = type;
this.issue.category = null;
this.issueCategories = getWithDefault(this.issueCategoriesByType, type, []);
}
/**
* Add a tag to the issue
*
* @param {String} tag
* @memberof IssueFormPanelComponent
*/
addTag(tag) {
if (!isArray(this.issue.tags)) {
this.issue.tags = [];
}
this.issue.tags.pushObject(tag);
}
/**
* Remove a tag from the issue tags.
*
* @param {Number} index
* @memberof IssueFormPanelComponent
*/
removeTag(index) {
this.issue.tags.removeAt(index);
}
/**
* View the details of the issue.
*
* @action
*/
onViewDetails() {
const isActionOverrided = contextComponentCallback(this, 'onViewDetails', this.issue);
if (!isActionOverrided) {
this.contextPanel.focus(this.issue, 'viewing');
}
}
/**
* Handles cancel button press.
*
* @action
* @returns {any}
*/
onPressCancel() {
return contextComponentCallback(this, 'onPressCancel', this.issue);
}
}