mirror of
https://github.com/grafana/grafana.git
synced 2025-02-15 01:53:33 -06:00
* WIP: intial structure * Refactor: adds create library element endpoint * Feature: adds delete library element * wip * Refactor: adds get api * Refactor: adds get all api * Refactor: adds patch api * Refactor: changes to library_element_connection * Refactor: add get connections api * wip: in the middle of refactor * wip * Refactor: consolidating both api:s * Refactor: points front end to library elements api * Tests: Fixes broken test * Fix: fixes delete library elements in folder and adds tests * Refactor: changes order of tabs in manage folder * Refactor: fixes so link does not cover whole card * Update pkg/services/libraryelements/libraryelements.go Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com> * Update pkg/services/libraryelements/libraryelements_permissions_test.go Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com> * Update pkg/services/libraryelements/database.go Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com> * Chore: changes after PR comments * Update libraryelements.go * Chore: updates after PR comments Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
61 lines
2.0 KiB
TypeScript
61 lines
2.0 KiB
TypeScript
import { LibraryElementDTO, PanelModelLibraryPanel } from './types';
|
|
import { PanelModel } from '../dashboard/state';
|
|
import { addLibraryPanel, updateLibraryPanel } from './state/api';
|
|
import { createErrorNotification, createSuccessNotification } from '../../core/copy/appNotification';
|
|
import { AppNotification } from '../../types';
|
|
|
|
export function createPanelLibraryErrorNotification(message: string): AppNotification {
|
|
return createErrorNotification(message);
|
|
}
|
|
|
|
export function createPanelLibrarySuccessNotification(message: string): AppNotification {
|
|
return createSuccessNotification(message);
|
|
}
|
|
|
|
export function toPanelModelLibraryPanel(libraryPanelDto: LibraryElementDTO): PanelModelLibraryPanel {
|
|
const { uid, name, meta, version } = libraryPanelDto;
|
|
return { uid, name, meta, version };
|
|
}
|
|
|
|
export async function saveAndRefreshLibraryPanel(panel: PanelModel, folderId: number): Promise<LibraryElementDTO> {
|
|
const panelSaveModel = toPanelSaveModel(panel);
|
|
const savedPanel = await saveOrUpdateLibraryPanel(panelSaveModel, folderId);
|
|
updatePanelModelWithUpdate(panel, savedPanel);
|
|
return savedPanel;
|
|
}
|
|
|
|
function toPanelSaveModel(panel: PanelModel): any {
|
|
let panelSaveModel = panel.getSaveModel();
|
|
panelSaveModel = {
|
|
libraryPanel: {
|
|
name: panel.title,
|
|
uid: undefined,
|
|
},
|
|
...panelSaveModel,
|
|
};
|
|
|
|
return panelSaveModel;
|
|
}
|
|
|
|
function updatePanelModelWithUpdate(panel: PanelModel, updated: LibraryElementDTO): void {
|
|
panel.restoreModel({
|
|
...updated.model,
|
|
configRev: 0, // reset config rev, since changes have been saved
|
|
libraryPanel: toPanelModelLibraryPanel(updated),
|
|
});
|
|
panel.refresh();
|
|
}
|
|
|
|
function saveOrUpdateLibraryPanel(panel: any, folderId: number): Promise<LibraryElementDTO> {
|
|
if (!panel.libraryPanel) {
|
|
return Promise.reject();
|
|
}
|
|
|
|
if (panel.libraryPanel && panel.libraryPanel.uid === undefined) {
|
|
panel.libraryPanel.name = panel.title;
|
|
return addLibraryPanel(panel, folderId!);
|
|
}
|
|
|
|
return updateLibraryPanel(panel, folderId!);
|
|
}
|