diff --git a/x-pack/plugins/observability/public/pages/slo_edit/components/common/equivalent_api_request.tsx b/x-pack/plugins/observability/public/pages/slo_edit/components/common/equivalent_api_request.tsx new file mode 100644 index 000000000000..6bf597b12d9a --- /dev/null +++ b/x-pack/plugins/observability/public/pages/slo_edit/components/common/equivalent_api_request.tsx @@ -0,0 +1,128 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ +import { i18n } from '@kbn/i18n'; + +import React, { useEffect, useState } from 'react'; +import { + EuiButtonEmpty, + EuiCodeBlock, + EuiFlyout, + EuiFlyoutBody, + EuiFlyoutFooter, + EuiFlyoutHeader, + EuiSpacer, + EuiText, + EuiTitle, +} from '@elastic/eui'; +import { useFormContext } from 'react-hook-form'; +import { FormattedMessage } from '@kbn/i18n-react'; +import { CreateSLOInput } from '@kbn/slo-schema'; +import { CreateSLOForm } from '../../types'; +import { transformCreateSLOFormToCreateSLOInput } from '../../helpers/process_slo_form_values'; + +export function EquivalentApiRequest({ + isCreateSloLoading, + isUpdateSloLoading, +}: { + isCreateSloLoading: boolean; + isUpdateSloLoading: boolean; +}) { + const [isFlyoutVisible, setIsFlyoutVisible] = useState(false); + + const { getValues, trigger } = useFormContext(); + + const [sloData, setSloData] = useState(); + + useEffect(() => { + if (!isFlyoutVisible) { + return; + } + trigger().then((isValid) => { + if (isValid) { + setSloData(transformCreateSLOFormToCreateSLOInput(getValues())); + } + }); + }, [getValues, trigger, isFlyoutVisible]); + + let flyout; + + if (isFlyoutVisible) { + flyout = ( + setIsFlyoutVisible(false)}> + + +

+ {i18n.translate( + 'xpack.observability.equivalentApiRequest.h2.equivalentAPIRequestToLabel', + { defaultMessage: 'Equivalent API request' } + )} +

+
+
+ + + + + + + {'POST /api/observability/slos'} + + + + + + {sloData ? ( + + {JSON.stringify(sloData, null, 2)} + + ) : ( + + {i18n.translate( + 'xpack.observability.equivalentApiRequest.formIsNotValidCodeBlockLabel', + { defaultMessage: 'Form is not valid' } + )} + + )} + + + setIsFlyoutVisible(false)} + flush="left" + > + {i18n.translate('xpack.observability.equivalentApiRequest.closeButtonEmptyLabel', { + defaultMessage: 'Close', + })} + + +
+ ); + } + return ( + <> + setIsFlyoutVisible(true)} + > + {i18n.translate('xpack.observability.slo.sloEdit.equivalentApiRequest', { + defaultMessage: 'Equivalent API request', + })} + + {flyout} + + ); +} diff --git a/x-pack/plugins/observability/public/pages/slo_edit/components/slo_edit_form.tsx b/x-pack/plugins/observability/public/pages/slo_edit/components/slo_edit_form.tsx index e5f8e9b02ce2..0e3c491bdd7c 100644 --- a/x-pack/plugins/observability/public/pages/slo_edit/components/slo_edit_form.tsx +++ b/x-pack/plugins/observability/public/pages/slo_edit/components/slo_edit_form.tsx @@ -18,6 +18,7 @@ import { i18n } from '@kbn/i18n'; import type { GetSLOResponse } from '@kbn/slo-schema'; import React, { useCallback, useEffect, useState } from 'react'; import { FormProvider, useForm } from 'react-hook-form'; +import { EquivalentApiRequest } from './common/equivalent_api_request'; import { BurnRateRuleFlyout } from '../../slos/components/common/burn_rate_rule_flyout'; import { paths } from '../../../../common/locators/paths'; import { useCreateSlo } from '../../../hooks/slo/use_create_slo'; @@ -34,7 +35,6 @@ import { CREATE_RULE_SEARCH_PARAM, useAddRuleFlyoutState, } from '../hooks/use_add_rule_flyout_state'; -import { useCopyToJson } from '../hooks/use_copy_to_json'; import { useParseUrlState } from '../hooks/use_parse_url_state'; import { useSectionFormValidation } from '../hooks/use_section_form_validation'; import { useShowSections } from '../hooks/use_show_sections'; @@ -78,7 +78,6 @@ export function SloEditForm({ slo }: Props) { mode: 'all', }); const { watch, getFieldState, getValues, formState, trigger } = methods; - const handleCopyToJson = useCopyToJson({ trigger, getValues }); const { isIndicatorSectionValid, isObjectiveSectionValid, isDescriptionSectionValid } = useSectionFormValidation({ @@ -235,17 +234,10 @@ export function SloEditForm({ slo }: Props) { })} - - {i18n.translate('xpack.observability.slo.sloEdit.copyJsonButton', { - defaultMessage: 'Copy JSON', - })} - + diff --git a/x-pack/plugins/observability/public/pages/slo_edit/hooks/use_copy_to_json.ts b/x-pack/plugins/observability/public/pages/slo_edit/hooks/use_copy_to_json.ts deleted file mode 100644 index c2c59eaeb536..000000000000 --- a/x-pack/plugins/observability/public/pages/slo_edit/hooks/use_copy_to_json.ts +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import { i18n } from '@kbn/i18n'; -import { UseFormGetValues, UseFormTrigger } from 'react-hook-form'; -import { useKibana } from '../../../utils/kibana_react'; -import { transformCreateSLOFormToCreateSLOInput } from '../helpers/process_slo_form_values'; -import { CreateSLOForm } from '../types'; - -interface Props { - trigger: UseFormTrigger; - getValues: UseFormGetValues; -} - -export function useCopyToJson({ trigger, getValues }: Props): () => Promise { - const { notifications } = useKibana().services; - - const handleCopyToJson = async () => { - const isValid = await trigger(); - if (!isValid) { - return; - } - const values = transformCreateSLOFormToCreateSLOInput(getValues()); - try { - await copyTextToClipboard(JSON.stringify(values, null, 2)); - notifications.toasts.add({ - title: i18n.translate('xpack.observability.slo.sloEdit.copyJsonNotification', { - defaultMessage: 'JSON copied to clipboard', - }), - }); - } catch (e) { - notifications.toasts.add({ - title: i18n.translate('xpack.observability.slo.sloEdit.copyJsonFailedNotification', { - defaultMessage: 'Could not copy JSON to clipboard', - }), - }); - } - }; - - const copyTextToClipboard = async (text: string) => { - if (!window.navigator?.clipboard) { - throw new Error('Could not copy to clipboard!'); - } - await window.navigator.clipboard.writeText(text); - }; - - return handleCopyToJson; -} diff --git a/x-pack/plugins/translations/translations/fr-FR.json b/x-pack/plugins/translations/translations/fr-FR.json index 779921068028..35af07ca6452 100644 --- a/x-pack/plugins/translations/translations/fr-FR.json +++ b/x-pack/plugins/translations/translations/fr-FR.json @@ -29269,9 +29269,6 @@ "xpack.observability.slo.sloEdit.calendarTimeWindow.monthly": "Mensuel", "xpack.observability.slo.sloEdit.calendarTimeWindow.weekly": "Hebdomadaire", "xpack.observability.slo.sloEdit.cancelButton": "Annuler", - "xpack.observability.slo.sloEdit.copyJsonButton": "Copier le JSON", - "xpack.observability.slo.sloEdit.copyJsonFailedNotification": "Échec de la copie du JSON dans le presse-papiers", - "xpack.observability.slo.sloEdit.copyJsonNotification": "JSON copié dans le presse-papiers", "xpack.observability.slo.sloEdit.createAlert.ruleName": "Règle d'alerte de taux d'avancement du SLO", "xpack.observability.slo.sloEdit.createAlert.title": "Créer", "xpack.observability.slo.sloEdit.createSloButton": "Créer un SLO", diff --git a/x-pack/plugins/translations/translations/ja-JP.json b/x-pack/plugins/translations/translations/ja-JP.json index 27125a6261b2..b720e9f192e7 100644 --- a/x-pack/plugins/translations/translations/ja-JP.json +++ b/x-pack/plugins/translations/translations/ja-JP.json @@ -29269,9 +29269,6 @@ "xpack.observability.slo.sloEdit.calendarTimeWindow.monthly": "月ごと", "xpack.observability.slo.sloEdit.calendarTimeWindow.weekly": "週ごと", "xpack.observability.slo.sloEdit.cancelButton": "キャンセル", - "xpack.observability.slo.sloEdit.copyJsonButton": "JSONをコピー", - "xpack.observability.slo.sloEdit.copyJsonFailedNotification": "JSONをクリップボードにコピーできませんでした", - "xpack.observability.slo.sloEdit.copyJsonNotification": "JSONがクリップボードにコピーされました", "xpack.observability.slo.sloEdit.createAlert.ruleName": "SLOバーンレートアラートルール", "xpack.observability.slo.sloEdit.createAlert.title": "作成", "xpack.observability.slo.sloEdit.createSloButton": "SLOの作成", diff --git a/x-pack/plugins/translations/translations/zh-CN.json b/x-pack/plugins/translations/translations/zh-CN.json index 9f4ab71dc071..480765f1543a 100644 --- a/x-pack/plugins/translations/translations/zh-CN.json +++ b/x-pack/plugins/translations/translations/zh-CN.json @@ -29266,9 +29266,6 @@ "xpack.observability.slo.sloEdit.calendarTimeWindow.monthly": "每月", "xpack.observability.slo.sloEdit.calendarTimeWindow.weekly": "每周", "xpack.observability.slo.sloEdit.cancelButton": "取消", - "xpack.observability.slo.sloEdit.copyJsonButton": "复制 JSON", - "xpack.observability.slo.sloEdit.copyJsonFailedNotification": "无法将 JSON 复制到剪贴板", - "xpack.observability.slo.sloEdit.copyJsonNotification": "JSON 已复制到剪贴板", "xpack.observability.slo.sloEdit.createAlert.ruleName": "SLO 消耗速度告警规则", "xpack.observability.slo.sloEdit.createAlert.title": "创建", "xpack.observability.slo.sloEdit.createSloButton": "创建 SLO",