"use client" import { useState } from "react" import { useRouter } from "next/navigation" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { useToast } from "@/components/ui/use-toast" import { criarCheckout } from "@/lib/actions" type Metodo = "PIX" | "BOLETO" | "CREDIT_CARD" export function ProductBuyForm({ produtoId, }: { produtoId: string }) { const router = useRouter() const { toast } = useToast() const [isLoading, setIsLoading] = useState(false) const [metodo, setMetodo] = useState("PIX") const [form, setForm] = useState({ nome: "", email: "", cpfCnpj: "", telefone: "", }) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setIsLoading(true) try { const res = await criarCheckout({ produtoId, cliente: { nome: form.nome, email: form.email, cpfCnpj: form.cpfCnpj.replace(/\D/g, ""), telefone: form.telefone, }, metodo, }) router.push(`/pedido/${res.pedidoId}`) } catch (error) { toast({ title: "Erro ao processar pagamento", description: error instanceof Error ? error.message : "Tente novamente.", variant: "destructive", }) setIsLoading(false) } } return (
setForm({ ...form, nome: e.target.value })} />
setForm({ ...form, email: e.target.value })} />
setForm({ ...form, cpfCnpj: e.target.value })} />
setForm({ ...form, telefone: e.target.value })} />
{(["PIX", "BOLETO", "CREDIT_CARD"] as Metodo[]).map((m) => ( ))}
) }