Add user creation form with adaptive Dialog/Drawer and Zod validation
This commit is contained in:
136
app/(backend)/users/components/UserForm/index.tsx
Normal file
136
app/(backend)/users/components/UserForm/index.tsx
Normal file
@@ -0,0 +1,136 @@
|
||||
'use client';
|
||||
|
||||
import { z } from "zod"
|
||||
import { useForm } from "react-hook-form"
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
|
||||
// Shadcn UI components
|
||||
import {
|
||||
Form,
|
||||
FormControl,
|
||||
FormField,
|
||||
FormItem,
|
||||
FormLabel,
|
||||
FormMessage
|
||||
} from "@/components/ui/form"
|
||||
import { Input } from "@/components/ui/input"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue
|
||||
} from "@/components/ui/select"
|
||||
import { Switch } from "@/components/ui/switch"
|
||||
|
||||
// 1. Schema definition
|
||||
const formSchema = z.object({
|
||||
email: z.string().email("Invalid email address."),
|
||||
role: z.string().min(1, "Please select a role."), // Rollenbezeichnung
|
||||
status: z.boolean().default(true), // Status (Active/Inactive)
|
||||
password: z.string().min(8, "Password must be at least 8 characters."),
|
||||
})
|
||||
|
||||
export type FormValues = z.infer<typeof formSchema>
|
||||
|
||||
export default function UserForm() {
|
||||
// 2. Initialize the form
|
||||
const form = useForm({
|
||||
resolver: zodResolver(formSchema),
|
||||
defaultValues: {
|
||||
email: "",
|
||||
role: "user",
|
||||
status: true,
|
||||
password: "",
|
||||
},
|
||||
})
|
||||
|
||||
// 3. Define the submit handler
|
||||
function onSubmit(values: FormValues) {
|
||||
// All comments in code must be in English
|
||||
// Later we will connect this to a Server Action
|
||||
console.log("Form values:", values)
|
||||
}
|
||||
|
||||
return (
|
||||
<Form {...form}>
|
||||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6 py-4">
|
||||
{/* Email Field */}
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="email"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Email</FormLabel>
|
||||
<FormControl>
|
||||
<Input placeholder="example@mail.com" {...field} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
{/* Role Select Field */}
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="role"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Role (Rollenbezeichnung)</FormLabel>
|
||||
<Select onValueChange={field.onChange} defaultValue={field.value}>
|
||||
<FormControl>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="Select a role" />
|
||||
</SelectTrigger>
|
||||
</FormControl>
|
||||
<SelectContent>
|
||||
<SelectItem value="admin">Admin</SelectItem>
|
||||
<SelectItem value="editor">Editor</SelectItem>
|
||||
<SelectItem value="user">User</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
{/* Status Switch Field */}
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="status"
|
||||
render={({ field }) => (
|
||||
<FormItem className="flex flex-row items-center justify-between rounded-lg border p-3 shadow-sm">
|
||||
<div className="space-y-0.5">
|
||||
<FormLabel>Active Status</FormLabel>
|
||||
</div>
|
||||
<FormControl>
|
||||
<Switch
|
||||
checked={field.value}
|
||||
onCheckedChange={field.onChange}
|
||||
/>
|
||||
</FormControl>
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
{/* Password Field */}
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="password"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Password</FormLabel>
|
||||
<FormControl>
|
||||
<Input type="password" placeholder="••••••••" {...field} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<Button type="submit" className="w-full">Save User</Button>
|
||||
</form>
|
||||
</Form>
|
||||
)
|
||||
}
|
||||
73
app/(backend)/users/components/UserFormWrapper.tsx
Normal file
73
app/(backend)/users/components/UserFormWrapper.tsx
Normal file
@@ -0,0 +1,73 @@
|
||||
"use client"
|
||||
|
||||
import * as React from "react"
|
||||
import { Plus } from "lucide-react"
|
||||
import { useMediaQuery } from "@/hooks/use-media-query"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogTrigger,
|
||||
} from "@/components/ui/dialog"
|
||||
import {
|
||||
Drawer,
|
||||
DrawerClose,
|
||||
DrawerContent,
|
||||
DrawerFooter,
|
||||
DrawerHeader,
|
||||
DrawerTitle,
|
||||
DrawerTrigger,
|
||||
} from "@/components/ui/drawer"
|
||||
import UserForm from "./UserForm"
|
||||
|
||||
export function UserFormWrapper() {
|
||||
const [open, setOpen] = React.useState(false)
|
||||
const isDesktop = useMediaQuery("(min-width: 768px)")
|
||||
|
||||
// Common button content
|
||||
const TriggerButton = (
|
||||
<Button
|
||||
variant="default"
|
||||
className="bg-secondary hover:bg-secondary/90 border border-border rounded-lg cursor-pointer h-10"
|
||||
>
|
||||
<Plus className="text-muted-foreground size-5 md:mr-2" />
|
||||
<span className="hidden md:inline text-sm text-muted-foreground">
|
||||
Add New User
|
||||
</span>
|
||||
</Button>
|
||||
)
|
||||
|
||||
if (isDesktop) {
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={setOpen}>
|
||||
<DialogTrigger asChild>
|
||||
{TriggerButton}
|
||||
</DialogTrigger>
|
||||
<DialogContent className="sm:max-w-106.25 bg-card text-white border-border">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Add New User</DialogTitle>
|
||||
</DialogHeader>
|
||||
<UserForm />
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<Drawer open={open} onOpenChange={setOpen}>
|
||||
<DrawerTrigger asChild>
|
||||
{TriggerButton}
|
||||
</DrawerTrigger>
|
||||
<DrawerContent className="bg-card border-border text-white max-h-[96dvh]">
|
||||
<DrawerHeader className="text-left">
|
||||
<DrawerTitle>Add New User</DrawerTitle>
|
||||
</DrawerHeader>
|
||||
<div className="px-4 overflow-y-auto pb-4">
|
||||
<UserForm />
|
||||
</div>
|
||||
</DrawerContent>
|
||||
</Drawer>
|
||||
)
|
||||
}
|
||||
@@ -2,9 +2,12 @@
|
||||
|
||||
import { useState } from "react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Search, Plus, MoreVertical, Filter } from "lucide-react";
|
||||
import { UserMobileCard } from "./components/UserMobileCard";
|
||||
import { UserFormWrapper } from "./components/UserFormWrapper";
|
||||
|
||||
// Temporary user data for demonstration
|
||||
const tempUsers = [
|
||||
{ id: 1, name: "John Doe", email: "john@example.com", role: "Admin", joinedAt: "2023-01-01", status: "active" },
|
||||
{ id: 2, name: "Alice Smith", email: "alice@test.com", role: "Editor", joinedAt: "2023-02-15", status: "active" },
|
||||
@@ -31,23 +34,16 @@ export default function UsersPage() {
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Кнопка: на мобилке круглая, на десктопе — солидная и яркая */}
|
||||
<Button
|
||||
variant="default"
|
||||
className="bg-secondary hover:bg-secondary/90 border border-border rounded-lg cursor-pointer">
|
||||
<Plus className="text-muted-foreground size-5 mr-2" />
|
||||
<span className="text-sm text-muted-foreground">
|
||||
Add New User
|
||||
</span>
|
||||
</Button>
|
||||
{/* Button: Add New User for Desktop only */}
|
||||
<UserFormWrapper />
|
||||
</div>
|
||||
|
||||
{/* Search + Filter */}
|
||||
<div className="flex gap-3 md:gap-4">
|
||||
<div className="relative w-full group">
|
||||
<Search className="absolute left-3 top-1/2 -translate-y-1/2 size-4 text-muted-foreground" />
|
||||
<input
|
||||
className="w-full bg-card border border-border rounded-lg py-1 pl-10 pr-12 focus:outline-1"
|
||||
<Input
|
||||
className="w-full bg-card border border-border rounded-lg py-1 pl-10 pr-12 h-10 focus:outline-1"
|
||||
placeholder="Search users..."
|
||||
/>
|
||||
<div className="absolute right-2 top-1/2 -translate-y-1/2">
|
||||
@@ -61,13 +57,10 @@ export default function UsersPage() {
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
{/* Кнопка: на мобилке круглая, на десктопе — солидная и яркая */}
|
||||
<Button
|
||||
variant="default"
|
||||
className="md:hidden bg-secondary rounded-lg text-muted-foreground border border-border"
|
||||
>
|
||||
<Plus className="size-6 md:size-5 md:mr-2" />
|
||||
</Button>
|
||||
{/* Button: Add New User for Mobile only */}
|
||||
<div className="md:hidden">
|
||||
<UserFormWrapper />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user