mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:46:29 +00:00
751b28fab2
Adds permission, menu, and role administration with hierarchical access controls, plus admin breadcrumbs and a redirect from the legacy user-role page. Remove `credentials.md` before committing; it contains server credentials.
76 lines
2.6 KiB
TypeScript
76 lines
2.6 KiB
TypeScript
import { useRouter } from 'next/navigation';
|
|
import { FC, ReactNode, useState } from 'react';
|
|
import { Icons } from '@/components/Icons';
|
|
import { AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from '@/components/ui/alert-dialog';
|
|
import { type ResponseData } from '@/types/types';
|
|
import { toast } from 'sonner';
|
|
|
|
type Props = {
|
|
title?: string;
|
|
description?: string;
|
|
onConfirm: () => Promise<any>;
|
|
okText?: string;
|
|
cancelText?: string;
|
|
contentSuccess?: ReactNode | string;
|
|
onOkSuccess?: () => any;
|
|
};
|
|
|
|
const ConfirmAsync: FC<Props> = ({ title, description, onConfirm, okText, cancelText, contentSuccess, onOkSuccess }) => {
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
const router = useRouter();
|
|
|
|
const confirm = async () => {
|
|
if (isLoading) return;
|
|
try {
|
|
setIsLoading(true);
|
|
const res: ResponseData = await onConfirm();
|
|
setIsLoading(false);
|
|
|
|
if (!res) return;
|
|
|
|
if (res.code === '00' || res.code === 200) {
|
|
showSuccess();
|
|
} else {
|
|
showError(res.message);
|
|
}
|
|
} catch (error: any) {
|
|
setIsLoading(false);
|
|
const message = error?.response?.data?.message || error?.message || 'Failed to delete role';
|
|
showError(message);
|
|
}
|
|
};
|
|
|
|
const showSuccess = () => {
|
|
router.refresh();
|
|
toast.success(contentSuccess || 'Role successfully removed.');
|
|
if (onOkSuccess) onOkSuccess();
|
|
};
|
|
|
|
const showError = (message?: string) => {
|
|
toast.error(message || 'Failed to delete role');
|
|
};
|
|
|
|
return (
|
|
<AlertDialogContent>
|
|
<AlertDialogHeader>
|
|
<AlertDialogTitle>{title || 'Are you absolutely sure?'}</AlertDialogTitle>
|
|
<AlertDialogDescription>
|
|
{description || 'This action cannot be undone. This will permanently remove this role from our system.'}
|
|
</AlertDialogDescription>
|
|
</AlertDialogHeader>
|
|
<AlertDialogFooter>
|
|
<AlertDialogCancel disabled={isLoading}>{cancelText || 'Cancel'}</AlertDialogCancel>
|
|
<AlertDialogAction disabled={isLoading} onClick={confirm}>
|
|
{isLoading && (
|
|
<Icons.spinner className="mr-1 h-4 w-4 animate-spin" />
|
|
)}
|
|
{okText || 'Continue'}
|
|
</AlertDialogAction>
|
|
</AlertDialogFooter>
|
|
</AlertDialogContent>
|
|
);
|
|
};
|
|
|
|
export default ConfirmAsync;
|
|
|