@ducna01120/fleetops-engine
Version:
Fleet & Transport Management Extension for Fleetbase
154 lines (131 loc) • 4.59 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 apiUrl from '@fleetbase/ember-core/utils/api-url';
import contextComponentCallback from '@fleetbase/ember-core/utils/context-component-callback';
export default class VendorFormPanelCreateFormComponent extends Component {
store;
fetch;
intl;
contextPanel;
notifications;
/**
* State of whether editing credentials is enabled.
* @type {Boolean}
*/
isEditingCredentials = false;
/**
* State of whether to show advanced options for integrated vendor.
* @type {Boolean}
*/
showAdvancedOptions = false;
/**
* The selectable types of vendors.
* @type {Array}
*/
vendorTypes = [
{ label: this.intl.t('fleet-ops.component.vendor-form-panel.create-form.integrated-vendor'), value: 'integrated-vendor' },
{ label: this.intl.t('fleet-ops.component.vendor-form-panel.create-form.custom-vendor'), value: 'vendor' },
];
/**
* The selected type of vendor being created or edited.
* @type {String}
*/
selectedVendorType = this.vendorTypes[1];
/**
* The supported integrated vendors.
* @type {Array}
*/
supportedIntegratedVendors = [];
/**
* The selected integrated vendor provider.
* @type {Object}
*/
selectedIntegratedVendor;
constructor() {
super(...arguments);
this.vendor = this.args.vendor;
this.fetchSupportedIntegratedVendors.perform();
}
toggleCredentialsReset() {
if (this.isEditingCredentials) {
this.isEditingCredentials = false;
} else {
this.isEditingCredentials = true;
}
}
toggleAdvancedOptions() {
if (this.showAdvancedOptions) {
this.showAdvancedOptions = false;
} else {
this.showAdvancedOptions = true;
}
}
onSelectVendorType(selectedVendorType) {
this.selectedVendorType = selectedVendorType;
}
onSelectIntegratedVendor(integratedVendor) {
this.selectedIntegratedVendor = integratedVendor;
const { credential_params, option_params } = integratedVendor;
// create credentials object
const credentials = {};
if (isArray(integratedVendor.credential_params)) {
for (let i = 0; i < integratedVendor.credential_params.length; i++) {
const param = integratedVendor.credential_params.objectAt(i);
credentials[param] = null;
}
}
// create options object
const options = {};
if (isArray(integratedVendor.option_params)) {
for (let i = 0; i < integratedVendor.option_params.length; i++) {
const param = integratedVendor.option_params.objectAt(i);
options[param.key] = null;
}
}
const vendor = this.store.createRecord('integrated-vendor', {
provider: integratedVendor.code,
webhook_url: apiUrl(`listeners/${integratedVendor.code}`),
credentials: {},
options: {},
credential_params,
option_params,
});
this.vendor = vendor;
// trigger callback
contextComponentCallback(this, 'onVendorChanged', vendor);
}
selectVendorAddress(place) {
this.vendor.place = place;
this.vendor.place_uuid = place.id;
}
async editAddress() {
let place;
if (this.vendor.has_place) {
place = await this.vendor.place;
} else {
place = this.store.createRecord('place');
}
return this.contextPanel.focus(place, 'editing', {
onAfterSave: (place) => {
this.selectVendorAddress(place);
this.contextPanel.clear();
},
});
}
/**
* Fetches the supported integrated vendors.
*
* @returns {Promise}
*/
*fetchSupportedIntegratedVendors() {
try {
this.supportedIntegratedVendors = yield this.fetch.get('integrated-vendors/supported');
} catch (error) {
this.notifications.serverError(error);
}
}
}