Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reset form fields in delete dialog on reopen #2546

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ export const DeletePermission: React.FC<Props> = ({ trigger, permission }) => {
const [open, setOpen] = useState(false);

const formSchema = z.object({
name: z.string().refine((v) => v === permission.name, "Please confirm the role's name"),
name: z
.string()
.refine((v) => v === permission.name, "Please confirm the role's name"),
Vardhaman619 marked this conversation as resolved.
Show resolved Hide resolved
});

const form = useForm<z.infer<typeof formSchema>>({
Expand All @@ -68,8 +70,13 @@ export const DeletePermission: React.FC<Props> = ({ trigger, permission }) => {
deletePermission.mutate({ permissionId: permission.id });
}

function handleDialogOpenChange(newState: boolean) {
setOpen(newState);
form.reset();
}

return (
<Dialog open={open} onOpenChange={(o) => setOpen(o)}>
<Dialog open={open} onOpenChange={handleDialogOpenChange}>
<DialogTrigger asChild>{trigger}</DialogTrigger>
<DialogContent className="border-alert p-4 max-w-md mx-auto">
<DialogHeader>
Expand All @@ -79,10 +86,15 @@ export const DeletePermission: React.FC<Props> = ({ trigger, permission }) => {
</DialogDescription>
</DialogHeader>
<Form {...form}>
<form className="flex flex-col space-y-8" onSubmit={form.handleSubmit(onSubmit)}>
<form
className="flex flex-col space-y-8"
onSubmit={form.handleSubmit(onSubmit)}
>
<Alert variant="alert">
<AlertTitle>Warning</AlertTitle>
<AlertDescription>This action is not reversible. Please be certain.</AlertDescription>
<AlertDescription>
This action is not reversible. Please be certain.
</AlertDescription>
Vardhaman619 marked this conversation as resolved.
Show resolved Hide resolved
</Alert>

<FormField
Expand All @@ -93,8 +105,10 @@ export const DeletePermission: React.FC<Props> = ({ trigger, permission }) => {
<FormLabel className="font-normal text-content-subtle">
{" "}
Enter the permission's name{" "}
<span className="font-medium text-content break-all">{permission.name}</span> to
continue:
<span className="font-medium text-content break-all">
{permission.name}
</span>{" "}
to continue:
</FormLabel>
<FormControl>
<Input {...field} autoComplete="off" className="w-full" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ export const DeleteRole: React.FC<Props> = ({ trigger, role }) => {
const [open, setOpen] = useState(false);

const formSchema = z.object({
name: z.string().refine((v) => v === role.name, "Please confirm the role's name"),
name: z
.string()
.refine((v) => v === role.name, "Please confirm the role's name"),
});

const form = useForm<z.infer<typeof formSchema>>({
Expand All @@ -69,22 +71,32 @@ export const DeleteRole: React.FC<Props> = ({ trigger, role }) => {
deleteRole.mutate({ roleId: role.id });
}

function handleDialogOpenChange(newState: boolean) {
setOpen(newState);
form.reset();
}

return (
<Dialog open={open} onOpenChange={(o) => setOpen(o)}>
<Dialog open={open} onOpenChange={handleDialogOpenChange}>
Vardhaman619 marked this conversation as resolved.
Show resolved Hide resolved
<DialogTrigger asChild>{trigger}</DialogTrigger>
<DialogContent className="border-alert">
<DialogHeader>
<DialogTitle>Delete Role</DialogTitle>
<DialogDescription>
This role will be deleted, keys with this role will be disconnected from all permissions
granted by this role.
This role will be deleted, keys with this role will be disconnected
from all permissions granted by this role.
</DialogDescription>
</DialogHeader>
<Form {...form}>
<form className="flex flex-col space-y-8" onSubmit={form.handleSubmit(onSubmit)}>
<form
className="flex flex-col space-y-8"
onSubmit={form.handleSubmit(onSubmit)}
>
<Alert variant="alert">
<AlertTitle>Warning</AlertTitle>
<AlertDescription>This action is not reversible. Please be certain.</AlertDescription>
<AlertDescription>
This action is not reversible. Please be certain.
</AlertDescription>
</Alert>

<FormField
Expand All @@ -95,7 +107,10 @@ export const DeleteRole: React.FC<Props> = ({ trigger, role }) => {
<FormLabel className="font-normal text-content-subtle">
{" "}
Enter the role's key{" "}
<span className="font-medium text-content">{role.name}</span> to continue:
<span className="font-medium text-content">
{role.name}
</span>{" "}
to continue:
</FormLabel>
<FormControl>
<Input {...field} autoComplete="off" />
Expand Down