import { useState } from 'react' import { useForm } from 'react-hook-form' import { z } from 'zod' import { zodResolver } from '@hookform/resolvers/zod' import { Building2, Check, Loader2, Plus } from 'lucide-react' import { authClient } from '../lib/auth-client' import { Badge, Button, Input, Label } from '../components/ui' const createOrgSchema = z.object({ name: z.string().min(2, 'Ingresá el nombre del grupo'), slug: z .string() .min(2, 'Mínimo 2 caracteres') .regex(/^[a-z0-9-]+$/, 'Solo minúsculas, números y guiones'), }) type CreateOrgValues = z.infer const API_URL = import.meta.env.VITE_API_URL ?? 'http://localhost:4000' type OrganizationRow = { id: string name: string slug: string logo: string | null } function OrganizationList({ onRefresh }: { onRefresh: () => void }) { const { data, isPending } = authClient.useListOrganizations() const [syncing, setSyncing] = useState(null) const [activeId, setActiveId] = useState(null) const organizations = (data ?? []) as OrganizationRow[] const handleSyncGroup = async (org: OrganizationRow) => { setSyncing(org.id) try { const res = await fetch(`${API_URL}/api/v1/groups/from-organization`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify({ organizationId: org.id }), }) const body = (await res.json()) as { message?: string } if (!res.ok) { onRefresh() setSyncing(null) return } void body } finally { setSyncing(null) } onRefresh() } const handleSetActive = async (organizationId: string) => { setActiveId(organizationId) await authClient.organization.setActive({ organizationId }) setActiveId(null) } if (isPending) { return } if (organizations.length === 0) { return

Todavía no pertenecés a ningún grupo.

} return (
    {organizations.map((org) => (
  • {org.name}

    /{org.slug}

    Owner
  • ))}
) } export function OrganizationsPage() { const [refreshKey, setRefreshKey] = useState(0) const [feedback, setFeedback] = useState(null) const { register, handleSubmit, reset, setError, formState: { errors, isSubmitting }, } = useForm({ resolver: zodResolver(createOrgSchema) }) const refreshOrganizations = () => setRefreshKey((k) => k + 1) const onCreateOrg = handleSubmit(async ({ name, slug }) => { setFeedback(null) const { error, data } = await authClient.organization.create({ name, slug }) if (error) { setError('root', { message: error.message ?? 'No se pudo crear el grupo' }) return } reset({ name: '', slug: '' }) setFeedback('Grupo creado. Sincronizalo con el cobro grupal.') refreshOrganizations() void data }) return (

Grupos

Creá un grupo para organizar tus cobros. Cada organización se vincula a un grupo.

{feedback ? (

{feedback}

) : null}

Crear grupo

{errors.name ?

{errors.name.message}

: null}
{errors.slug ?

{errors.slug.message}

: null}
{errors.root ?

{errors.root.message}

: null}

Tus grupos

) }