/* * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ import React, { useContext, useEffect, useState } from "react"; import { CoreStart } from "opensearch-dashboards/public"; import { ServicesContext } from "../../../../services"; import { CoreServicesContext } from "../../../../components/core_services"; import { EuiButton, EuiButtonEmpty, EuiLoadingSpinner, EuiModal, EuiModalBody, EuiModalFooter, EuiModalHeader, EuiModalHeaderTitle, EuiSpacer, } from "@elastic/eui"; import { submitTemplateChange, useComponentMapTemplate } from "../../utils/hooks"; import { Link } from "react-router-dom"; import { ROUTES } from "../../../../utils/constants"; import { AllBuiltInComponents } from "../../../../components/FormGenerator"; import { ServerResponse } from "../../../../../server/models/types"; export interface DeleteTemplateModalProps { selectedItems: string[]; visible: boolean; onClose: () => void; onDelete: () => void; } export default function DeleteTemplateModal(props: DeleteTemplateModalProps) { const { onClose, visible, selectedItems, onDelete } = props; const services = useContext(ServicesContext); const coreServices = useContext(CoreServicesContext) as CoreStart; const { componentMapTemplate, loading } = useComponentMapTemplate(); const [checked, setChecked] = useState(false); const linkedIndexItemCount = componentMapTemplate[selectedItems[0]]?.length || 0; const onConfirm = async () => { if (services) { let result: ServerResponse = { ok: true, response: {}, }; if (linkedIndexItemCount) { result = await Promise.all( componentMapTemplate[selectedItems[0]].map((item) => submitTemplateChange({ coreService: coreServices, commonService: services.commonService, templateName: item, transformTemplate(currentTemplate) { return { ...currentTemplate, composed_of: currentTemplate.composed_of?.filter((item) => item !== selectedItems[0]) || [], }; }, }) ) ).then((result) => ({ response: {}, ok: result.every((item) => item.ok), error: result .filter((item) => !item.ok) .map((item) => item.error) .join(", "), })); } if (result.ok) { result = await services.commonService.apiCaller({ endpoint: "transport.request", data: { path: `/_component_template/${selectedItems.join(",")}`, method: "DELETE", }, }); } if (result && result.ok) { coreServices.notifications.toasts.addSuccess(`Delete [${selectedItems.join(", ")}] successfully`); onDelete(); } else { coreServices.notifications.toasts.addDanger(result?.error || ""); } } }; useEffect(() => { if (visible) { setChecked(false); } }, [visible]); if (!visible) { return null; } return ( Delete {selectedItems[0]} {loading ? ( ) : (

{!linkedIndexItemCount ? "The following component template will be permanently deleted. This action cannot be undone." : `The component template ${selectedItems.join( ", " )} will be permanently deleted. The component template will be unlinked from ${linkedIndexItemCount} index templates:`}

{!linkedIndexItemCount ? (
    {selectedItems.map((item) => (
  • {item}
  • ))}
) : null} {linkedIndexItemCount ? ( <> {componentMapTemplate[selectedItems[0]]?.map((item, index) => ( {index > 0 && ", "} {item} ))} setChecked(checked)} /> ) : null}
)}
Cancel {!linkedIndexItemCount ? ( Delete ) : null} {linkedIndexItemCount ? ( Apply changes ) : null}
); }