import { css } from '@emotion/css'; import React, { useEffect, useMemo } from 'react'; import { useDispatch } from 'react-redux'; import { Modal, Button, Form, Field, Input, useStyles2 } from '@grafana/ui'; import { useCleanup } from 'app/core/hooks/useCleanup'; import { CombinedRuleGroup, CombinedRuleNamespace } from 'app/types/unified-alerting'; import { useUnifiedAlertingSelector } from '../../hooks/useUnifiedAlertingSelector'; import { updateLotexNamespaceAndGroupAction } from '../../state/actions'; import { getRulesSourceName } from '../../utils/datasource'; import { initialAsyncRequestState } from '../../utils/redux'; import { durationValidationPattern } from '../../utils/time'; interface Props { namespace: CombinedRuleNamespace; group: CombinedRuleGroup; onClose: () => void; } interface FormValues { namespaceName: string; groupName: string; groupInterval: string; } export function EditCloudGroupModal(props: Props): React.ReactElement { const { namespace, group, onClose } = props; const styles = useStyles2(getStyles); const dispatch = useDispatch(); const { loading, error, dispatched } = useUnifiedAlertingSelector((state) => state.updateLotexNamespaceAndGroup) ?? initialAsyncRequestState; const defaultValues = useMemo( (): FormValues => ({ namespaceName: namespace.name, groupName: group.name, groupInterval: group.interval ?? '', }), [namespace, group] ); // close modal if successfully saved useEffect(() => { if (dispatched && !loading && !error) { onClose(); } }, [dispatched, loading, onClose, error]); useCleanup((state) => state.unifiedAlerting.updateLotexNamespaceAndGroup); const onSubmit = (values: FormValues) => { dispatch( updateLotexNamespaceAndGroupAction({ rulesSourceName: getRulesSourceName(namespace.rulesSource), groupName: group.name, newGroupName: values.groupName, namespaceName: namespace.name, newNamespaceName: values.namespaceName, groupInterval: values.groupInterval || undefined, }) ); }; return (
{({ register, errors, formState: { isDirty } }) => ( <> )}
); } const getStyles = () => ({ modal: css` max-width: 560px; `, });