add confirm button for dangerous admin actions

This commit is contained in:
Brandon Egger
2023-09-05 20:27:33 -05:00
parent 8b42377453
commit f66c35a225
4 changed files with 88 additions and 11 deletions

View File

@ -1,4 +1,6 @@
import { ExclamationCircleIcon } from "@heroicons/react/24/solid";
import Link from "next/link";
import { useEffect, useState } from "react";
const AdminActionBody = ({
label,
@ -38,6 +40,53 @@ const AdminActionLink = ({
);
};
const AdminActionConfirmButton = ({
label,
onConfirm,
symbol,
type = "button",
}: {
label: string;
onConfirm?: () => void;
symbol: JSX.Element | undefined;
type?: HTMLButtonElement["type"];
}) => {
const [isConfirmView, setConfirmView] = useState(false);
useEffect(() => {
if (!isConfirmView) {
return;
}
setTimeout(() => {
if (isConfirmView) {
setConfirmView(false);
}
}, 5000);
}, [isConfirmView]);
if (isConfirmView) {
return (
<AdminActionButton
symbol={<ExclamationCircleIcon className="w-4 animate-ping" />}
label={`Confirm ${label}`}
onClick={onConfirm}
type={type}
/>
);
}
return (
<AdminActionButton
symbol={symbol}
label={label}
onClick={() => {
setConfirmView(true);
}}
/>
);
};
const AdminActionButton = ({
label,
onClick,
@ -65,4 +114,4 @@ const AdminActionButton = ({
);
};
export { AdminActionLink, AdminActionButton };
export { AdminActionLink, AdminActionButton, AdminActionConfirmButton };