Site Yönetimi,Kullanıcı Girişi,Karanlık mod özellikleri db bağlantıları.
This commit is contained in:
57
components/dashboard/appearance-form.tsx
Normal file
57
components/dashboard/appearance-form.tsx
Normal file
@@ -0,0 +1,57 @@
|
||||
"use client"
|
||||
|
||||
import { useTheme } from "next-themes"
|
||||
import { Card, CardContent, CardTitle, CardHeader } from "@/components/ui/card"
|
||||
import { Label } from "@/components/ui/label"
|
||||
import { Switch } from "@/components/ui/switch"
|
||||
import { useEffect, useState } from "react"
|
||||
|
||||
export function AppearanceForm() {
|
||||
const { theme, setTheme } = useTheme()
|
||||
const [mounted, setMounted] = useState(false)
|
||||
|
||||
// Avoid hydration mismatch
|
||||
useEffect(() => {
|
||||
setMounted(true)
|
||||
}, [])
|
||||
|
||||
if (!mounted) {
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Görünüm</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="flex items-center justify-between space-y-2">
|
||||
<Label htmlFor="dark-mode" className="flex flex-col space-y-1">
|
||||
<span>Karanlık Mod</span>
|
||||
<span className="font-normal text-xs text-muted-foreground">Koyu temayı etkinleştir.</span>
|
||||
</Label>
|
||||
<Switch id="dark-mode" disabled />
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Görünüm</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="flex items-center justify-between space-y-2">
|
||||
<Label htmlFor="dark-mode" className="flex flex-col space-y-1">
|
||||
<span>Karanlık Mod</span>
|
||||
<span className="font-normal text-xs text-muted-foreground">Koyu temayı etkinleştir.</span>
|
||||
</Label>
|
||||
<Switch
|
||||
id="dark-mode"
|
||||
checked={theme === 'dark'}
|
||||
onCheckedChange={(checked) => setTheme(checked ? 'dark' : 'light')}
|
||||
/>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
186
components/dashboard/category-form.tsx
Normal file
186
components/dashboard/category-form.tsx
Normal file
@@ -0,0 +1,186 @@
|
||||
"use client"
|
||||
|
||||
import { useState } from "react"
|
||||
import { useForm } from "react-hook-form"
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import * as z from "zod"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import { Input } from "@/components/ui/input"
|
||||
import { Textarea } from "@/components/ui/textarea"
|
||||
import {
|
||||
Form,
|
||||
FormControl,
|
||||
FormField,
|
||||
FormItem,
|
||||
FormLabel,
|
||||
FormMessage,
|
||||
} from "@/components/ui/form"
|
||||
import { toast } from "sonner"
|
||||
import { useRouter } from "next/navigation"
|
||||
import { createCategory, updateCategory, deleteCategory } from "@/app/(dashboard)/dashboard/categories/actions"
|
||||
import { Trash } from "lucide-react"
|
||||
import { AlertModal } from "@/components/modals/alert-modal"
|
||||
|
||||
const formSchema = z.object({
|
||||
name: z.string().min(2, "Kategori adı en az 2 karakter olmalıdır."),
|
||||
description: z.string().optional(),
|
||||
image_url: z.string().optional(),
|
||||
})
|
||||
|
||||
type CategoryFormValues = z.infer<typeof formSchema>
|
||||
|
||||
interface CategoryFormProps {
|
||||
initialData?: {
|
||||
id: string
|
||||
name: string
|
||||
description?: string
|
||||
image_url?: string
|
||||
} | null
|
||||
}
|
||||
|
||||
export function CategoryForm({ initialData }: CategoryFormProps) {
|
||||
const router = useRouter()
|
||||
const [open, setOpen] = useState(false)
|
||||
const [loading, setLoading] = useState(false)
|
||||
|
||||
const title = initialData ? "Kategoriyi Düzenle" : "Yeni Kategori"
|
||||
const description = initialData ? "Kategori detaylarını düzenleyin." : "Yeni bir kategori ekleyin."
|
||||
const toastMessage = initialData ? "Kategori güncellendi." : "Kategori oluşturuldu."
|
||||
const action = initialData ? "Kaydet" : "Oluştur"
|
||||
|
||||
const form = useForm<CategoryFormValues>({
|
||||
resolver: zodResolver(formSchema),
|
||||
defaultValues: initialData || {
|
||||
name: "",
|
||||
description: "",
|
||||
image_url: "",
|
||||
},
|
||||
})
|
||||
|
||||
const onSubmit = async (data: CategoryFormValues) => {
|
||||
setLoading(true)
|
||||
try {
|
||||
if (initialData) {
|
||||
const result = await updateCategory(initialData.id, data)
|
||||
if ((result as any).error) {
|
||||
toast.error((result as any).error)
|
||||
} else {
|
||||
toast.success(toastMessage)
|
||||
router.push(`/dashboard/categories`)
|
||||
router.refresh()
|
||||
}
|
||||
} else {
|
||||
const result = await createCategory(data)
|
||||
if ((result as any).error) {
|
||||
toast.error((result as any).error)
|
||||
} else {
|
||||
toast.success(toastMessage)
|
||||
router.push(`/dashboard/categories`)
|
||||
router.refresh()
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
toast.error("Bir hata oluştu.")
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
const onDelete = async () => {
|
||||
setLoading(true)
|
||||
try {
|
||||
const result = await deleteCategory(initialData!.id)
|
||||
if ((result as any).error) {
|
||||
toast.error((result as any).error)
|
||||
} else {
|
||||
toast.success("Kategori silindi.")
|
||||
router.push(`/dashboard/categories`)
|
||||
router.refresh()
|
||||
}
|
||||
} catch (error) {
|
||||
toast.error("Silme işlemi başarısız.")
|
||||
} finally {
|
||||
setLoading(false)
|
||||
setOpen(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<AlertModal
|
||||
isOpen={open}
|
||||
onClose={() => setOpen(false)}
|
||||
onConfirm={onDelete}
|
||||
loading={loading}
|
||||
/>
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="space-y-1">
|
||||
<h2 className="text-2xl font-bold tracking-tight">{title}</h2>
|
||||
<p className="text-sm text-muted-foreground">{description}</p>
|
||||
</div>
|
||||
{initialData && (
|
||||
<Button
|
||||
variant="destructive"
|
||||
size="sm"
|
||||
onClick={() => setOpen(true)}
|
||||
disabled={loading}
|
||||
>
|
||||
<Trash className="h-4 w-4" />
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
<div className="p-4 border rounded-md mt-4">
|
||||
<Form {...form}>
|
||||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8 w-full">
|
||||
<div className="grid grid-cols-1 gap-8 md:grid-cols-2">
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="name"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Başlık</FormLabel>
|
||||
<FormControl>
|
||||
<Input disabled={loading} placeholder="Kategori adı..." {...field} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="image_url"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Görsel URL (Opsiyonel)</FormLabel>
|
||||
<FormControl>
|
||||
<Input disabled={loading} placeholder="https://..." {...field} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<div className="col-span-1 md:col-span-2">
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="description"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Açıklama</FormLabel>
|
||||
<FormControl>
|
||||
<Textarea disabled={loading} placeholder="Kategori açıklaması..." {...field} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<Button disabled={loading} className="ml-auto" type="submit">
|
||||
{action}
|
||||
</Button>
|
||||
</form>
|
||||
</Form>
|
||||
</div>
|
||||
</>
|
||||
)
|
||||
}
|
||||
200
components/dashboard/product-form.tsx
Normal file
200
components/dashboard/product-form.tsx
Normal file
@@ -0,0 +1,200 @@
|
||||
"use client"
|
||||
|
||||
import { useState } from "react"
|
||||
import { useForm } from "react-hook-form"
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import * as z from "zod"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import {
|
||||
Form,
|
||||
FormControl,
|
||||
FormDescription,
|
||||
FormField,
|
||||
FormItem,
|
||||
FormLabel,
|
||||
FormMessage,
|
||||
} from "@/components/ui/form"
|
||||
import { Input } from "@/components/ui/input"
|
||||
import { Textarea } from "@/components/ui/textarea"
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from "@/components/ui/select"
|
||||
import { useRouter } from "next/navigation"
|
||||
import { toast } from "sonner"
|
||||
import { Loader2 } from "lucide-react"
|
||||
|
||||
const productSchema = z.object({
|
||||
name: z.string().min(2, "Ürün adı en az 2 karakter olmalıdır"),
|
||||
category: z.string().min(1, "Kategori seçiniz"),
|
||||
description: z.string().optional(),
|
||||
price: z.coerce.number().min(0, "Fiyat 0'dan küçük olamaz"),
|
||||
image_url: z.string().optional(),
|
||||
})
|
||||
|
||||
type ProductFormValues = z.infer<typeof productSchema>
|
||||
|
||||
import { createProduct, updateProduct } from "@/app/(dashboard)/dashboard/products/actions"
|
||||
|
||||
// Define the shape of data coming from Supabase
|
||||
interface Product {
|
||||
id: number
|
||||
name: string
|
||||
category: string
|
||||
description: string | null
|
||||
price: number
|
||||
image_url: string | null
|
||||
created_at: string
|
||||
}
|
||||
|
||||
interface ProductFormProps {
|
||||
initialData?: Product
|
||||
}
|
||||
|
||||
export function ProductForm({ initialData }: ProductFormProps) {
|
||||
const router = useRouter()
|
||||
const [loading, setLoading] = useState(false)
|
||||
|
||||
const form = useForm<ProductFormValues>({
|
||||
resolver: zodResolver(productSchema) as any,
|
||||
defaultValues: initialData ? {
|
||||
name: initialData.name,
|
||||
category: initialData.category,
|
||||
description: initialData.description || "",
|
||||
price: initialData.price,
|
||||
image_url: initialData.image_url || "",
|
||||
} : {
|
||||
name: "",
|
||||
category: "",
|
||||
description: "",
|
||||
price: 0,
|
||||
image_url: "",
|
||||
},
|
||||
})
|
||||
|
||||
async function onSubmit(data: ProductFormValues) {
|
||||
try {
|
||||
setLoading(true)
|
||||
|
||||
let result
|
||||
if (initialData) {
|
||||
result = await updateProduct(initialData.id, data)
|
||||
} else {
|
||||
result = await createProduct(data)
|
||||
}
|
||||
|
||||
if (!result.success) {
|
||||
toast.error(result.error || "Bir hata oluştu")
|
||||
return
|
||||
}
|
||||
|
||||
toast.success(initialData ? "Ürün güncellendi" : "Ürün başarıyla oluşturuldu")
|
||||
router.push("/dashboard/products")
|
||||
router.refresh()
|
||||
} catch (error) {
|
||||
toast.error("Bir aksilik oldu")
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Form {...form}>
|
||||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8 w-full max-w-2xl">
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="name"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Ürün Adı</FormLabel>
|
||||
<FormControl>
|
||||
<Input placeholder="Çelik Kasa Model X" {...field} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="category"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Kategori</FormLabel>
|
||||
<Select onValueChange={field.onChange} defaultValue={field.value}>
|
||||
<FormControl>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="Kategori seçin" />
|
||||
</SelectTrigger>
|
||||
</FormControl>
|
||||
<SelectContent>
|
||||
<SelectItem value="ev">Ev Tipi</SelectItem>
|
||||
<SelectItem value="ofis">Ofis Tipi</SelectItem>
|
||||
<SelectItem value="otel">Otel Kasası</SelectItem>
|
||||
<SelectItem value="ozel">Özel Üretim</SelectItem>
|
||||
<SelectItem value="diger">Diğer</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="price"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Fiyat (₺)</FormLabel>
|
||||
<FormControl>
|
||||
<Input type="number" placeholder="0.00" {...field} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="image_url"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Görsel URL (Opsiyonel)</FormLabel>
|
||||
<FormControl>
|
||||
<Input placeholder="https://..." {...field} />
|
||||
</FormControl>
|
||||
<FormDescription>
|
||||
Ürün görseli için şimdilik dış bağlantı kullanın.
|
||||
</FormDescription>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="description"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Açıklama</FormLabel>
|
||||
<FormControl>
|
||||
<Textarea placeholder="Ürün özellikleri..." {...field} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<Button type="submit" disabled={loading}>
|
||||
{loading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
|
||||
{initialData ? "Güncelle" : "Oluştur"}
|
||||
</Button>
|
||||
</form>
|
||||
</Form>
|
||||
)
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
import Link from "next/link"
|
||||
import { usePathname } from "next/navigation"
|
||||
import { cn } from "@/lib/utils"
|
||||
import { LayoutDashboard, Package, ShoppingCart, Users, Settings } from "lucide-react"
|
||||
import { LayoutDashboard, Package, ShoppingCart, Users, Settings, Globe, Tags } from "lucide-react"
|
||||
|
||||
const sidebarItems = [
|
||||
{
|
||||
@@ -21,6 +21,11 @@ const sidebarItems = [
|
||||
href: "/dashboard/orders",
|
||||
icon: ShoppingCart,
|
||||
},
|
||||
{
|
||||
title: "Kategoriler",
|
||||
href: "/dashboard/categories",
|
||||
icon: Tags,
|
||||
},
|
||||
{
|
||||
title: "Kullanıcılar",
|
||||
href: "/dashboard/users",
|
||||
@@ -31,6 +36,11 @@ const sidebarItems = [
|
||||
href: "/dashboard/settings",
|
||||
icon: Settings,
|
||||
},
|
||||
{
|
||||
title: "Siteye Dön",
|
||||
href: "/",
|
||||
icon: Globe,
|
||||
},
|
||||
]
|
||||
|
||||
interface SidebarProps extends React.HTMLAttributes<HTMLDivElement> { }
|
||||
|
||||
164
components/dashboard/site-settings-form.tsx
Normal file
164
components/dashboard/site-settings-form.tsx
Normal file
@@ -0,0 +1,164 @@
|
||||
"use client"
|
||||
|
||||
import { useState } from "react"
|
||||
import { useRouter } from "next/navigation"
|
||||
import { useForm } from "react-hook-form"
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import * as z from "zod"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import { Input } from "@/components/ui/input"
|
||||
import { Textarea } from "@/components/ui/textarea"
|
||||
import {
|
||||
Form,
|
||||
FormControl,
|
||||
FormDescription,
|
||||
FormField,
|
||||
FormItem,
|
||||
FormLabel,
|
||||
FormMessage,
|
||||
} from "@/components/ui/form"
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
|
||||
import { toast } from "sonner"
|
||||
import { Loader2 } from "lucide-react"
|
||||
import { updateSiteSettings } from "@/app/(dashboard)/dashboard/settings/actions"
|
||||
|
||||
const settingsSchema = z.object({
|
||||
site_title: z.string().min(2, "Site başlığı en az 2 karakter olmalıdır."),
|
||||
site_description: z.string().optional(),
|
||||
contact_email: z.string().email("Geçerli bir e-posta adresi giriniz.").optional().or(z.literal("")),
|
||||
contact_phone: z.string().optional(),
|
||||
currency: z.string().default("TRY"),
|
||||
})
|
||||
|
||||
type SettingsFormValues = z.infer<typeof settingsSchema>
|
||||
|
||||
interface SiteSettingsFormProps {
|
||||
initialData: any
|
||||
}
|
||||
|
||||
export function SiteSettingsForm({ initialData }: SiteSettingsFormProps) {
|
||||
const router = useRouter()
|
||||
const [loading, setLoading] = useState(false)
|
||||
|
||||
const form = useForm<SettingsFormValues>({
|
||||
resolver: zodResolver(settingsSchema),
|
||||
defaultValues: {
|
||||
site_title: initialData?.site_title || "ParaKasa",
|
||||
site_description: initialData?.site_description || "",
|
||||
contact_email: initialData?.contact_email || "",
|
||||
contact_phone: initialData?.contact_phone || "",
|
||||
currency: initialData?.currency || "TRY",
|
||||
},
|
||||
})
|
||||
|
||||
const onSubmit = async (data: SettingsFormValues) => {
|
||||
setLoading(true)
|
||||
try {
|
||||
// @ts-ignore
|
||||
const result = await updateSiteSettings(data)
|
||||
|
||||
if (result.error) {
|
||||
toast.error(result.error)
|
||||
return
|
||||
}
|
||||
|
||||
toast.success("Site ayarları güncellendi.")
|
||||
router.refresh()
|
||||
} catch (error) {
|
||||
toast.error("Bir sorun oluştu.")
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Genel Ayarlar</CardTitle>
|
||||
<CardDescription>Web sitesinin genel yapılandırma ayarları.</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<Form {...form}>
|
||||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="site_title"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Site Başlığı</FormLabel>
|
||||
<FormControl>
|
||||
<Input placeholder="ParaKasa" {...field} />
|
||||
</FormControl>
|
||||
<FormDescription>Tarayıcı sekmesinde görünen ad.</FormDescription>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="site_description"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Site Açıklaması</FormLabel>
|
||||
<FormControl>
|
||||
<Textarea placeholder="Premium çelik kasalar..." {...field} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="contact_email"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>İletişim E-posta</FormLabel>
|
||||
<FormControl>
|
||||
<Input placeholder="info@parakasa.com" {...field} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="contact_phone"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>İletişim Telefon</FormLabel>
|
||||
<FormControl>
|
||||
<Input placeholder="+90 555 123 45 67" {...field} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="currency"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Para Birimi</FormLabel>
|
||||
<FormControl>
|
||||
<Input placeholder="TRY" {...field} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<Button type="submit" disabled={loading}>
|
||||
{loading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
|
||||
Ayarları Kaydet
|
||||
</Button>
|
||||
</form>
|
||||
</Form>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
201
components/dashboard/user-form.tsx
Normal file
201
components/dashboard/user-form.tsx
Normal file
@@ -0,0 +1,201 @@
|
||||
"use client"
|
||||
|
||||
import { useState } from "react"
|
||||
import { useRouter } from "next/navigation"
|
||||
import { useForm } from "react-hook-form"
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import * as z from "zod"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import { Input } from "@/components/ui/input"
|
||||
import {
|
||||
Form,
|
||||
FormControl,
|
||||
FormField,
|
||||
FormItem,
|
||||
FormLabel,
|
||||
FormMessage,
|
||||
} from "@/components/ui/form"
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from "@/components/ui/select"
|
||||
import { Card, CardContent } from "@/components/ui/card"
|
||||
import { toast } from "sonner"
|
||||
import { Loader2 } from "lucide-react"
|
||||
import { createUser, updateUser } from "@/app/(dashboard)/dashboard/users/actions"
|
||||
|
||||
const userSchema = z.object({
|
||||
firstName: z.string().min(2, "Ad en az 2 karakter olmalıdır."),
|
||||
lastName: z.string().min(2, "Soyad en az 2 karakter olmalıdır."),
|
||||
email: z.string().email("Geçerli bir e-posta adresi giriniz."),
|
||||
password: z.string().optional(), // Password is optional on edit
|
||||
role: z.enum(["admin", "user"]),
|
||||
}).refine((data) => {
|
||||
// If we are creating a NEW user (no ID passed in props effectively, but schema doesn't know props),
|
||||
// we generally want password required. But here we'll handle it in the component logic or strictly separate schemas.
|
||||
// For simplicity, we make password optional in Zod but check it in onSubmit if it's a create action.
|
||||
return true
|
||||
})
|
||||
|
||||
type UserFormValues = z.infer<typeof userSchema>
|
||||
|
||||
interface UserFormProps {
|
||||
initialData?: {
|
||||
id: string
|
||||
firstName: string
|
||||
lastName: string
|
||||
email: string
|
||||
role: "admin" | "user"
|
||||
}
|
||||
}
|
||||
|
||||
export function UserForm({ initialData }: UserFormProps) {
|
||||
const router = useRouter()
|
||||
const [loading, setLoading] = useState(false)
|
||||
|
||||
const form = useForm<UserFormValues>({
|
||||
resolver: zodResolver(userSchema),
|
||||
defaultValues: initialData ? {
|
||||
firstName: initialData.firstName,
|
||||
lastName: initialData.lastName,
|
||||
email: initialData.email,
|
||||
password: "", // Empty password means no change
|
||||
role: initialData.role,
|
||||
} : {
|
||||
firstName: "",
|
||||
lastName: "",
|
||||
email: "",
|
||||
password: "",
|
||||
role: "user",
|
||||
},
|
||||
})
|
||||
|
||||
const onSubmit = async (data: UserFormValues) => {
|
||||
setLoading(true)
|
||||
try {
|
||||
let result;
|
||||
if (initialData) {
|
||||
// Update
|
||||
result = await updateUser(initialData.id, data)
|
||||
} else {
|
||||
// Create
|
||||
if (!data.password || data.password.length < 6) {
|
||||
toast.error("Yeni kullanıcı için şifre gereklidir (min 6 karakter).")
|
||||
setLoading(false)
|
||||
return
|
||||
}
|
||||
result = await createUser(data.firstName, data.lastName, data.email, data.password, data.role)
|
||||
}
|
||||
|
||||
if (result.error) {
|
||||
toast.error(result.error)
|
||||
return
|
||||
}
|
||||
|
||||
toast.success(initialData ? "Kullanıcı güncellendi." : "Kullanıcı oluşturuldu.")
|
||||
router.push("/dashboard/users")
|
||||
router.refresh()
|
||||
} catch (error) {
|
||||
toast.error("Bir sorun oluştu.")
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Card className="max-w-xl">
|
||||
<CardContent className="pt-6">
|
||||
<Form {...form}>
|
||||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="firstName"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Ad</FormLabel>
|
||||
<FormControl>
|
||||
<Input placeholder="Ahmet" {...field} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="lastName"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Soyad</FormLabel>
|
||||
<FormControl>
|
||||
<Input placeholder="Yılmaz" {...field} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="email"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>E-posta</FormLabel>
|
||||
<FormControl>
|
||||
<Input placeholder="ahmet@parakasa.com" {...field} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="password"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Şifre</FormLabel>
|
||||
<FormControl>
|
||||
<Input type="password" placeholder={initialData ? "Değiştirmek için yeni şifre girin" : "******"} {...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="user">Kullanıcı</SelectItem>
|
||||
<SelectItem value="admin">Yönetici</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<Button type="submit" disabled={loading} className="w-full">
|
||||
{loading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
|
||||
Kullanıcı Oluştur
|
||||
</Button>
|
||||
</form>
|
||||
</Form>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
AvatarImage,
|
||||
} from "@/components/ui/avatar"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import Link from "next/link"
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
@@ -49,12 +50,21 @@ export function UserNav() {
|
||||
</DropdownMenuLabel>
|
||||
<DropdownMenuSeparator />
|
||||
<DropdownMenuGroup>
|
||||
<DropdownMenuItem>
|
||||
Profil
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem>
|
||||
Ayarlar
|
||||
</DropdownMenuItem>
|
||||
<Link href="/dashboard/profile">
|
||||
<DropdownMenuItem className="cursor-pointer">
|
||||
Profil
|
||||
</DropdownMenuItem>
|
||||
</Link>
|
||||
<Link href="/dashboard/users">
|
||||
<DropdownMenuItem className="cursor-pointer">
|
||||
Kullanıcılar
|
||||
</DropdownMenuItem>
|
||||
</Link>
|
||||
<Link href="/dashboard/settings">
|
||||
<DropdownMenuItem className="cursor-pointer">
|
||||
Ayarlar
|
||||
</DropdownMenuItem>
|
||||
</Link>
|
||||
</DropdownMenuGroup>
|
||||
<DropdownMenuSeparator />
|
||||
<DropdownMenuItem onClick={handleSignOut}>
|
||||
|
||||
Reference in New Issue
Block a user