actions.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { Plugin, 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. export enum ActionTypes {
  7. LoadPlugins = 'LOAD_PLUGINS',
  8. LoadPluginDashboards = 'LOAD_PLUGIN_DASHBOARDS',
  9. SetPluginsSearchQuery = 'SET_PLUGIN_SEARCH_QUERY',
  10. SetLayoutMode = 'SET_LAYOUT_MODE',
  11. }
  12. export interface LoadPluginsAction {
  13. type: ActionTypes.LoadPlugins;
  14. payload: Plugin[];
  15. }
  16. export interface LoadPluginDashboardsAction {
  17. type: ActionTypes.LoadPluginDashboards;
  18. payload: PluginDashboard[];
  19. }
  20. export interface SetPluginsSearchQueryAction {
  21. type: ActionTypes.SetPluginsSearchQuery;
  22. payload: string;
  23. }
  24. export interface SetLayoutModeAction {
  25. type: ActionTypes.SetLayoutMode;
  26. payload: LayoutMode;
  27. }
  28. export const setPluginsLayoutMode = (mode: LayoutMode): SetLayoutModeAction => ({
  29. type: ActionTypes.SetLayoutMode,
  30. payload: mode,
  31. });
  32. export const setPluginsSearchQuery = (query: string): SetPluginsSearchQueryAction => ({
  33. type: ActionTypes.SetPluginsSearchQuery,
  34. payload: query,
  35. });
  36. const pluginsLoaded = (plugins: Plugin[]): LoadPluginsAction => ({
  37. type: ActionTypes.LoadPlugins,
  38. payload: plugins,
  39. });
  40. const pluginDashboardsLoaded = (dashboards: PluginDashboard[]): LoadPluginDashboardsAction => ({
  41. type: ActionTypes.LoadPluginDashboards,
  42. payload: dashboards,
  43. });
  44. export type Action = LoadPluginsAction | LoadPluginDashboardsAction | SetPluginsSearchQueryAction | SetLayoutModeAction;
  45. type ThunkResult<R> = ThunkAction<R, StoreState, undefined, Action>;
  46. export function loadPlugins(): ThunkResult<void> {
  47. return async dispatch => {
  48. const result = await getBackendSrv().get('api/plugins', { embedded: 0 });
  49. dispatch(pluginsLoaded(result));
  50. };
  51. }
  52. export function loadPluginDashboards(): ThunkResult<void> {
  53. return async (dispatch, getStore) => {
  54. const dataSourceType = getStore().dataSources.dataSource.type;
  55. const response = await getBackendSrv().get(`api/plugins/${dataSourceType}/dashboards`);
  56. dispatch(pluginDashboardsLoaded(response));
  57. };
  58. }