Skip to content

Modular logistics and supply chain operating system (LSOS)

refactor dashboard components

Turtuvshinf9e6c8d50d66
diff --git a/console/app/components/dashboard.hbs b/console/app/components/dashboard.hbs
index 1f4a4d7f..e3257881 100644
--- a/console/app/components/dashboard.hbs
+++ b/console/app/components/dashboard.hbs
@@ -1,15 +1,15 @@
<div class="fleetbase-dashboard-grid flex items-center justify-between mx-2">
<div class="left-section">
{{! Dashboard Title Text }}
- <h1 class="text-lg font-bold">{{this.currentDashboard.name}}</h1>
+ <h1 class="text-lg font-bold">{{this.dashboard.currentDashboard.name}}</h1>
</div>
<div class="right-section ml-4 flex items-center">
{{! Select Dropdown }}
<div class="fleetbase-model-select fleetbase-power-select ember-model-select h-10">
<PowerSelect
- @options={{this.dashboards}}
- @selected={{this.currentDashboard}}
- @onChange={{fn this.selectDashboard}}
+ @options={{this.dashboard.dashboards}}
+ @selected={{this.dashboard.currentDashboard}}
+ @onChange={{this.selectDashboard}}
@placeholder="Select Dashboard"
@triggerClass="form-select form-input"
as |dashboard|
@@ -36,7 +36,7 @@
<span>Edit dashboard</span>
</a>
- <a href="javascript:;" class="next-dd-item" {{on "click" (dropdown-fn dd this.deleteDashboard this.currentDashboard)}}>
+ <a href="javascript:;" class="next-dd-item" {{on "click" (dropdown-fn dd this.deleteDashboard this.dashboard.currentDashboard)}}>
<div class="w-6">
<FaIcon @icon="trash" />
</div>
@@ -46,7 +46,7 @@
</div>
</DropdownButton>
</div>
- {{#if this.isEditingDashboard}}
+ {{#if this.dashboard.isEditingDashboard}}
<div class="ml-2 h-10">
<Button @type="magic" @icon="save" @helpText={{"Save Dashboard"}} @onClick={{fn this.onChangeEdit false}} class="h-10" />
</div>
@@ -54,7 +54,12 @@
</div>
</div>
-<Dashboard::Create @isEdit={{this.isEditingDashboard}} @dashboard={{this.currentDashboard}} />
-{{#if this.isEditingDashboard}}
- <Dashboard::WidgetPanel @isOpen={{this.isEditingDashboard}} @onLoad={{this.setWidgetSelectorPanelContext}} @dashboard={{this.currentDashboard}} @onClose={{fn this.onChangeEdit false}} />
+<Dashboard::Create @isEdit={{this.dashboard.isEditingDashboard}} @dashboard={{this.dashboard.currentDashboard}} />
+{{#if this.dashboard.isEditingDashboard}}
+ <Dashboard::WidgetPanel
+ @isOpen={{this.dashboard.isEditingDashboard}}
+ @onLoad={{this.setWidgetSelectorPanelContext}}
+ @dashboard={{this.dashboard.currentDashboard}}
+ @onClose={{fn this.onChangeEdit false}}
+ />
{{/if}}
\ No newline at end of file
diff --git a/console/app/components/dashboard.js b/console/app/components/dashboard.js
index ad46e982..baaf45a8 100644
--- a/console/app/components/dashboard.js
+++ b/console/app/components/dashboard.js
@@ -9,10 +9,7 @@ export default class DashboardComponent extends Component {
@service notifications;
@service modalsManager;
@service fetch;
-
- @tracked dashboards = [];
- @tracked currentDashboard;
- @tracked isEditingDashboard = false;
+ @service dashboard;
constructor() {
super(...arguments);
@@ -21,43 +18,15 @@ export default class DashboardComponent extends Component {
}
@task *loadDashboards() {
- try {
- this.dashboards = yield this.store.findAll('dashboard');
-
- if (this.dashboards.length > 0) {
- this.currentDashboard = this.dashboards.find((dashboard) => dashboard.is_default) || this.dashboards[0];
-
- if (this.currentDashboard?.widgets?.length === 0) {
- this.onChangeEdit(true);
- }
- console.log('Current Dashboard: ', this.currentDashboard);
- }
- } catch (error) {
- console.error('Error loading dashboards:', error);
- }
+ this.dashboard.loadDashboards.perform();
}
@action selectDashboard(dashboard) {
- console.log('Selected dashboard: ', dashboard);
- this.fetch
- .post('dashboards/switch', { dashboard_uuid: dashboard.id })
- .then((response) => {
- this.store.pushPayload(response);
- const selectedDashboardId = response.uuid;
- this.currentDashboard = this.store.peekRecord('dashboard', selectedDashboardId);
-
- if (this.currentDashboard?.widgets?.length === 0) {
- this.onChangeEdit(true);
- }
- })
- .finally(() => {
- this.isLoading = false;
- });
+ this.dashboard.selectDashboard.perform(dashboard);
}
@action setWidgetSelectorPanelContext(widgetSelectorContext) {
this.widgetSelectorContext = widgetSelectorContext;
- console.log('widgetSelectorContext', widgetSelectorContext);
}
@action onChangeEdit(state = true) {
@@ -68,79 +37,45 @@ export default class DashboardComponent extends Component {
this.modalsManager.show('modals/create-dashboard', {
title: `Create a new dashboard`,
acceptButtonText: 'Save Changes',
- confirm: (modal, done) => {
+ confirm: async (modal, done) => {
modal.startLoading();
// Get the name from the modal options
const { name } = modal.getOptions();
- const newDashboard = this.store.createRecord('dashboard', { name });
-
- newDashboard
- .save()
- .then((response) => {
- this.currentDashboard = response;
- this.isEditingDashboard = true;
- if (typeof options.successNotification === 'function') {
- this.notifications.success(options.successNotification(response));
- } else {
- this.notifications.success(options.successNotification || `${response.name} created.`);
- }
-
- done();
- })
- .catch((error) => {
- modal.stopLoading();
- this.notifications.serverError(error);
- });
+ await this.dashboard.createDashboard.perform(name);
+ done();
},
...options,
});
}
@action deleteDashboard(dashboard, options = {}) {
- if (this.dashboards.length === 1) {
+ if (this.dashboard.dashboards?.length === 1) {
this.notifications.error('You cannot delete the last dashboard.');
return;
}
this.modalsManager.confirm({
title: `Are you sure to delete this ${dashboard.name}?`,
- confirm: (modal) => {
+ confirm: async (modal, done) => {
if (typeof options.onConfirm === 'function') {
options.onConfirm(model);
}
modal.startLoading();
- return dashboard
- .destroyRecord()
- .then((model) => {
- this.notifications.success(options.successNotification || `${dashboard.name} has been deleted.`);
- if (typeof options.onSuccess === 'function') {
- options.onSuccess(model);
- }
- this.currentDashboard = this.dashboards[0];
- })
- .catch((error) => {
- this.notifications.serverError(error);
-
- if (typeof options.onError === 'function') {
- options.onError(error, model);
- }
- })
- .finally(() => {
- if (typeof options.callback === 'function') {
- options.callback(this.currentDashboard);
- }
- });
+ await this.dashboard.deleteDashboard.perform(dashboard);
+ done();
},
...options,
});
}
setCurrentDashboard(dashboard) {
- return this.fetch.post('dashboards/switch', { dashboard_uuid: dashboard.id }).then((response) => {
- this.currentDashboard = response;
- });
+ this.dashboard.setCurrentDashboard.perform(dashboard);
+ }
+
+ onChangeEdit(state = true) {
+ this.dashboard.onChangeEdit(state);
}
}
diff --git a/console/app/components/dashboard/create.js b/console/app/components/dashboard/create.js
index 09f22134..5e19d147 100644
--- a/console/app/components/dashboard/create.js
+++ b/console/app/components/dashboard/create.js
@@ -2,9 +2,12 @@ import { action, computed } from '@ember/object';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
+import { inject as service } from '@ember/service';
export default class DashboardCreateComponent extends Component {
@tracked dashboard;
+ @service notifications;
+
constructor(owner, args) {
super(...arguments);
@@ -28,7 +31,9 @@ export default class DashboardCreateComponent extends Component {
}
@action removeWidget(widget) {
- this.args.dashboard.removeWidget(widget.id);
+ this.args.dashboard.removeWidget(widget.id).catch((error) => {
+ this.notifications.serverError(error);
+ });
}
@computed('args.isEdit')
diff --git a/console/app/components/dashboard/widget-panel.js b/console/app/components/dashboard/widget-panel.js
index cbf11193..87a17fb6 100644
--- a/console/app/components/dashboard/widget-panel.js
+++ b/console/app/components/dashboard/widget-panel.js
@@ -8,6 +8,7 @@ export default class DashboardWidgetPanelComponent extends Component {
@tracked availableWidgets = [];
@tracked dashboard;
@tracked isOpen = true;
+ @service notifications;
/**
* Constructs the component and applies initial state.
*/
@@ -17,7 +18,7 @@ export default class DashboardWidgetPanelComponent extends Component {
this.availableWidgets = this.universe.getWidgets();
this.dashboard = dashboard;
- console.log(this.availableWidgets);
+ console.log(this.availableWidgets, this.dashboard);
}
/**
@@ -36,7 +37,9 @@ export default class DashboardWidgetPanelComponent extends Component {
@action addWidgetToDashboard(widget) {
console.log('Adding widget to dashboard: ', widget);
- this.args.dashboard.addWidget(widget);
+ this.args.dashboard.addWidget(widget).catch((error) => {
+ this.notifications.serverError(error);
+ });
}
/**
diff --git a/console/app/models/dashboard.js b/console/app/models/dashboard.js
index 28c4e9ae..3350af83 100644
--- a/console/app/models/dashboard.js
+++ b/console/app/models/dashboard.js
@@ -5,6 +5,7 @@ export default class DashboardModel extends Model {
@attr('string') uuid;
@attr('boolean') is_default;
@attr('string') name;
+ @attr('string') owner_uuid;
@hasMany('dashboard-widget') widgets;
addWidget(widget) {
@@ -14,9 +15,18 @@ export default class DashboardModel extends Model {
const widgetRecord = store.createRecord('dashboard-widget', widget);
widgetRecord.dashboard = this;
-
- widgetRecord.save().then((widgetRecord) => {
- this.widgets.pushObject(widgetRecord);
+ console.log(widgetRecord);
+ return new Promise((resolve, reject) => {
+ widgetRecord
+ .save()
+ .then((widgetRecord) => {
+ this.widgets.pushObject(widgetRecord);
+ resolve(widgetRecord);
+ })
+ .catch((error) => {
+ store.unloadRecord(widgetRecord);
+ reject(error);
+ });
});
}
@@ -27,14 +37,17 @@ export default class DashboardModel extends Model {
const widgetRecord = store.peekRecord('dashboard-widget', widget);
if (widgetRecord) {
- widgetRecord
- .destroyRecord()
- .then(() => {
- this.widgets.removeObject(widgetRecord);
- })
- .catch((error) => {
- console.error('Error removing widget:', error);
- });
+ return new Promise((resolve, reject) => {
+ widgetRecord
+ .destroyRecord()
+ .then(() => {
+ this.widgets.removeObject(widgetRecord);
+ resolve();
+ })
+ .catch((error) => {
+ reject(error);
+ });
+ });
}
}
}
diff --git a/console/app/services/dashboard.js b/console/app/services/dashboard.js
new file mode 100644
index 00000000..941c69cc
--- /dev/null
+++ b/console/app/services/dashboard.js
@@ -0,0 +1,98 @@
+// app/services/dashboard.js
+import Service from '@ember/service';
+import { inject as service } from '@ember/service';
+import { tracked } from '@glimmer/tracking';
+import { task } from 'ember-concurrency-decorators';
+import { action } from '@ember/object';
+
+export default class DashboardService extends Service {
+ @service store;
+ @service fetch;
+ @service notifications;
+
+ @tracked dashboards = [];
+ @tracked currentDashboard;
+ @tracked isEditingDashboard = false;
+
+ @task *loadDashboards() {
+ try {
+ this.dashboards = yield this.store.findAll('dashboard');
+
+ if (this.dashboards.length > 0) {
+ this.currentDashboard = this.dashboards.find((dashboard) => dashboard.is_default) || this.dashboards[0];
+
+ if (this.currentDashboard?.widgets?.length === 0) {
+ this.onChangeEdit(true);
+ }
+ }
+ } catch (error) {
+ console.error('Error loading dashboards:', error);
+ }
+ }
+
+ @task *selectDashboard(dashboard) {
+ try {
+ const response = yield this.fetch.post('dashboards/switch', { dashboard_uuid: dashboard.id });
+ this.store.pushPayload(response);
+ const selectedDashboardId = response.uuid;
+ this.currentDashboard = this.store.peekRecord('dashboard', selectedDashboardId);
+
+ if (this.currentDashboard?.widgets?.length === 0) {
+ this.onChangeEdit(true);
+ }
+ } catch (error) {
+ this.notifications.error(`Error switching dashboard: ${error}`);
+ }
+ }
+
+ @task *createDashboard(name, options = {}) {
+ try {
+ const newDashboard = this.store.createRecord('dashboard', { name, is_default: true });
+ const response = yield newDashboard.save();
+
+ if (typeof options.successNotification === 'function') {
+ this.notifications.success(options.successNotification(response));
+ } else {
+ this.notifications.success(options.successNotification || `${response.name} created.`);
+ }
+
+ const defaultDashboard = this.dashboards.find((dashboard) => dashboard.owner_uuid === 'system');
+ console.log(this.dashboards, defaultDashboard);
+ this.store.unloadRecord(defaultDashboard);
+ this.selectDashboard.perform(response);
+ this.isEditingDashboard = true;
+ } catch (error) {
+ this.notifications.serverError(error);
+ }
+ }
+
+ @task *deleteDashboard(dashboard, options = {}) {
+ try {
+ yield dashboard.destroyRecord();
+ this.notifications.success(options.successNotification || `${dashboard.name} has been deleted.`);
+ this.loadDashboards.perform();
+ } catch (error) {
+ this.notifications.serverError(error);
+ if (options.onError) {
+ options.onError(error, dashboard);
+ }
+ } finally {
+ if (options.callback) {
+ options.callback(this.currentDashboard);
+ }
+ }
+ }
+
+ @task *setCurrentDashboard(dashboard) {
+ try {
+ const response = yield this.fetch.post('dashboards/switch', { dashboard_uuid: dashboard.id });
+ this.currentDashboard = response;
+ } catch (error) {
+ this.notifications.error(`Error setting current dashboard: ${error}`);
+ }
+ }
+
+ @action onChangeEdit(state = true) {
+ this.isEditingDashboard = state;
+ }
+}
diff --git a/console/tests/unit/services/dashboard-test.js b/console/tests/unit/services/dashboard-test.js
new file mode 100644
index 00000000..e0ca0af8
--- /dev/null
+++ b/console/tests/unit/services/dashboard-test.js
@@ -0,0 +1,12 @@
+import { module, test } from 'qunit';
+import { setupTest } from '@fleetbase/console/tests/helpers';
+
+module('Unit | Service | dashboard', function (hooks) {
+ setupTest(hooks);
+
+ // TODO: Replace this with your real tests.
+ test('it exists', function (assert) {
+ let service = this.owner.lookup('service:dashboard');
+ assert.ok(service);
+ });
+});