import { ReactNode, useEffect, useState } from "react"; import { useToggle } from "@app/hooks"; import { Button } from "../Button"; import { FormControl } from "../FormControl"; import { Input } from "../Input"; import { Modal, ModalClose, ModalContent } from "../Modal"; type Props = { isOpen?: boolean; onClose?: () => void; onChange?: (isOpen: boolean) => void; deleteKey: string; title: string; subTitle?: string; onDeleteApproved: () => Promise; buttonText?: string; formContent?: ReactNode; children?: ReactNode; deletionMessage?: ReactNode; }; export const DeleteActionModal = ({ isOpen, onClose, onChange, deleteKey, onDeleteApproved, title, subTitle = "This action is irreversible.", buttonText = "Delete", formContent, deletionMessage, children }: Props): JSX.Element => { const [inputData, setInputData] = useState(""); const [isLoading, setIsLoading] = useToggle(); useEffect(() => { setInputData(""); }, [isOpen]); const onDelete = async () => { setIsLoading.on(); try { await onDeleteApproved(); } catch { setIsLoading.off(); } finally { setIsLoading.off(); } }; return ( { setInputData(""); if (onChange) onChange(isOpenState); }} > {" "} } onClose={onClose} > {formContent}
{ evt.preventDefault(); if (deleteKey === inputData) onDelete(); }} > {deletionMessage || ( <> Type {deleteKey} to perform this action )} } className="mb-0" > setInputData(e.target.value)} placeholder={`Type ${deleteKey} here`} /> {children}
); };