diff --git a/shop/components/admin/create-product-dialog.tsx b/shop/components/admin/create-product-dialog.tsx index 2e69f68..2cd870a 100644 --- a/shop/components/admin/create-product-dialog.tsx +++ b/shop/components/admin/create-product-dialog.tsx @@ -26,7 +26,7 @@ import { Input } from '@/components/ui/input' import { Button } from '@/components/ui/button' import { Checkbox } from '@/components/ui/checkbox' import { Plus, Trash2, X, PlusCircle } from 'lucide-react' -import { createProduct } from '@/lib/actions/products' +import { createProduct, updateProduct } from '@/lib/actions/products' import { ScrollArea } from '@/components/ui/scroll-area' import { Separator } from '@/components/ui/separator' import { Category } from '@/lib/types' @@ -58,18 +58,25 @@ const productSchema = z.object({ type ProductFormValues = z.infer -export function CreateProductDialog({ categories }: { categories: Category[] }) { +export function CreateProductDialog({ children, categories, product }: { children?: React.ReactNode, categories: Category[], product?: Product }) { const [open, setOpen] = useState(false) const form = useForm({ resolver: zodResolver(productSchema) as any, defaultValues: { - name: '', - description: '', - base_price: 0, - category_id: '', - is_active: true, - modules: [], + name: product?.name || '', + description: product?.description || '', + base_price: product?.base_price || 0, + category_id: product?.category_id || '', + is_active: product?.is_active ?? true, + modules: product?.modules?.map(m => ({ + name: m.name, + description: m.description || '', + price: m.price, + is_required: m.is_required, + requirements: m.requirements?.join(', ') || '', + exclusions: m.exclusions?.join(', ') || '', + })) || [], }, }) @@ -86,24 +93,34 @@ export function CreateProductDialog({ categories }: { categories: Category[] }) requirements: m.requirements ? m.requirements.split(',').map(s => s.trim()).filter(Boolean) : [], exclusions: m.exclusions ? m.exclusions.split(',').map(s => s.trim()).filter(Boolean) : [], })) - await createProduct(productData as any, formattedModules as any) + + if (product) { + await updateProduct(product.id, productData as any, formattedModules as any) + } else { + await createProduct(productData as any, formattedModules as any) + } + setOpen(false) - form.reset() + if (!product) form.reset() } catch (error) { - console.error('Error creating product:', error) + console.error('Error saving product:', error) } } return ( - + {children || ( + + )} - Neues Produkt erstellen + + {product ? 'Produkt bearbeiten' : 'Neues Produkt erstellen'} + Definieren Sie das Basisprodukt und fügen Sie optionale oder erforderliche Module hinzu. @@ -111,7 +128,7 @@ export function CreateProductDialog({ categories }: { categories: Category[] })
- +
{ @@ -75,9 +77,11 @@ export function ProductList({ initialProducts }: { initialProducts: Product[] })
- + + +