Folders: Prevents deletion of General folder (#40192) (#40200)

(cherry picked from commit 70e759e0c0)

Co-authored-by: Hugo Häggmark <hugo.haggmark@grafana.com>
This commit is contained in:
Grot (@grafanabot)
2021-10-08 11:48:26 +02:00
committed by GitHub
co-authored by Hugo Häggmark
parent 37fcce3940
commit 4facd4aedf
2 changed files with 113 additions and 11 deletions
@@ -53,12 +53,83 @@ describe('useManageDashboards', () => {
});
describe('when called and General folder and another folder are selected', () => {
it('then canDelete should be true', () => {
it('then canDelete should be false', () => {
const results: DashboardSection[] = [
{ id: 1, checked: true, items: [], title: 'One', type: DashboardSearchItemType.DashFolder, toggle, url: '/' },
{
id: 1,
checked: true,
items: [
{
id: 11,
checked: true,
title: 'Eleven',
type: DashboardSearchItemType.DashDB,
url: '/',
isStarred: false,
tags: [],
uri: '',
},
],
title: 'One',
type: DashboardSearchItemType.DashFolder,
toggle,
url: '/',
},
{
id: GENERAL_FOLDER_ID,
checked: true,
items: [
{
id: 10,
checked: true,
title: 'Ten',
type: DashboardSearchItemType.DashDB,
url: '/',
isStarred: false,
tags: [],
uri: '',
},
],
title: 'General',
type: DashboardSearchItemType.DashFolder,
toggle,
url: '/',
},
{ id: 2, checked: false, items: [], title: 'Two', type: DashboardSearchItemType.DashFolder, toggle, url: '/' },
];
const { result } = setupTestContext({ results });
expect(result.current.canDelete).toBe(false);
});
});
describe('when called on an empty General folder that is not selected but another folder is selected', () => {
it('then canDelete should be true', () => {
const results: DashboardSection[] = [
{
id: 1,
checked: true,
items: [
{
id: 11,
checked: true,
title: 'Eleven',
type: DashboardSearchItemType.DashDB,
url: '/',
isStarred: false,
tags: [],
uri: '',
},
],
title: 'One',
type: DashboardSearchItemType.DashFolder,
toggle,
url: '/',
},
{
id: GENERAL_FOLDER_ID,
checked: false,
items: [],
title: 'General',
type: DashboardSearchItemType.DashFolder,
@@ -74,6 +145,37 @@ describe('useManageDashboards', () => {
});
});
describe('when called on a non empty General folder that is not selected dashboard in General folder is selected', () => {
it('then canDelete should be true', () => {
const results: DashboardSection[] = [
{
id: GENERAL_FOLDER_ID,
checked: false,
items: [
{
id: 10,
checked: true,
title: 'Ten',
type: DashboardSearchItemType.DashDB,
url: '/',
isStarred: false,
tags: [],
uri: '',
},
],
title: 'General',
type: DashboardSearchItemType.DashFolder,
toggle,
url: '/',
},
];
const { result } = setupTestContext({ results });
expect(result.current.canDelete).toBe(true);
});
});
describe('when called and no folder is selected', () => {
it('then canDelete should be false', () => {
const results: DashboardSection[] = [
@@ -1,7 +1,7 @@
import { useCallback, useMemo, useReducer } from 'react';
import { FolderDTO } from 'app/types';
import { contextSrv } from 'app/core/services/context_srv';
import { DashboardQuery, DashboardSection, OnDeleteItems, OnMoveItems, OnToggleChecked } from '../types';
import { DashboardQuery, OnDeleteItems, OnMoveItems, OnToggleChecked } from '../types';
import { DELETE_ITEMS, MOVE_ITEMS, TOGGLE_ALL_CHECKED, TOGGLE_CHECKED } from '../reducers/actionTypes';
import { manageDashboardsReducer, manageDashboardsState, ManageDashboardsState } from '../reducers/manageDashboards';
import { useSearch } from './useSearch';
@@ -42,14 +42,14 @@ export const useManageDashboards = (
dispatch({ type: MOVE_ITEMS, payload: { dashboards: selectedDashboards, folder } });
};
const canMove = useMemo(
() => results.some((result: DashboardSection) => result.items && result.items.some((item) => item.checked)),
[results]
);
const canDelete = useMemo(
() => canMove || results.some((result: DashboardSection) => result.checked && result.id !== GENERAL_FOLDER_ID),
[canMove, results]
);
const canMove = useMemo(() => results.some((result) => result.items && result.items.some((item) => item.checked)), [
results,
]);
const canDelete = useMemo(() => {
const includesGeneralFolder = results.find((result) => result.checked && result.id === GENERAL_FOLDER_ID);
return canMove && !includesGeneralFolder;
}, [canMove, results]);
const canSave = folder?.canSave;
const hasEditPermissionInFolders = folder ? canSave : contextSrv.hasEditPermissionInFolders;