diff --git a/src/components/campaign/municipality/MunicipalityListLevelControl.tsx b/src/components/campaign/municipality/MunicipalityListLevelControl.tsx index c40cd2de..771cc63d 100644 --- a/src/components/campaign/municipality/MunicipalityListLevelControl.tsx +++ b/src/components/campaign/municipality/MunicipalityListLevelControl.tsx @@ -12,7 +12,6 @@ import { useCampaignCellFailureChannel } from '@/components/campaign/shared/useC import { Alert, AlertDescription } from '@/components/ui/Alert' import { Button } from '@/components/ui/button' import { Checkbox } from '@/components/ui/Checkbox' -import { DrawerCloseButton } from '@/components/ui/Drawer' import { Field, FieldContent, FieldLabel } from '@/components/ui/field' import { NativeSelect, NativeSelectOption } from '@/components/ui/native-select' import { Spinner } from '@/components/ui/Spinner' @@ -239,7 +238,14 @@ export const MunicipalityListLevelControl = ({ isSheet && open ? ( <> {submitButton} - Cancelar + ) : undefined } diff --git a/src/components/campaign/municipality/MunicipalityListSignalControl.tsx b/src/components/campaign/municipality/MunicipalityListSignalControl.tsx index 2cbe9adf..40c6d409 100644 --- a/src/components/campaign/municipality/MunicipalityListSignalControl.tsx +++ b/src/components/campaign/municipality/MunicipalityListSignalControl.tsx @@ -10,7 +10,6 @@ import { import { CampaignFormActionMessage } from '@/components/campaign/shared/CampaignFormActionMessage' import { useCampaignFormSuccessToast } from '@/components/campaign/shared/useCampaignFormSuccessToast' import { Button } from '@/components/ui/button' -import { DrawerCloseButton } from '@/components/ui/Drawer' import { Spinner } from '@/components/ui/Spinner' import type { CampaignFormActionState } from '@/utilities/campaignFormActionError' import { @@ -85,7 +84,14 @@ export const MunicipalityListSignalControl = ({ isSheet && open ? ( <> {submitButton} - Cancelar + ) : undefined } diff --git a/src/components/campaign/shared/CampaignCellEditOverlay.tsx b/src/components/campaign/shared/CampaignCellEditOverlay.tsx index 9bb86bc6..f1a0b8fa 100644 --- a/src/components/campaign/shared/CampaignCellEditOverlay.tsx +++ b/src/components/campaign/shared/CampaignCellEditOverlay.tsx @@ -179,7 +179,7 @@ export const CampaignCellEditOverlay = ({ wasOpenRef.current = open if (!open) { - if (wasOpen) sharedSheet.dismissSheet(onOpenChange) + if (wasOpen) sharedSheet.dismissActiveSheet() return } @@ -204,6 +204,7 @@ export const CampaignCellEditOverlay = ({ {triggerButton} {liveRegion} {showPortals && bodyTarget ? createPortal(children, bodyTarget) : null} + {/* Portaled footer is outside the Drawer React tree — no DrawerCloseButton (B119). */} {showPortals && footerTarget && footer ? createPortal(footer, footerTarget) : null} ) diff --git a/src/components/campaign/shared/CampaignListSheetHost.tsx b/src/components/campaign/shared/CampaignListSheetHost.tsx index 2d882e7a..381b06bd 100644 --- a/src/components/campaign/shared/CampaignListSheetHost.tsx +++ b/src/components/campaign/shared/CampaignListSheetHost.tsx @@ -36,6 +36,8 @@ type CampaignListSheetContextValue = { /** Bumps when the drawer body/footer mount targets attach — portals read this. */ portalRevision: number openSheet: (chrome: CampaignListSheetChrome) => void + /** Dismiss whichever cell opened the shared sheet (B119 — stable ref, not callback identity). */ + dismissActiveSheet: () => void dismissSheet: (onOpenChange: (open: boolean) => void) => void isActiveSheet: (onOpenChange: (open: boolean) => void) => boolean } @@ -68,14 +70,22 @@ export const CampaignListSheetProvider = ({ children }: { children: ReactNode }) if (node) setPortalRevision((value) => value + 1) }, []) - const dismissSheet = useCallback((onOpenChange: (open: boolean) => void) => { - if (activeOnOpenChangeRef.current !== onOpenChange) return + const dismissActiveSheet = useCallback(() => { + const onOpenChange = activeOnOpenChangeRef.current setOpen(false) activeOnOpenChangeRef.current = null setChrome(null) - onOpenChange(false) + onOpenChange?.(false) }, []) + const dismissSheet = useCallback( + (onOpenChange: (open: boolean) => void) => { + if (activeOnOpenChangeRef.current !== onOpenChange) return + dismissActiveSheet() + }, + [dismissActiveSheet], + ) + const openSheet = useCallback((next: CampaignListSheetChrome) => { const previousOnOpenChange = activeOnOpenChangeRef.current if (previousOnOpenChange && previousOnOpenChange !== next.onOpenChange) { @@ -107,15 +117,14 @@ export const CampaignListSheetProvider = ({ children }: { children: ReactNode }) const handleOpenChange = useCallback( (nextOpen: boolean) => { if (nextOpen) return - const onOpenChange = activeOnOpenChangeRef.current - if (!onOpenChange) { + if (!activeOnOpenChangeRef.current) { setOpen(false) setChrome(null) return } - dismissSheet(onOpenChange) + dismissActiveSheet() }, - [dismissSheet], + [dismissActiveSheet], ) const contextValue = useMemo( @@ -124,10 +133,11 @@ export const CampaignListSheetProvider = ({ children }: { children: ReactNode }) footerPortalRef, portalRevision, openSheet, + dismissActiveSheet, dismissSheet, isActiveSheet, }), - [portalRevision, openSheet, dismissSheet, isActiveSheet], + [portalRevision, openSheet, dismissActiveSheet, dismissSheet, isActiveSheet], ) return ( diff --git a/tests/e2e/campaignMunicipalities.e2e.spec.ts b/tests/e2e/campaignMunicipalities.e2e.spec.ts index a569125f..f932bce4 100644 --- a/tests/e2e/campaignMunicipalities.e2e.spec.ts +++ b/tests/e2e/campaignMunicipalities.e2e.spec.ts @@ -430,6 +430,33 @@ test.describe('Municípios — cards no celular (B42)', () => { await expect(page).toHaveURL(`${campaign.baseURL}/campanha/municipios/${municipality.slug}`) await expect(page.getByRole('heading', { name: municipality.name })).toBeVisible() }) + + test('opens the nivel Drawer without navigating or crashing (B119)', async ({ + campaign, + page, + }) => { + const { fixtures } = campaign + const coordinator = await fixtures.createCampaignUser('coordinator', { + name: fixtures.value('Coordenador Mobile Nivel'), + }) + const municipality = await fixtures.claimMunicipality() + + await campaign.login(page, coordinator.email!, coordinator.password) + const listURL = `${campaign.baseURL}/campanha/municipios?q=${encodeURIComponent(municipality.name)}` + await page.goto(listURL) + + const card = page.locator('[data-view="mobile-cards"] article').first() + await expect(card).toBeVisible() + + await card.getByRole('button', { name: 'Nível de envolvimento' }).click() + const drawer = page.getByRole('dialog', { name: 'Registrar nível de envolvimento' }) + await expect(drawer).toBeVisible() + await expect(drawer.getByText(municipality.name, { exact: true })).toBeVisible() + await expect(page).toHaveURL(listURL) + + await drawer.getByRole('button', { name: 'Cancelar' }).click() + await expect(drawer).toBeHidden() + }) }) /** diff --git a/tests/unit/campaignCellEditOverlay.unit.spec.ts b/tests/unit/campaignCellEditOverlay.unit.spec.ts index b9455b4c..4b2fdc50 100644 --- a/tests/unit/campaignCellEditOverlay.unit.spec.ts +++ b/tests/unit/campaignCellEditOverlay.unit.spec.ts @@ -8,6 +8,7 @@ import { MunicipalityListLevelControl } from '@/components/campaign/municipality import { MunicipalityListSignalControl } from '@/components/campaign/municipality/MunicipalityListSignalControl' import { MunicipalityListTrendControl } from '@/components/campaign/municipality/MunicipalityListTrendControl' import type { CampaignCellEditOverlayVariant } from '@/components/campaign/shared/CampaignCellEditOverlay' +import { CampaignListSheetProvider } from '@/components/campaign/shared/CampaignListSheetHost' import { MunicipalityPortfolioCell } from '@/components/campaign/shared/MunicipalityPortfolioCell' import { TooltipProvider } from '@/components/ui/tooltip' import { toVoteEstimateScenarioViewModel } from '@/lib/voteEstimate' @@ -179,6 +180,30 @@ describe('campaign cell edit overlay', () => { }, ) + /** + * B119: the mobile cards host ONE shared Drawer (`CampaignListSheetProvider`) + * and portal cell footers into it. `DrawerCloseButton` must stay inside the + * Drawer React tree — portaled footers call `onOpenChange(false)` instead. + */ + it('opens the nivel sheet through CampaignListSheetProvider without crashing', async () => { + const nivelCase = overlayCases.find((item) => item.name === 'nível de envolvimento') + expect(nivelCase).toBeDefined() + + renderWithTooltip(createElement(CampaignListSheetProvider, null, nivelCase!.element('sheet'))) + + fireEvent.click(screen.getByRole('button', { name: nivelCase!.triggerLabel })) + + const dialog = await screen.findByRole('dialog') + expect(dialog.querySelector('[data-slot="drawer-title"]')?.textContent).toBe( + nivelCase!.drawerTitle, + ) + expect(dialog.textContent).toContain('Cancelar') + expect(dialog.textContent).toContain(nivelCase!.footerLabel) + + fireEvent.click(screen.getByRole('button', { name: 'Cancelar' })) + await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull()) + }) + it.each(overlayCases)( 'keeps $name in a Popover on the desktop table', async ({ triggerLabel, element }) => {