@premieroctet/next-admin
Version:
Next-Admin provides a customizable and turnkey admin dashboard for applications built with Next.js and powered by the Prisma ORM. It aims to simplify the development process by providing a turnkey admin system that can be easily integrated into your proje
57 lines (56 loc) • 1.85 kB
JavaScript
import { useConfig } from "../context/ConfigContext.mjs";
import { useI18n } from "../context/I18nContext.mjs";
import { useMessage } from "../context/MessageContext.mjs";
import { slugify } from "../utils/tools.mjs";
import { useRouterInternal } from "./useRouterInternal.mjs";
const useDeleteAction = (resource)=>{
const { apiBasePath } = useConfig();
const { router } = useRouterInternal();
const { t } = useI18n();
const { showMessage } = useMessage();
const runDeletion = async (ids)=>{
const response = await fetch(`${apiBasePath}/${slugify(resource)}`, {
method: "DELETE",
body: JSON.stringify(ids),
headers: {
"Content-Type": "application/json"
}
});
if (!response.ok) {
const result = await response.json();
throw new Error(result.error);
}
};
const runSingleDeletion = async (id)=>{
const response = await fetch(`${apiBasePath}/${slugify(resource)}/${id}`, {
method: "DELETE"
});
if (!response.ok) {
const result = await response.json();
throw new Error(result.error);
}
};
const deleteItems = async (ids)=>{
if (window.confirm(t("list.row.actions.delete.alert", {
count: ids.length
}))) try {
await runDeletion(ids);
showMessage({
type: "success",
message: t("list.row.actions.delete.success")
});
router.refresh();
} catch {
showMessage({
type: "error",
message: t("list.row.actions.delete.error")
});
}
};
return {
deleteItems,
runDeletion,
runSingleDeletion
};
};
export { useDeleteAction };