actions.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { getBackendSrv } from 'app/core/services/backend_srv';
  2. import { AlertRuleDTO, StoreState } from 'app/types';
  3. import { ThunkAction } from 'redux-thunk';
  4. export enum ActionTypes {
  5. LoadAlertRules = 'LOAD_ALERT_RULES',
  6. LoadedAlertRules = 'LOADED_ALERT_RULES',
  7. SetSearchQuery = 'SET_ALERT_SEARCH_QUERY',
  8. }
  9. export interface LoadAlertRulesAction {
  10. type: ActionTypes.LoadAlertRules;
  11. }
  12. export interface LoadedAlertRulesAction {
  13. type: ActionTypes.LoadedAlertRules;
  14. payload: AlertRuleDTO[];
  15. }
  16. export interface SetSearchQueryAction {
  17. type: ActionTypes.SetSearchQuery;
  18. payload: string;
  19. }
  20. export const loadAlertRules = (): LoadAlertRulesAction => ({
  21. type: ActionTypes.LoadAlertRules,
  22. });
  23. export const loadedAlertRules = (rules: AlertRuleDTO[]): LoadedAlertRulesAction => ({
  24. type: ActionTypes.LoadedAlertRules,
  25. payload: rules,
  26. });
  27. export const setSearchQuery = (query: string): SetSearchQueryAction => ({
  28. type: ActionTypes.SetSearchQuery,
  29. payload: query,
  30. });
  31. export type Action = LoadAlertRulesAction | LoadedAlertRulesAction | SetSearchQueryAction;
  32. type ThunkResult<R> = ThunkAction<R, StoreState, undefined, Action>;
  33. export function getAlertRulesAsync(options: { state: string }): ThunkResult<void> {
  34. return async dispatch => {
  35. dispatch(loadAlertRules());
  36. const rules: AlertRuleDTO[] = await getBackendSrv().get('/api/alerts', options);
  37. dispatch(loadedAlertRules(rules));
  38. };
  39. }
  40. export function togglePauseAlertRule(id: number, options: { paused: boolean }): ThunkResult<void> {
  41. return async (dispatch, getState) => {
  42. await getBackendSrv().post(`/api/alerts/${id}/pause`, options);
  43. const stateFilter = getState().location.query.state || 'all';
  44. dispatch(getAlertRulesAsync({ state: stateFilter.toString() }));
  45. };
  46. }