mirror of https://github.com/grafana/grafana
Nested folders: hook up move/delete logic properly (#67648)
* clear selection post move/delete * move actions out of rtk-query * move findItems, create selectors, refetch children when moving/deleting * cleaner syntax * remove unnecessary function, just put logic in the selector * handle moving/deleting from the root * slightly cleaner * handle when rootItems are undefined * handle 'general' in the fetchChildren reducer * only refresh at the end * don't need thunk apipull/67416/merge
parent
9b81d738bf
commit
02086e843f
@ -1,10 +1,49 @@ |
||||
import { createAsyncThunk } from '@reduxjs/toolkit'; |
||||
|
||||
import { getBackendSrv } from '@grafana/runtime'; |
||||
import { DeleteDashboardResponse } from 'app/features/manage-dashboards/types'; |
||||
import { GENERAL_FOLDER_UID } from 'app/features/search/constants'; |
||||
import { getFolderChildren } from 'app/features/search/service/folders'; |
||||
import { createAsyncThunk, DashboardDTO } from 'app/types'; |
||||
|
||||
export const fetchChildren = createAsyncThunk( |
||||
'browseDashboards/fetchChildren', |
||||
async (parentUID: string | undefined) => { |
||||
return await getFolderChildren(parentUID, undefined, true); |
||||
// Need to handle the case where the parentUID is the root
|
||||
const uid = parentUID === GENERAL_FOLDER_UID ? undefined : parentUID; |
||||
return await getFolderChildren(uid, undefined, true); |
||||
} |
||||
); |
||||
|
||||
export const deleteDashboard = createAsyncThunk('browseDashboards/deleteDashboard', async (dashboardUID: string) => { |
||||
return getBackendSrv().delete<DeleteDashboardResponse>(`/api/dashboards/uid/${dashboardUID}`); |
||||
}); |
||||
|
||||
export const deleteFolder = createAsyncThunk('browseDashboards/deleteFolder', async (folderUID: string) => { |
||||
return getBackendSrv().delete(`/api/folders/${folderUID}`, undefined, { |
||||
params: { forceDeleteRules: true }, |
||||
}); |
||||
}); |
||||
|
||||
export const moveDashboard = createAsyncThunk( |
||||
'browseDashboards/moveDashboard', |
||||
async ({ dashboardUID, destinationUID }: { dashboardUID: string; destinationUID: string }) => { |
||||
const fullDash: DashboardDTO = await getBackendSrv().get(`/api/dashboards/uid/${dashboardUID}`); |
||||
|
||||
const options = { |
||||
dashboard: fullDash.dashboard, |
||||
folderUid: destinationUID, |
||||
overwrite: false, |
||||
}; |
||||
|
||||
return getBackendSrv().post('/api/dashboards/db', { |
||||
message: '', |
||||
...options, |
||||
}); |
||||
} |
||||
); |
||||
|
||||
export const moveFolder = createAsyncThunk( |
||||
'browseDashboards/moveFolder', |
||||
async ({ folderUID, destinationUID }: { folderUID: string; destinationUID: string }) => { |
||||
return getBackendSrv().post(`/api/folders/${folderUID}/move`, { parentUID: destinationUID }); |
||||
} |
||||
); |
||||
|
@ -0,0 +1,28 @@ |
||||
import { DashboardViewItem } from 'app/features/search/types'; |
||||
|
||||
export function findItem( |
||||
rootItems: DashboardViewItem[], |
||||
childrenByUID: Record<string, DashboardViewItem[] | undefined>, |
||||
uid: string |
||||
): DashboardViewItem | undefined { |
||||
for (const item of rootItems) { |
||||
if (item.uid === uid) { |
||||
return item; |
||||
} |
||||
} |
||||
|
||||
for (const parentUID in childrenByUID) { |
||||
const children = childrenByUID[parentUID]; |
||||
if (!children) { |
||||
continue; |
||||
} |
||||
|
||||
for (const child of children) { |
||||
if (child.uid === uid) { |
||||
return child; |
||||
} |
||||
} |
||||
} |
||||
|
||||
return undefined; |
||||
} |
Loading…
Reference in new issue