ConfirmDeleteModal.tsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { css } from '@emotion/css';
  2. import React, { FC } from 'react';
  3. import { GrafanaTheme } from '@grafana/data';
  4. import { ConfirmModal, stylesFactory, useTheme } from '@grafana/ui';
  5. import { deleteFoldersAndDashboards } from 'app/features/manage-dashboards/state/actions';
  6. import { DashboardSection, OnDeleteItems } from '../types';
  7. import { getCheckedUids } from '../utils';
  8. interface Props {
  9. onDeleteItems: OnDeleteItems;
  10. results: DashboardSection[];
  11. isOpen: boolean;
  12. onDismiss: () => void;
  13. }
  14. export const ConfirmDeleteModal: FC<Props> = ({ results, onDeleteItems, isOpen, onDismiss }) => {
  15. const theme = useTheme();
  16. const styles = getStyles(theme);
  17. const uids = getCheckedUids(results);
  18. const { folders, dashboards } = uids;
  19. const folderCount = folders.length;
  20. const dashCount = dashboards.length;
  21. let text = 'Do you want to delete the ';
  22. let subtitle;
  23. const dashEnding = dashCount === 1 ? '' : 's';
  24. const folderEnding = folderCount === 1 ? '' : 's';
  25. if (folderCount > 0 && dashCount > 0) {
  26. text += `selected folder${folderEnding} and dashboard${dashEnding}?\n`;
  27. subtitle = `All dashboards and alerts of the selected folder${folderEnding} will also be deleted`;
  28. } else if (folderCount > 0) {
  29. text += `selected folder${folderEnding} and all ${folderCount === 1 ? 'its' : 'their'} dashboards and alerts?`;
  30. } else {
  31. text += `selected dashboard${dashEnding}?`;
  32. }
  33. const deleteItems = () => {
  34. deleteFoldersAndDashboards(folders, dashboards).then(() => {
  35. onDismiss();
  36. onDeleteItems(folders, dashboards);
  37. });
  38. };
  39. return isOpen ? (
  40. <ConfirmModal
  41. isOpen={isOpen}
  42. title="Delete"
  43. body={
  44. <>
  45. {text} {subtitle && <div className={styles.subtitle}>{subtitle}</div>}
  46. </>
  47. }
  48. confirmText="Delete"
  49. onConfirm={deleteItems}
  50. onDismiss={onDismiss}
  51. />
  52. ) : null;
  53. };
  54. const getStyles = stylesFactory((theme: GrafanaTheme) => {
  55. return {
  56. subtitle: css`
  57. font-size: ${theme.typography.size.base};
  58. padding-top: ${theme.spacing.md};
  59. `,
  60. };
  61. });