2021-03-02 09:37:36 -06:00
|
|
|
import { createErrorNotification, createSuccessNotification } from '../../core/copy/appNotification';
|
|
|
|
import { AppNotification } from '../../types';
|
2022-04-22 08:33:13 -05:00
|
|
|
import { PanelModel } from '../dashboard/state';
|
|
|
|
|
|
|
|
import { addLibraryPanel, updateLibraryPanel } from './state/api';
|
2022-10-26 17:38:20 -05:00
|
|
|
import { LibraryElementDTO } from './types';
|
2021-03-02 09:37:36 -06:00
|
|
|
|
|
|
|
export function createPanelLibraryErrorNotification(message: string): AppNotification {
|
|
|
|
return createErrorNotification(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createPanelLibrarySuccessNotification(message: string): AppNotification {
|
|
|
|
return createSuccessNotification(message);
|
|
|
|
}
|
|
|
|
|
2022-11-17 02:22:57 -06:00
|
|
|
export async function saveAndRefreshLibraryPanel(panel: PanelModel, folderUid: string): Promise<LibraryElementDTO> {
|
2021-03-02 09:37:36 -06:00
|
|
|
const panelSaveModel = toPanelSaveModel(panel);
|
2022-11-17 02:22:57 -06:00
|
|
|
const savedPanel = await saveOrUpdateLibraryPanel(panelSaveModel, folderUid);
|
2021-03-02 09:37:36 -06:00
|
|
|
updatePanelModelWithUpdate(panel, savedPanel);
|
|
|
|
return savedPanel;
|
|
|
|
}
|
|
|
|
|
|
|
|
function toPanelSaveModel(panel: PanelModel): any {
|
|
|
|
let panelSaveModel = panel.getSaveModel();
|
|
|
|
panelSaveModel = {
|
|
|
|
libraryPanel: {
|
|
|
|
name: panel.title,
|
|
|
|
uid: undefined,
|
|
|
|
},
|
|
|
|
...panelSaveModel,
|
|
|
|
};
|
|
|
|
|
|
|
|
return panelSaveModel;
|
|
|
|
}
|
|
|
|
|
2021-05-11 00:10:19 -05:00
|
|
|
function updatePanelModelWithUpdate(panel: PanelModel, updated: LibraryElementDTO): void {
|
2021-03-02 09:37:36 -06:00
|
|
|
panel.restoreModel({
|
|
|
|
...updated.model,
|
2021-04-27 09:14:21 -05:00
|
|
|
configRev: 0, // reset config rev, since changes have been saved
|
2022-10-26 17:38:20 -05:00
|
|
|
libraryPanel: updated,
|
2021-09-01 06:27:43 -05:00
|
|
|
title: panel.title,
|
2021-03-02 09:37:36 -06:00
|
|
|
});
|
2022-09-06 12:00:32 -05:00
|
|
|
panel.hasSavedPanelEditChange = true;
|
2021-03-02 09:37:36 -06:00
|
|
|
panel.refresh();
|
|
|
|
}
|
|
|
|
|
2022-11-17 02:22:57 -06:00
|
|
|
function saveOrUpdateLibraryPanel(panel: any, folderUid: string): Promise<LibraryElementDTO> {
|
2021-03-02 09:37:36 -06:00
|
|
|
if (!panel.libraryPanel) {
|
|
|
|
return Promise.reject();
|
|
|
|
}
|
|
|
|
|
2022-10-26 17:38:20 -05:00
|
|
|
if (panel.libraryPanel && panel.libraryPanel.uid === '') {
|
2022-11-17 02:22:57 -06:00
|
|
|
return addLibraryPanel(panel, folderUid!);
|
2021-03-02 09:37:36 -06:00
|
|
|
}
|
|
|
|
|
2021-09-08 23:21:31 -05:00
|
|
|
return updateLibraryPanel(panel);
|
2021-03-02 09:37:36 -06:00
|
|
|
}
|