fastapi-rtk
Version:
A React component library for FastAPI in combination with FastAPI React Toolkit backend, built with Mantine, JsonForms, and Zustand.
61 lines (60 loc) • 2.3 kB
JavaScript
import { jsx } from "react/jsx-runtime";
import { DEBOUNCE_LOADING_DELAY, VIEW_MODE } from "fastapi-rtk/constants";
import { Text } from "@mantine/core";
import { useDebouncedState } from "@mantine/hooks";
import { useCallback } from "react";
import { CommonModal } from "../Modals/CommonModal.mjs";
import { overlayProps } from "../Modals/overlayProps.mjs";
import { useApi } from "../hooks/api/useApi.mjs";
import { useForms } from "../hooks/api/useForms.mjs";
import "../Wrappers/Provider/Contexts/LangContext.mjs";
import { useTranslation } from "../../../.external/esm/react-i18next@15.7.3_i18next@25.5.2_react-dom@18.3.1_react@18.3.1_typescript@5.9.2/react-i18next/dist/es/useTranslation.mjs";
const DeleteDialog = ({ deleteText, onSuccess, onError, ...props }) => {
const { refetch, refetchInfo, deleteEntry } = useApi();
const { t } = useTranslation();
const { opened, setOpened, getItem, reset, view, setView } = useForms("delete");
const [loading, setLoading] = useDebouncedState(false, DEBOUNCE_LOADING_DELAY);
const onReset = useCallback(() => {
setOpened(false);
}, [setOpened]);
const onSubmit = useCallback(
(e) => {
e == null ? void 0 : e.preventDefault();
setLoading(true);
deleteEntry(getItem()).then((res) => {
onSuccess == null ? void 0 : onSuccess(res);
refetch();
refetchInfo();
}).catch(onError).finally(() => {
setLoading(false);
setOpened(false);
});
},
[setLoading, deleteEntry, getItem, onError, onSuccess, refetch, refetchInfo, setOpened]
);
return /* @__PURE__ */ jsx(
CommonModal,
{
view,
setView,
onReset,
onSubmit,
withTitleOptions: false,
actionButtons: { resetButtonProps: { variant: "outline" }, submitButtonProps: { color: "red" } },
resetButtonText: t("Cancel"),
buttonText: t("Delete"),
buttonLoading: loading,
size: "md",
opened,
onClose: () => setOpened(false),
onExitTransitionEnd: reset,
...view === VIEW_MODE.OVERLAY ? overlayProps : {},
...props,
title: props.title ?? t("Delete Item?"),
children: deleteText ?? /* @__PURE__ */ jsx(Text, { children: t("Are you sure you want to delete this item?") })
}
);
};
export {
DeleteDialog
};