actions.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. SetSearchQuery = 'SET_ALERT_SEARCH_QUERY',
  7. }
  8. export interface LoadAlertRulesAction {
  9. type: ActionTypes.LoadAlertRules;
  10. payload: AlertRuleDTO[];
  11. }
  12. export interface SetSearchQueryAction {
  13. type: ActionTypes.SetSearchQuery;
  14. payload: string;
  15. }
  16. export const loadAlertRules = (rules: AlertRuleDTO[]): LoadAlertRulesAction => ({
  17. type: ActionTypes.LoadAlertRules,
  18. payload: rules,
  19. });
  20. export const setSearchQuery = (query: string): SetSearchQueryAction => ({
  21. type: ActionTypes.SetSearchQuery,
  22. payload: query,
  23. });
  24. export type Action = LoadAlertRulesAction | SetSearchQueryAction;
  25. type ThunkResult<R> = ThunkAction<R, StoreState, undefined, Action>;
  26. export function getAlertRulesAsync(options: { state: string }): ThunkResult<void> {
  27. return async dispatch => {
  28. const rules = await getBackendSrv().get('/api/alerts', options);
  29. dispatch(loadAlertRules(rules));
  30. };
  31. }
  32. export function togglePauseAlertRule(id: number, options: { paused: boolean }): ThunkResult<void> {
  33. return async (dispatch, getState) => {
  34. await getBackendSrv().post(`/api/alerts/${id}/pause`, options);
  35. const stateFilter = getState().location.query.state || 'all';
  36. dispatch(getAlertRulesAsync({ state: stateFilter.toString() }));
  37. };
  38. }