From de546e00e031cb783de3440ed5ec6f7245288065 Mon Sep 17 00:00:00 2001 From: Marcos Fargas Date: Thu, 15 Oct 2026 03:40:22 +0200 Subject: [PATCH] fix(context): recarga completa y refresco automatico al cambiar de cliente en Contexto --- src/components/CalendarPortfolioBoard.tsx | 5 +- src/components/TenantSwitcher.tsx | 86 ++++++++++++++++------- 2 files changed, 62 insertions(+), 29 deletions(-) diff --git a/src/components/CalendarPortfolioBoard.tsx b/src/components/CalendarPortfolioBoard.tsx index 393dcac..9e5b5b9 100644 --- a/src/components/CalendarPortfolioBoard.tsx +++ b/src/components/CalendarPortfolioBoard.tsx @@ -66,10 +66,7 @@ export default function CalendarPortfolioBoard({ const href = latest ? `/dashboard/calendario?view=archive&month=${latest}` : '/dashboard/calendario?view=archive'; - startTransition(() => { - router.push(href); - router.refresh(); - }); + window.location.href = href; } return ( diff --git a/src/components/TenantSwitcher.tsx b/src/components/TenantSwitcher.tsx index d3a81a1..c77e51b 100644 --- a/src/components/TenantSwitcher.tsx +++ b/src/components/TenantSwitcher.tsx @@ -1,8 +1,7 @@ 'use client'; -import React, { useEffect, useState, useTransition } from 'react'; -import { useRouter } from 'next/navigation'; -import { ChevronDown, Eye, Building2 } from 'lucide-react'; +import React, { useEffect, useState } from 'react'; +import { ChevronDown, Eye, Building2, Loader2, Check } from 'lucide-react'; type TenantOpt = { id: string; name: string; slug: string }; @@ -13,12 +12,11 @@ export default function TenantSwitcher({ initialName: string; isAgency: boolean; }) { - const router = useRouter(); const [open, setOpen] = useState(false); const [tenants, setTenants] = useState([]); const [activeName, setActiveName] = useState(initialName); const [activeId, setActiveId] = useState(null); - const [pending, startTransition] = useTransition(); + const [switching, setSwitching] = useState(false); useEffect(() => { if (!isAgency) return; @@ -52,39 +50,74 @@ export default function TenantSwitcher({ async function select(tenant: TenantOpt | null) { setOpen(false); - await fetch('/api/admin/active-tenant', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ tenantId: tenant?.id ?? null }), - }); - setActiveId(tenant?.id ?? null); - setActiveName(tenant?.name ?? 'Vista global agencia'); - startTransition(() => router.refresh()); + setSwitching(true); + try { + await fetch('/api/admin/active-tenant', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ tenantId: tenant?.id ?? null }), + }); + setActiveId(tenant?.id ?? null); + setActiveName(tenant?.name ?? 'Vista global agencia'); + + // Limpiar query params de tenant para que no haya conflicto + const currentUrl = new URL(window.location.href); + const hadTenantParam = + currentUrl.searchParams.has('tenant') || + currentUrl.searchParams.has('tenant_id') || + currentUrl.searchParams.has('tenantId'); + + currentUrl.searchParams.delete('tenant'); + currentUrl.searchParams.delete('tenant_id'); + currentUrl.searchParams.delete('tenantId'); + + if (hadTenantParam) { + window.location.href = currentUrl.toString(); + } else { + window.location.reload(); + } + } catch { + window.location.reload(); + } } return (
+ {open && (
{tenants.length === 0 ? ( @@ -96,14 +129,17 @@ export default function TenantSwitcher({ )) )}