Files
weeding/src/app/dashboard/settings/users/new/user-form.tsx

131 lines
4.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use client'
import { zodResolver } from "@hookform/resolvers/zod"
import { useForm } from "react-hook-form"
import * as z from "zod"
import { Button } from "@/components/ui/button"
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form"
import { Input } from "@/components/ui/input"
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"
import { createUser } from "./actions"
import { useRouter } from "next/navigation"
import { useState } from "react"
import { toast } from "sonner"
const formSchema = z.object({
email: z.string().email("Geçerli bir e-posta adresi giriniz."),
password: z.string().min(6, "Şifre en az 6 karakter olmalıdır."),
full_name: z.string().min(2, "İsim en az 2 karakter olmalıdır."),
role: z.enum(["admin", "staff"]),
})
export function UserForm() {
const router = useRouter()
const [loading, setLoading] = useState(false)
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
email: "",
password: "",
full_name: "",
role: "staff",
},
})
async function onSubmit(values: z.infer<typeof formSchema>) {
setLoading(true)
try {
const result = await createUser(values)
if (result?.error) {
toast.error(result.error)
} else {
toast.success("Kullanıcı başarıyla oluşturuldu")
router.push('/dashboard/settings/users')
router.refresh()
}
} catch (error) {
toast.error("Bir hata oluştu")
} finally {
setLoading(false)
}
}
return (
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
<FormField
control={form.control}
name="email"
render={({ field }) => (
<FormItem>
<FormLabel>E-posta</FormLabel>
<FormControl>
<Input placeholder="ornek@email.com" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="password"
render={({ field }) => (
<FormItem>
<FormLabel>Şifre</FormLabel>
<FormControl>
<Input type="password" placeholder="******" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="full_name"
render={({ field }) => (
<FormItem>
<FormLabel>İsim Soyisim</FormLabel>
<FormControl>
<Input placeholder="Ahmet Yılmaz" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="role"
render={({ field }) => (
<FormItem>
<FormLabel>Rol</FormLabel>
<Select onValueChange={field.onChange} defaultValue={field.value}>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Rol Seçin" />
</SelectTrigger>
</FormControl>
<SelectContent>
<SelectItem value="staff">Personel</SelectItem>
<SelectItem value="admin">Yönetici</SelectItem>
</SelectContent>
</Select>
<FormMessage />
</FormItem>
)}
/>
<Button type="submit" disabled={loading} className="w-full">
{loading ? "Oluşturuluyor..." : "Kullanıcı Oluştur"}
</Button>
</form>
</Form>
)
}