EditCloudGroupModal.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { css } from '@emotion/css';
  2. import React, { useEffect, useMemo } from 'react';
  3. import { useDispatch } from 'react-redux';
  4. import { Modal, Button, Form, Field, Input, useStyles2 } from '@grafana/ui';
  5. import { useCleanup } from 'app/core/hooks/useCleanup';
  6. import { CombinedRuleGroup, CombinedRuleNamespace } from 'app/types/unified-alerting';
  7. import { useUnifiedAlertingSelector } from '../../hooks/useUnifiedAlertingSelector';
  8. import { updateLotexNamespaceAndGroupAction } from '../../state/actions';
  9. import { getRulesSourceName } from '../../utils/datasource';
  10. import { initialAsyncRequestState } from '../../utils/redux';
  11. import { durationValidationPattern } from '../../utils/time';
  12. interface Props {
  13. namespace: CombinedRuleNamespace;
  14. group: CombinedRuleGroup;
  15. onClose: () => void;
  16. }
  17. interface FormValues {
  18. namespaceName: string;
  19. groupName: string;
  20. groupInterval: string;
  21. }
  22. export function EditCloudGroupModal(props: Props): React.ReactElement {
  23. const { namespace, group, onClose } = props;
  24. const styles = useStyles2(getStyles);
  25. const dispatch = useDispatch();
  26. const { loading, error, dispatched } =
  27. useUnifiedAlertingSelector((state) => state.updateLotexNamespaceAndGroup) ?? initialAsyncRequestState;
  28. const defaultValues = useMemo(
  29. (): FormValues => ({
  30. namespaceName: namespace.name,
  31. groupName: group.name,
  32. groupInterval: group.interval ?? '',
  33. }),
  34. [namespace, group]
  35. );
  36. // close modal if successfully saved
  37. useEffect(() => {
  38. if (dispatched && !loading && !error) {
  39. onClose();
  40. }
  41. }, [dispatched, loading, onClose, error]);
  42. useCleanup((state) => state.unifiedAlerting.updateLotexNamespaceAndGroup);
  43. const onSubmit = (values: FormValues) => {
  44. dispatch(
  45. updateLotexNamespaceAndGroupAction({
  46. rulesSourceName: getRulesSourceName(namespace.rulesSource),
  47. groupName: group.name,
  48. newGroupName: values.groupName,
  49. namespaceName: namespace.name,
  50. newNamespaceName: values.namespaceName,
  51. groupInterval: values.groupInterval || undefined,
  52. })
  53. );
  54. };
  55. return (
  56. <Modal
  57. className={styles.modal}
  58. isOpen={true}
  59. title="Edit namespace or rule group"
  60. onDismiss={onClose}
  61. onClickBackdrop={onClose}
  62. >
  63. <Form defaultValues={defaultValues} onSubmit={onSubmit} key={JSON.stringify(defaultValues)}>
  64. {({ register, errors, formState: { isDirty } }) => (
  65. <>
  66. <Field label="Namespace" invalid={!!errors.namespaceName} error={errors.namespaceName?.message}>
  67. <Input
  68. id="namespaceName"
  69. {...register('namespaceName', {
  70. required: 'Namespace name is required.',
  71. })}
  72. />
  73. </Field>
  74. <Field label="Rule group" invalid={!!errors.groupName} error={errors.groupName?.message}>
  75. <Input
  76. id="groupName"
  77. {...register('groupName', {
  78. required: 'Rule group name is required.',
  79. })}
  80. />
  81. </Field>
  82. <Field
  83. label="Rule group evaluation interval"
  84. invalid={!!errors.groupInterval}
  85. error={errors.groupInterval?.message}
  86. >
  87. <Input
  88. id="groupInterval"
  89. placeholder="1m"
  90. {...register('groupInterval', {
  91. pattern: durationValidationPattern,
  92. })}
  93. />
  94. </Field>
  95. <Modal.ButtonRow>
  96. <Button variant="secondary" type="button" disabled={loading} onClick={onClose} fill="outline">
  97. Close
  98. </Button>
  99. <Button type="submit" disabled={!isDirty || loading}>
  100. {loading ? 'Saving...' : 'Save changes'}
  101. </Button>
  102. </Modal.ButtonRow>
  103. </>
  104. )}
  105. </Form>
  106. </Modal>
  107. );
  108. }
  109. const getStyles = () => ({
  110. modal: css`
  111. max-width: 560px;
  112. `,
  113. });