index.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import {Component, Fragment} from 'react';
  2. import {Panel, PanelBody} from 'app/components/panels';
  3. import {Organization, Project} from 'app/types';
  4. import {removeAtArrayIndex} from 'app/utils/removeAtArrayIndex';
  5. import {replaceAtArrayIndex} from 'app/utils/replaceAtArrayIndex';
  6. import withProjects from 'app/utils/withProjects';
  7. import ActionsPanel from 'app/views/settings/incidentRules/triggers/actionsPanel';
  8. import TriggerForm from 'app/views/settings/incidentRules/triggers/form';
  9. import {
  10. Action,
  11. AlertRuleThresholdType,
  12. MetricActionTemplate,
  13. Trigger,
  14. UnsavedIncidentRule,
  15. } from '../types';
  16. type Props = {
  17. organization: Organization;
  18. projects: Project[];
  19. ruleId?: string;
  20. triggers: Trigger[];
  21. resolveThreshold: UnsavedIncidentRule['resolveThreshold'];
  22. thresholdType: UnsavedIncidentRule['thresholdType'];
  23. aggregate: UnsavedIncidentRule['aggregate'];
  24. currentProject: string;
  25. availableActions: MetricActionTemplate[] | null;
  26. disabled: boolean;
  27. errors: Map<number, {[fieldName: string]: string}>;
  28. onChange: (
  29. triggers: Trigger[],
  30. triggerIndex?: number,
  31. changeObj?: Partial<Trigger>
  32. ) => void;
  33. onThresholdTypeChange: (thresholdType: AlertRuleThresholdType) => void;
  34. onResolveThresholdChange: (
  35. resolveThreshold: UnsavedIncidentRule['resolveThreshold']
  36. ) => void;
  37. };
  38. /**
  39. * A list of forms to add, edit, and delete triggers.
  40. */
  41. class Triggers extends Component<Props> {
  42. handleDeleteTrigger = (index: number) => {
  43. const {triggers, onChange} = this.props;
  44. const updatedTriggers = removeAtArrayIndex(triggers, index);
  45. onChange(updatedTriggers);
  46. };
  47. handleChangeTrigger = (
  48. triggerIndex: number,
  49. trigger: Trigger,
  50. changeObj: Partial<Trigger>
  51. ) => {
  52. const {triggers, onChange} = this.props;
  53. const updatedTriggers = replaceAtArrayIndex(triggers, triggerIndex, trigger);
  54. onChange(updatedTriggers, triggerIndex, changeObj);
  55. };
  56. handleAddAction = (triggerIndex: number, action: Action) => {
  57. const {onChange, triggers} = this.props;
  58. const trigger = triggers[triggerIndex];
  59. const actions = [...trigger.actions, action];
  60. const updatedTriggers = replaceAtArrayIndex(triggers, triggerIndex, {
  61. ...trigger,
  62. actions,
  63. });
  64. onChange(updatedTriggers, triggerIndex, {actions});
  65. };
  66. handleChangeActions = (
  67. triggerIndex: number,
  68. triggers: Trigger[],
  69. actions: Action[]
  70. ): void => {
  71. const {onChange} = this.props;
  72. const trigger = triggers[triggerIndex];
  73. const updatedTriggers = replaceAtArrayIndex(triggers, triggerIndex, {
  74. ...trigger,
  75. actions,
  76. });
  77. onChange(updatedTriggers, triggerIndex, {actions});
  78. };
  79. render() {
  80. const {
  81. availableActions,
  82. currentProject,
  83. errors,
  84. organization,
  85. projects,
  86. triggers,
  87. disabled,
  88. aggregate,
  89. thresholdType,
  90. resolveThreshold,
  91. onThresholdTypeChange,
  92. onResolveThresholdChange,
  93. } = this.props;
  94. // Note we only support 2 triggers max
  95. return (
  96. <Fragment>
  97. <Panel>
  98. <PanelBody>
  99. <TriggerForm
  100. disabled={disabled}
  101. errors={errors}
  102. organization={organization}
  103. projects={projects}
  104. triggers={triggers}
  105. aggregate={aggregate}
  106. resolveThreshold={resolveThreshold}
  107. thresholdType={thresholdType}
  108. onChange={this.handleChangeTrigger}
  109. onThresholdTypeChange={onThresholdTypeChange}
  110. onResolveThresholdChange={onResolveThresholdChange}
  111. />
  112. </PanelBody>
  113. </Panel>
  114. <ActionsPanel
  115. disabled={disabled}
  116. loading={availableActions === null}
  117. error={false}
  118. availableActions={availableActions}
  119. currentProject={currentProject}
  120. organization={organization}
  121. projects={projects}
  122. triggers={triggers}
  123. onChange={this.handleChangeActions}
  124. onAdd={this.handleAddAction}
  125. />
  126. </Fragment>
  127. );
  128. }
  129. }
  130. export default withProjects(Triggers);