mirror of
https://codeberg.org/MarkusThielker/finances.git
synced 2025-04-12 05:08:43 +00:00
N-FIN-47: add optional confirmation dialog to new component
This commit is contained in:
parent
0e62b7c2fd
commit
e809912ae3
1 changed files with 48 additions and 4 deletions
|
@ -9,11 +9,29 @@ import { toast } from 'sonner';
|
||||||
import { sonnerContent } from '@/components/ui/sonner';
|
import { sonnerContent } from '@/components/ui/sonner';
|
||||||
import type { VariantProps } from 'class-variance-authority';
|
import type { VariantProps } from 'class-variance-authority';
|
||||||
import { ActionResponse } from '@/lib/types/actionResponse';
|
import { ActionResponse } from '@/lib/types/actionResponse';
|
||||||
|
import {
|
||||||
|
AlertDialog,
|
||||||
|
AlertDialogAction,
|
||||||
|
AlertDialogCancel,
|
||||||
|
AlertDialogContent,
|
||||||
|
AlertDialogDescription,
|
||||||
|
AlertDialogFooter,
|
||||||
|
AlertDialogHeader,
|
||||||
|
AlertDialogTitle,
|
||||||
|
AlertDialogTrigger,
|
||||||
|
} from '@/components/ui/alert-dialog';
|
||||||
|
|
||||||
|
export interface ConfirmationDialogProps {
|
||||||
|
title: string;
|
||||||
|
description?: string;
|
||||||
|
actionText?: string;
|
||||||
|
}
|
||||||
|
|
||||||
export interface ButtonWithActionProps<T = any>
|
export interface ButtonWithActionProps<T = any>
|
||||||
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
|
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
|
||||||
VariantProps<typeof buttonVariants> {
|
VariantProps<typeof buttonVariants> {
|
||||||
asChild?: boolean;
|
asChild?: boolean;
|
||||||
|
dialog?: ConfirmationDialogProps;
|
||||||
action: () => Promise<ActionResponse<T>>;
|
action: () => Promise<ActionResponse<T>>;
|
||||||
callback?: (data: T) => void;
|
callback?: (data: T) => void;
|
||||||
}
|
}
|
||||||
|
@ -36,14 +54,40 @@ const ServerActionTrigger = React.forwardRef<HTMLButtonElement, ButtonWithAction
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: add optional confirmation dialog
|
return props.dialog ? (
|
||||||
|
<AlertDialog>
|
||||||
return (
|
<AlertDialogTrigger asChild>
|
||||||
|
<Comp
|
||||||
|
className={cn(buttonVariants({variant, size, className}))}
|
||||||
|
{...{...props, action: undefined, callback: undefined}}
|
||||||
|
ref={ref}
|
||||||
|
/>
|
||||||
|
</AlertDialogTrigger>
|
||||||
|
<AlertDialogContent>
|
||||||
|
<AlertDialogHeader>
|
||||||
|
<AlertDialogTitle>{props.dialog.title}</AlertDialogTitle>
|
||||||
|
{props.dialog?.description && (
|
||||||
|
<AlertDialogDescription>
|
||||||
|
{props.dialog.description}
|
||||||
|
</AlertDialogDescription>
|
||||||
|
)}
|
||||||
|
</AlertDialogHeader>
|
||||||
|
<AlertDialogFooter>
|
||||||
|
<AlertDialogCancel>
|
||||||
|
Cancel
|
||||||
|
</AlertDialogCancel>
|
||||||
|
<AlertDialogAction onClick={handleSubmit}>
|
||||||
|
{props.dialog.actionText || 'Confirm'}
|
||||||
|
</AlertDialogAction>
|
||||||
|
</AlertDialogFooter>
|
||||||
|
</AlertDialogContent>
|
||||||
|
</AlertDialog>
|
||||||
|
) : (
|
||||||
<Comp
|
<Comp
|
||||||
className={cn(buttonVariants({variant, size, className}))}
|
className={cn(buttonVariants({variant, size, className}))}
|
||||||
ref={ref}
|
ref={ref}
|
||||||
{...{...props, action: undefined, callback: undefined}}
|
|
||||||
onClick={handleSubmit}
|
onClick={handleSubmit}
|
||||||
|
{...props}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|
Loading…
Add table
Reference in a new issue