mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -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>
92 lines
3.0 KiB
TypeScript
92 lines
3.0 KiB
TypeScript
import React, { FC, useEffect, useMemo, useReducer } from 'react';
|
|
import { Button, Modal, useStyles } from '@grafana/ui';
|
|
import { LoadingState } from '@grafana/data';
|
|
|
|
import { LibraryElementDTO } from '../../types';
|
|
import { asyncDispatcher } from '../LibraryPanelsView/actions';
|
|
import { deleteLibraryPanelModalReducer, initialDeleteLibraryPanelModalState } from './reducer';
|
|
import { getConnectedDashboards } from './actions';
|
|
import { getModalStyles } from '../../styles';
|
|
|
|
interface Props {
|
|
libraryPanel: LibraryElementDTO;
|
|
onConfirm: () => void;
|
|
onDismiss: () => void;
|
|
}
|
|
|
|
export const DeleteLibraryPanelModal: FC<Props> = ({ libraryPanel, onDismiss, onConfirm }) => {
|
|
const styles = useStyles(getModalStyles);
|
|
const [{ dashboardTitles, loadingState }, dispatch] = useReducer(
|
|
deleteLibraryPanelModalReducer,
|
|
initialDeleteLibraryPanelModalState
|
|
);
|
|
const asyncDispatch = useMemo(() => asyncDispatcher(dispatch), [dispatch]);
|
|
useEffect(() => {
|
|
asyncDispatch(getConnectedDashboards(libraryPanel));
|
|
}, [asyncDispatch, libraryPanel]);
|
|
const connected = Boolean(dashboardTitles.length);
|
|
const done = loadingState === LoadingState.Done;
|
|
|
|
return (
|
|
<Modal className={styles.modal} title="Delete library panel" icon="trash-alt" onDismiss={onDismiss} isOpen={true}>
|
|
{!done ? <LoadingIndicator /> : null}
|
|
{done ? (
|
|
<div>
|
|
{connected ? <HasConnectedDashboards dashboardTitles={dashboardTitles} /> : null}
|
|
{!connected ? <Confirm /> : null}
|
|
|
|
<Modal.ButtonRow>
|
|
<Button variant="secondary" onClick={onDismiss} fill="outline">
|
|
Cancel
|
|
</Button>
|
|
<Button variant="destructive" onClick={onConfirm} disabled={connected}>
|
|
Delete
|
|
</Button>
|
|
</Modal.ButtonRow>
|
|
</div>
|
|
) : null}
|
|
</Modal>
|
|
);
|
|
};
|
|
|
|
const LoadingIndicator: FC = () => <span>Loading library panel...</span>;
|
|
|
|
const Confirm: FC = () => {
|
|
const styles = useStyles(getModalStyles);
|
|
|
|
return <div className={styles.modalText}>Do you want to delete this panel?</div>;
|
|
};
|
|
|
|
const HasConnectedDashboards: FC<{ dashboardTitles: string[] }> = ({ dashboardTitles }) => {
|
|
const styles = useStyles(getModalStyles);
|
|
const suffix = dashboardTitles.length === 1 ? 'dashboard.' : 'dashboards.';
|
|
const message = `${dashboardTitles.length} ${suffix}`;
|
|
if (dashboardTitles.length === 0) {
|
|
return null;
|
|
}
|
|
|
|
return (
|
|
<div>
|
|
<p className={styles.textInfo}>
|
|
{'This library panel can not be deleted because it is connected to '}
|
|
<strong>{message}</strong>
|
|
{' Remove the library panel from the dashboards listed below and retry.'}
|
|
</p>
|
|
<table className={styles.myTable}>
|
|
<thead>
|
|
<tr>
|
|
<th>Dashboard name</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{dashboardTitles.map((title, i) => (
|
|
<tr key={`dash-title-${i}`}>
|
|
<td>{title}</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
);
|
|
};
|