diff --git a/frontend/src/app/dashboard/companies/new/page.tsx b/frontend/src/app/dashboard/companies/new/page.tsx new file mode 100644 index 0000000..9782030 --- /dev/null +++ b/frontend/src/app/dashboard/companies/new/page.tsx @@ -0,0 +1,275 @@ +"use client" + +import { useState } from "react" +import { useRouter } from "next/navigation" +import { Button } from "@/components/ui/button" +import { Input } from "@/components/ui/input" +import { Textarea } from "@/components/ui/textarea" +import { Label } from "@/components/ui/label" +import { Loader2, Eye, EyeOff, ArrowLeft } from "lucide-react" +import { adminCompaniesApi } from "@/lib/api" +import { toast } from "sonner" +import { useTranslation } from "@/lib/i18n" +import { Card, CardContent } from "@/components/ui/card" +import Link from "next/link" + +const formatCNPJ = (value: string) => { + return value + .replace(/\D/g, "") + .replace(/^(\d{2})(\d)/, "$1.$2") + .replace(/^(\d{2})\.(\d{3})(\d)/, "$1.$2.$3") + .replace(/\.(\d{3})(\d)/, ".$1/$2") + .replace(/(\d{4})(\d)/, "$1-$2") + .substring(0, 18) +} + +const formatPhone = (value: string) => { + return value + .replace(/\D/g, "") + .replace(/^(\d{2})(\d)/, "($1) $2") + .replace(/(\d{5})(\d)/, "$1-$2") + .substring(0, 15) +} + +export default function NewCompanyPage() { + const { t } = useTranslation() + const router = useRouter() + const [creating, setCreating] = useState(false) + const [showPassword, setShowPassword] = useState(false) + const [showConfirmPassword, setShowConfirmPassword] = useState(false) + const [formData, setFormData] = useState({ + name: "", + slug: "", + email: "", + password: "", + confirmPassword: "", + document: "", + phone: "", + website: "", + address: "", + description: "", + logoUrl: "", + yearsInMarket: "", + }) + + const generateSlug = (name: string) => { + return name + .toLowerCase() + .normalize("NFD") + .replace(/[\u0300-\u036f]/g, "") + .replace(/[^a-z0-9]+/g, "-") + .replace(/(^-|-$)/g, "") + } + + const handleCreate = async () => { + try { + setCreating(true) + const payload = { + ...formData, + document: formData.document.replace(/\D/g, ''), + phone: formData.phone.replace(/\D/g, ''), + } + await adminCompaniesApi.create(payload) + toast.success(t('admin.companies.success.created')) + router.push("/dashboard/companies") + } catch (error: any) { + console.error("Error creating company:", error) + if (error.message?.includes("already exists")) { + toast.error(t('admin.companies.error.emailExists', { defaultValue: "User with this email already exists" })) + } else { + toast.error("Failed to create company") + } + } finally { + setCreating(false) + } + } + + return ( +
{t('admin.companies.create.subtitle')}
+{t('admin.companies.fields.passwordsDoNotMatch')}
+ )} +