actions.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { StoreState } from 'app/types';
  2. import { ThunkAction } from 'redux-thunk';
  3. import { getBackendSrv } from '../../../core/services/backend_srv';
  4. import { LayoutMode } from '../../../core/components/LayoutSelector/LayoutSelector';
  5. import { PluginDashboard } from '../../../types/plugins';
  6. import { PluginMeta } from '@grafana/ui';
  7. export enum ActionTypes {
  8. LoadPlugins = 'LOAD_PLUGINS',
  9. LoadPluginDashboards = 'LOAD_PLUGIN_DASHBOARDS',
  10. LoadedPluginDashboards = 'LOADED_PLUGIN_DASHBOARDS',
  11. SetPluginsSearchQuery = 'SET_PLUGIN_SEARCH_QUERY',
  12. SetLayoutMode = 'SET_LAYOUT_MODE',
  13. }
  14. export interface LoadPluginsAction {
  15. type: ActionTypes.LoadPlugins;
  16. payload: PluginMeta[];
  17. }
  18. export interface LoadPluginDashboardsAction {
  19. type: ActionTypes.LoadPluginDashboards;
  20. }
  21. export interface LoadedPluginDashboardsAction {
  22. type: ActionTypes.LoadedPluginDashboards;
  23. payload: PluginDashboard[];
  24. }
  25. export interface SetPluginsSearchQueryAction {
  26. type: ActionTypes.SetPluginsSearchQuery;
  27. payload: string;
  28. }
  29. export interface SetLayoutModeAction {
  30. type: ActionTypes.SetLayoutMode;
  31. payload: LayoutMode;
  32. }
  33. export const setPluginsLayoutMode = (mode: LayoutMode): SetLayoutModeAction => ({
  34. type: ActionTypes.SetLayoutMode,
  35. payload: mode,
  36. });
  37. export const setPluginsSearchQuery = (query: string): SetPluginsSearchQueryAction => ({
  38. type: ActionTypes.SetPluginsSearchQuery,
  39. payload: query,
  40. });
  41. const pluginsLoaded = (plugins: PluginMeta[]): LoadPluginsAction => ({
  42. type: ActionTypes.LoadPlugins,
  43. payload: plugins,
  44. });
  45. const pluginDashboardsLoad = (): LoadPluginDashboardsAction => ({
  46. type: ActionTypes.LoadPluginDashboards,
  47. });
  48. const pluginDashboardsLoaded = (dashboards: PluginDashboard[]): LoadedPluginDashboardsAction => ({
  49. type: ActionTypes.LoadedPluginDashboards,
  50. payload: dashboards,
  51. });
  52. export type Action =
  53. | LoadPluginsAction
  54. | LoadPluginDashboardsAction
  55. | LoadedPluginDashboardsAction
  56. | SetPluginsSearchQueryAction
  57. | SetLayoutModeAction;
  58. type ThunkResult<R> = ThunkAction<R, StoreState, undefined, Action>;
  59. export function loadPlugins(): ThunkResult<void> {
  60. return async dispatch => {
  61. const result = await getBackendSrv().get('api/plugins', { embedded: 0 });
  62. dispatch(pluginsLoaded(result));
  63. };
  64. }
  65. export function loadPluginDashboards(): ThunkResult<void> {
  66. return async (dispatch, getStore) => {
  67. dispatch(pluginDashboardsLoad());
  68. const dataSourceType = getStore().dataSources.dataSource.type;
  69. const response = await getBackendSrv().get(`api/plugins/${dataSourceType}/dashboards`);
  70. dispatch(pluginDashboardsLoaded(response));
  71. };
  72. }