Sms Rehber eklemesi,Mobil uyumluluk ayarları,iletişim sayfası düzenlemeler vb.
This commit is contained in:
@@ -31,10 +31,11 @@ import imageCompression from 'browser-image-compression'
|
||||
import { createClient } from "@/lib/supabase-browser"
|
||||
import Image from "next/image"
|
||||
|
||||
import { createProduct, updateProduct } from "@/app/(dashboard)/dashboard/products/actions"
|
||||
import { createProduct, updateProduct, deleteProductImage } from "@/app/(dashboard)/dashboard/products/actions"
|
||||
|
||||
const productSchema = z.object({
|
||||
name: z.string().min(2, "Ürün adı en az 2 karakter olmalıdır"),
|
||||
product_code: z.string().optional(),
|
||||
category: z.string().min(1, "Kategori seçiniz"),
|
||||
description: z.string().optional(),
|
||||
price: z.coerce.number().min(0, "Fiyat 0'dan küçük olamaz"),
|
||||
@@ -50,6 +51,7 @@ type ProductFormValues = z.infer<typeof productSchema>
|
||||
interface Product {
|
||||
id: number
|
||||
name: string
|
||||
product_code?: string | null
|
||||
category: string
|
||||
description: string | null
|
||||
price: number
|
||||
@@ -80,6 +82,7 @@ export function ProductForm({ initialData }: ProductFormProps) {
|
||||
resolver: zodResolver(productSchema) as Resolver<ProductFormValues>,
|
||||
defaultValues: initialData ? {
|
||||
name: initialData.name,
|
||||
product_code: initialData.product_code || "",
|
||||
category: initialData.category,
|
||||
description: initialData.description || "",
|
||||
price: initialData.price,
|
||||
@@ -88,6 +91,7 @@ export function ProductForm({ initialData }: ProductFormProps) {
|
||||
images: initialData.image_url ? [initialData.image_url] : []
|
||||
} : {
|
||||
name: "",
|
||||
product_code: "",
|
||||
category: "",
|
||||
description: "",
|
||||
price: 0,
|
||||
@@ -159,16 +163,31 @@ export function ProductForm({ initialData }: ProductFormProps) {
|
||||
}
|
||||
}
|
||||
|
||||
const removeImage = (index: number) => {
|
||||
const removeImage = async (index: number) => {
|
||||
const imageToDelete = previewImages[index]
|
||||
|
||||
// If editing an existing product and image is from server (starts with http/https usually)
|
||||
if (initialData && imageToDelete.startsWith("http")) {
|
||||
const result = await deleteProductImage(imageToDelete, initialData.id)
|
||||
if (!result.success) {
|
||||
toast.error("Resim silinirken hata oluştu")
|
||||
return
|
||||
}
|
||||
toast.success("Resim silindi")
|
||||
}
|
||||
|
||||
const currentImages = [...form.getValues("images") || []]
|
||||
currentImages.splice(index, 1)
|
||||
form.setValue("images", currentImages)
|
||||
if (currentImages.length > 0) {
|
||||
form.setValue("image_url", currentImages[0])
|
||||
// Filter out the deleted image URL if it matches
|
||||
const newImages = currentImages.filter(url => url !== imageToDelete)
|
||||
|
||||
form.setValue("images", newImages)
|
||||
|
||||
if (newImages.length > 0) {
|
||||
form.setValue("image_url", newImages[0])
|
||||
} else {
|
||||
form.setValue("image_url", "")
|
||||
}
|
||||
setPreviewImages(currentImages)
|
||||
setPreviewImages(newImages)
|
||||
}
|
||||
|
||||
async function onSubmit(data: ProductFormValues) {
|
||||
@@ -224,7 +243,7 @@ export function ProductForm({ initialData }: ProductFormProps) {
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="name"
|
||||
@@ -238,7 +257,22 @@ export function ProductForm({ initialData }: ProductFormProps) {
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="product_code"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Ürün Kodu</FormLabel>
|
||||
<FormControl>
|
||||
<Input placeholder="KOD-123" {...field} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="category"
|
||||
|
||||
Reference in New Issue
Block a user