Skip to content

Commit

Permalink
feat(edit-dashboard-json): [MTM-61842] Update dashboard refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jdre-c8y committed Dec 18, 2024
1 parent 67bb4bf commit 3085f18
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ import Ajv from 'ajv';
import { debounceTime } from 'rxjs';
import { Subject } from 'rxjs/internal/Subject';
import { takeUntil } from 'rxjs/operators';
import {
ContextDashboardManagedObject,
ContextDashboardService,
} from '@c8y/ngx-components/context-dashboard';
import { cloneDeep } from 'lodash-es';
import { ContextDashboardManagedObject } from '@c8y/ngx-components/context-dashboard';

function replacer(_key: any, value: any) {
// Filtering out properties
Expand Down Expand Up @@ -92,7 +88,6 @@ export class DashboardJsonEditorComponent implements OnInit, OnDestroy {
private validate$ = new Subject<string>();
private _close: ((value: string) => void) | undefined;
private modalRef = inject(BsModalRef);
private contextDashboardService = inject(ContextDashboardService);
private destroy$ = new Subject<void>();

ngOnInit(): void {
Expand All @@ -117,7 +112,6 @@ export class DashboardJsonEditorComponent implements OnInit, OnDestroy {
}

async onSave() {
await this.updateDashboard();
this._close!(this.valueString);
this.modalRef.hide();
}
Expand Down Expand Up @@ -155,20 +149,4 @@ export class DashboardJsonEditorComponent implements OnInit, OnDestroy {
this.widgetErrors.set(this.ajv.errorsText(validate?.errors).split(','));
}
}

private async updateDashboard() {
try {
const dashboardMO: ContextDashboardManagedObject = cloneDeep(
this.dashboardMO
);
dashboardMO.c8y_Dashboard = JSON.parse(this.valueString);

await this.contextDashboardService.update(
dashboardMO,
this.currentContext
);
} catch (_) {
// intended empty
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import {
IconDirective,
} from '@c8y/ngx-components';
import { ActivatedRoute, Router } from '@angular/router';
import { ContextDashboardManagedObject } from '@c8y/ngx-components/context-dashboard';
import {
ContextDashboardManagedObject,
ContextDashboardService,
} from '@c8y/ngx-components/context-dashboard';
import { BsModalService } from 'ngx-bootstrap/modal';
import { DashboardJsonEditorComponent } from '../dashboard-json-editor/dashboard-json-editor.component';
import { cloneDeep } from 'lodash-es';

@Component({
selector: 'c8y-edit-dashboard-json-button',
Expand Down Expand Up @@ -37,6 +41,7 @@ export class EditDashboardJsonButtonComponent implements OnInit {
private router = inject(Router);
private contextRouteService = inject(ContextRouteService);
private modalService = inject(BsModalService);
private contextDashboardService = inject(ContextDashboardService);

async editDashboardJSON() {
try {
Expand All @@ -52,6 +57,12 @@ export class EditDashboardJsonButtonComponent implements OnInit {
keyboard: false,
}).content as DashboardJsonEditorComponent;
const dashboardJSON = await modalRef.result;

if (this.dashboardMO) {
await this.updateDashboard(dashboardJSON);
} else {
// TODO: create new dashboard
}
console.log(dashboardJSON);

// TODO: find better way to refresh dashboard
Expand Down Expand Up @@ -94,4 +105,17 @@ export class EditDashboardJsonButtonComponent implements OnInit {
}
return route;
}

private async updateDashboard(updatedDashboard: string) {
try {
const dashboardMO: ContextDashboardManagedObject = cloneDeep(
this.dashboardMO
);
dashboardMO.c8y_Dashboard = JSON.parse(updatedDashboard);

await this.contextDashboardService.update(dashboardMO, this.contextData);
} catch (_) {
// intended empty
}
}
}

0 comments on commit 3085f18

Please sign in to comment.