mirror of
https://github.com/rgrgogu/new_starr.git
synced 2026-09-27 00:12:54 +08:00
106 lines
3.0 KiB
React
106 lines
3.0 KiB
React
// modules/admin/components/user_groups/AddGroupDialog.jsx
|
|
|
|
import { useForm } from "react-hook-form";
|
|
import { z } from "zod";
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
DialogFooter,
|
|
DialogClose,
|
|
} from "@/components/ui/dialog";
|
|
import { Input } from "@/components/ui/input";
|
|
import { Textarea } from "@/components/ui/textarea";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Label } from "@/components/ui/label";
|
|
|
|
const schema = z.object({
|
|
name: z.string().min(1, "Name is required."),
|
|
description: z.string().min(1, "Description is required."),
|
|
});
|
|
|
|
/**
|
|
* Dialog for creating a new group.
|
|
* Matches context: createGroup({ name, description })
|
|
*
|
|
* @param {Object} props
|
|
* @param {boolean} props.open
|
|
* @param {Function} props.onOpenChange
|
|
* @param {Function} props.onSubmit Called with { name, description }
|
|
* @param {boolean} [props.loading]
|
|
*/
|
|
export function AddGroupDialog({ open, onOpenChange, onSubmit, loading }) {
|
|
const {
|
|
register,
|
|
handleSubmit,
|
|
reset,
|
|
formState: { errors },
|
|
} = useForm({
|
|
resolver: zodResolver(schema),
|
|
defaultValues: { name: "", description: "" },
|
|
});
|
|
|
|
async function onValid(values) {
|
|
await onSubmit(values);
|
|
reset();
|
|
}
|
|
|
|
function handleClose() {
|
|
reset();
|
|
onOpenChange(false);
|
|
}
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={handleClose}>
|
|
<DialogContent className="sm:max-w-[440px]">
|
|
<DialogHeader>
|
|
<DialogTitle>Add group</DialogTitle>
|
|
</DialogHeader>
|
|
|
|
<form onSubmit={handleSubmit(onValid)} className="space-y-4">
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="name">
|
|
Name <span className="text-destructive">*</span>
|
|
</Label>
|
|
<Input
|
|
id="name"
|
|
placeholder="Group name"
|
|
{...register("name")}
|
|
/>
|
|
{errors.name && (
|
|
<p className="text-sm text-destructive">{errors.name.message}</p>
|
|
)}
|
|
</div>
|
|
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="description">
|
|
Description <span className="text-destructive">*</span>
|
|
</Label>
|
|
<Textarea
|
|
id="description"
|
|
placeholder="Brief description of this group"
|
|
className="resize-none"
|
|
rows={3}
|
|
{...register("description")}
|
|
/>
|
|
{errors.description && (
|
|
<p className="text-sm text-destructive">{errors.description.message}</p>
|
|
)}
|
|
</div>
|
|
|
|
<DialogFooter>
|
|
<DialogClose asChild>
|
|
<Button type="button" variant="outline" disabled={loading}>Cancel</Button>
|
|
</DialogClose>
|
|
<Button type="submit" disabled={loading}>
|
|
{loading ? "Creating..." : "Add group"}
|
|
</Button>
|
|
</DialogFooter>
|
|
</form>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
} |