From 2d10101394fd07584bc4e2573ba7d883021f77a9 Mon Sep 17 00:00:00 2001 From: GoHorse Deploy Date: Fri, 6 Mar 2026 10:10:16 -0300 Subject: [PATCH] refactor: move company creation to separate page and improve users table --- .../src/app/dashboard/companies/new/page.tsx | 275 ++++++++++++++++++ frontend/src/app/dashboard/companies/page.tsx | 239 +-------------- frontend/src/app/dashboard/users/page.tsx | 10 +- 3 files changed, 286 insertions(+), 238 deletions(-) create mode 100644 frontend/src/app/dashboard/companies/new/page.tsx 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.title')}

+

{t('admin.companies.create.subtitle')}

+
+
+ + + +
+
+ + + setFormData({ + ...formData, + name: e.target.value, + slug: generateSlug(e.target.value), + }) + } + placeholder={t('admin.companies.create.namePlaceholder')} + /> +
+
+ + setFormData({ ...formData, slug: e.target.value })} + placeholder={t('admin.companies.create.slugPlaceholder')} + /> +
+
+ + setFormData({ ...formData, document: formatCNPJ(e.target.value) })} + placeholder="CNPJ / Document" + /> +
+
+ + setFormData({ ...formData, email: e.target.value })} + placeholder={t('admin.companies.create.emailPlaceholder')} + /> +
+
+
+ +
+ setFormData({ ...formData, password: e.target.value })} + placeholder="******" + /> + +
+
+
+ +
+ setFormData({ ...formData, confirmPassword: e.target.value })} + placeholder="******" + /> + +
+ {formData.password !== formData.confirmPassword && formData.confirmPassword && ( +

{t('admin.companies.fields.passwordsDoNotMatch')}

+ )} +
+
+
+ + setFormData({ ...formData, phone: formatPhone(e.target.value) })} + placeholder="+55 11 99999-9999" + /> +
+
+ + setFormData({ ...formData, website: e.target.value })} + placeholder="https://..." + /> +
+
+ + setFormData({ ...formData, address: e.target.value })} + placeholder="Address..." + /> +
+
+ +