mirror of https://github.com/grafana/grafana.git
				
				
				
			Browse Dashboards: Reset cache when folder is changed during import (#107116)
* Browse Dashboards: Reset cache when folder is changed during import * Fetch dashboard data * Do not invalidate tags * Handle fetch error
This commit is contained in:
		
							parent
							
								
									e7af803906
								
							
						
					
					
						commit
						e0eff97d94
					
				|  | @ -2,7 +2,7 @@ import { createApi } from '@reduxjs/toolkit/query/react'; | ||||||
| 
 | 
 | ||||||
| import { AppEvents, isTruthy, locationUtil } from '@grafana/data'; | import { AppEvents, isTruthy, locationUtil } from '@grafana/data'; | ||||||
| import { t } from '@grafana/i18n'; | import { t } from '@grafana/i18n'; | ||||||
| import { config, getBackendSrv, locationService } from '@grafana/runtime'; | import { config, getBackendSrv, isFetchError, locationService } from '@grafana/runtime'; | ||||||
| import { Dashboard } from '@grafana/schema'; | import { Dashboard } from '@grafana/schema'; | ||||||
| import { Spec as DashboardV2Spec } from '@grafana/schema/dist/esm/schema/dashboard/v2alpha1/types.spec.gen'; | import { Spec as DashboardV2Spec } from '@grafana/schema/dist/esm/schema/dashboard/v2alpha1/types.spec.gen'; | ||||||
| import { folderAPIv1beta1 as folderAPI } from 'app/api/clients/folder/v1beta1'; | import { folderAPIv1beta1 as folderAPI } from 'app/api/clients/folder/v1beta1'; | ||||||
|  | @ -401,14 +401,47 @@ export const browseDashboardsAPI = createApi({ | ||||||
|           folderUid, |           folderUid, | ||||||
|         }, |         }, | ||||||
|       }), |       }), | ||||||
|       onQueryStarted: ({ folderUid }, { queryFulfilled, dispatch }) => { |       onQueryStarted: async ({ dashboard, folderUid }, { queryFulfilled, dispatch }) => { | ||||||
|  |         // Check if a dashboard with this UID already exists to find its current folder
 | ||||||
|  |         let currentFolderUid: string | undefined; | ||||||
|  |         if (dashboard.uid) { | ||||||
|  |           try { | ||||||
|  |             const existingDashboard = await getDashboardAPI().getDashboardDTO(dashboard.uid); | ||||||
|  |             currentFolderUid = isDashboardV2Resource(existingDashboard) | ||||||
|  |               ? existingDashboard.metadata?.name | ||||||
|  |               : existingDashboard.meta?.folderUid; | ||||||
|  |           } catch (error) { | ||||||
|  |             if (isFetchError(error)) { | ||||||
|  |               if (error.status !== 404) { | ||||||
|  |                 console.error('Error fetching dashboard', error); | ||||||
|  |               } else { | ||||||
|  |                 // Do not show the error alert if the dashboard does not exist
 | ||||||
|  |                 // this is expected when importing a new dashboard
 | ||||||
|  |                 error.isHandled = true; | ||||||
|  |               } | ||||||
|  |             } | ||||||
|  |           } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|         queryFulfilled.then(async (response) => { |         queryFulfilled.then(async (response) => { | ||||||
|  |           // Refresh destination folder
 | ||||||
|           dispatch( |           dispatch( | ||||||
|             refetchChildren({ |             refetchChildren({ | ||||||
|               parentUID: folderUid, |               parentUID: folderUid, | ||||||
|               pageSize: PAGE_SIZE, |               pageSize: PAGE_SIZE, | ||||||
|             }) |             }) | ||||||
|           ); |           ); | ||||||
|  | 
 | ||||||
|  |           // If the dashboard was moved from a different folder, refresh the source folder too
 | ||||||
|  |           if (currentFolderUid && currentFolderUid !== folderUid) { | ||||||
|  |             dispatch( | ||||||
|  |               refetchChildren({ | ||||||
|  |                 parentUID: currentFolderUid, | ||||||
|  |                 pageSize: PAGE_SIZE, | ||||||
|  |               }) | ||||||
|  |             ); | ||||||
|  |           } | ||||||
|  | 
 | ||||||
|           const dashboardUrl = locationUtil.stripBaseFromUrl(response.data.importedUrl); |           const dashboardUrl = locationUtil.stripBaseFromUrl(response.data.importedUrl); | ||||||
|           locationService.push(dashboardUrl); |           locationService.push(dashboardUrl); | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue