@lipagas/fleetops-engine
Version:
Fleet & Transport Management Extension for Fleetbase
298 lines (249 loc) • 10.4 kB
JavaScript
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { inject as service } from '@ember/service';
import { action, computed, set } from '@ember/object';
import { equal } from '@ember/object/computed';
import isModel from '@lipagas/ember-core/utils/is-model';
export default class OrderConfigComponent extends Component {
modalsManager;
fetch;
socket;
intl;
store;
notifications;
crud;
currentUser;
activeTab = 'details';
isDetailsTab;
isActivityTab;
isEntitiesTab;
selected;
isLoading = true;
isSaving = false;
configurations = [];
uninstallChannels = [];
get orderConfig() {
return this.configurations.find((config) => config.namespace === this.selected);
}
get orderConfigIndex() {
return this.configurations.findIndex((config) => config.namespace === this.selected);
}
get configFields() {
const defaultLogicFields = ['status', 'type', 'internal_id', 'id'];
const metaFields = Array.from(this.orderConfig.meta.fields ?? []).map((metaField) => `meta.${metaField.key}`);
return [...defaultLogicFields, ...metaFields];
}
setupComponent() {
this.loadConfigurations();
}
async loadConfigurations() {
this.configurations = await this.fetchConfigurations();
if (typeof this.args.onReady === 'function') {
this.args.onReady(this.configurations);
}
}
selectConfig(selected) {
this.selected = selected;
if (typeof this.args.onConfigChanged === 'function') {
this.args.onConfigChanged(this.orderConfig);
}
}
fetchConfigurations() {
this.isLoading = true;
return new Promise((resolve, reject) => {
this.fetch
.get('fleet-ops/order-configs/get-installed')
.then((configs) => {
const serialized = [];
for (let i = 0; i < configs.length; i++) {
const config = configs.objectAt(i);
const normalizedConfig = this.store.normalize('order-config', config);
const serializedConfig = this.store.push(normalizedConfig);
serialized.pushObject(serializedConfig);
}
if (typeof this.args.onConfigsLoaded === 'function') {
this.args.onConfigsLoaded(serialized);
}
this.isLoading = false;
resolve(serialized);
})
.catch((error) => {
this.isLoading = false;
reject(error);
});
});
}
createNewOrderConfig() {
const tags = [];
this.modalsManager.show('modals/new-order-config', {
title: this.intl.t('fleet-ops.component.order-config.create-new-title'),
acceptButtonIcon: 'check',
acceptButtonIconPrefix: 'fas',
declineButtonIcon: 'times',
declineButtonIconPrefix: 'fas',
name: null,
description: null,
tags,
addTag: (tag) => {
tags.pushObject(tag);
},
removeTag: (index) => {
tags.removeAt(index);
},
confirm: (modal) => {
modal.startLoading();
const { name, description, tags } = modal.getOptions();
if (!name) {
modal.stopLoading();
return this.notifications.warning(this.intl.t('fleet-ops.component.order-config.warning-message'));
}
return this.fetch
.post('fleet-ops/order-configs/new', { name, description, tags })
.then((newConfig) => {
const newConfigExtension = this.fetch.jsonToModel(newConfig, 'extension');
this.configurations.pushObject(newConfigExtension);
this.notifications.success(this.intl.t('fleet-ops.component.order-config.success-message'));
this.selectConfig(newConfigExtension.namespace);
})
.catch((error) => {
this.notifications.serverError(error);
});
},
});
}
saveConfig() {
const { orderConfig } = this;
if (!orderConfig) {
this.notifications.warning(this.intl.t('fleet-ops.component.order-config.no-order-warning'));
return;
}
this.isSaving = true;
this.fetch
.post(`fleet-ops/order-configs/save`, {
data: orderConfig,
})
.then(() => {
this.notifications.success(this.intl.t('fleet-ops.component.order-config.saved-success', { orderName: orderConfig.name }));
this.isSaving = false;
})
.catch((error) => {
this.notifications.serverError(error);
this.isSaving = false;
});
}
cloneConfiguration(extension) {
const name = `${extension.name} Clone`;
const description = extension.description;
this.modalsManager.show('modals/clone-config-form', {
title: this.intl.t('fleet-ops.component.order-config.enter-name-title'),
acceptButtonIcon: 'check',
acceptButtonIconPrefix: 'fas',
declineButtonIcon: 'times',
declineButtonIconPrefix: 'fas',
name,
description,
confirm: (modal) => {
modal.startLoading();
const { name, description } = modal.getOptions();
if (!name) {
return this.notifications.warning(this.intl.t('fleet-ops.component.order-config.no-config-warning'));
}
return this.fetch
.post('fleet-ops/order-configs/clone', {
id: extension.id,
installed: extension.installed,
name,
description,
})
.then((newConfig) => {
const clone = this.fetch.jsonToModel(newConfig, 'extension');
this.configurations.pushObject(clone);
this.notifications.success(this.intl.t('fleet-ops.component.order-config.cloned-success'));
this.selectConfig(clone.namespace);
})
.catch((error) => {
this.notifications.serverError(error);
});
},
});
}
deleteExtension(extension) {
const uninstall = isModel(extension) ? extension : this.fetch.jsonToModel(extension, 'extension');
return this.crud.delete(uninstall, {
body: this.intl.t('fleet-ops.component.order-config.body'),
acceptButtonIcon: 'trash',
declineButtonIcon: 'times',
declineButtonIconPrefix: 'fas',
onFinish: () => {
this.configurations.removeObject(extension);
this.selected = undefined;
},
});
}
uninstallConfiguration(extension) {
const extensionName = extension.display_name ?? extension.name;
this.listenForUninstallProgress(extension);
this.modalsManager.show('modals/uninstall-prompt', {
title: this.intl.t('fleet-ops.component.order-config.uninstall-title'),
acceptButtonText: 'Uninstall',
acceptButtonScheme: 'danger',
extension,
extensionName,
isUninstalling: false,
uninstallProgress: 0,
confirm: (modal, done) => {
modal.startLoading();
modal.setOption('isUninstalling', true);
extension
.uninstall()
.then(() => {
this.closeUninstallChannel();
this.notifications.success(this.intl.t('fleet-ops.component.order-config.uninstall-success', { extensionName: extensionName }));
this.configurations.removeObject(extension);
this.selected = undefined;
done();
})
.catch((error) => {
modal.stopLoading();
modal.setOption('isInstalling', false);
this.notifications.serverError(error);
});
},
decline: (modal) => {
this.closeUninstallChannel();
modal.done();
},
});
}
async listenForUninstallProgress(extension) {
// get channel identifier
const channelId = `${extension.id}:${this.currentUser.id}`;
// setup socket
const socket = this.socket.instance();
// get channel
const channel = socket.subscribe(channelId);
this.uninstallChannels = channel;
// listen to channel for events
await channel.listener('subscribe').once();
// get incoming data and console out
for await (let data of channel) {
if (data.progress) {
this.modalsManager.setOption('uninstallProgress', data.progress);
}
}
}
closeUninstallChannel() {
if (this.uninstallChannels) {
this.uninstallChannels.close();
}
}
setMetaFields(fields = []) {
set(this.orderConfig, 'meta.fields', fields);
}
setFlow(flow = {}) {
set(this.orderConfig, 'meta.flow', flow);
}
setEntities(entities = {}) {
set(this.orderConfig, 'meta.entities', entities);
}
}