'use client' import { lazy, Suspense, useCallback, useEffect, useMemo, useRef, useState } from 'react' import { AlertTriangleIcon, UserRoundCogIcon } from 'lucide-react' import type { CoordinatorAssignmentFormState } from '@/app/(campaign)/campanha/(app)/nucleos/[slug]/coordinatorAssignmentFormActions' import { Alert, AlertDescription, AlertTitle } from '@/components/ui/Alert' import { Button } from '@/components/ui/button' import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger, } from '@/components/ui/dialog' import { Spinner } from '@/components/ui/Spinner' import type { AssignedCoordinatorViewModel } from '@/utilities/nucleusCoordinatorAssignmentPageData' import type { NucleusCoordinatorOption } from '@/utilities/nucleusCoordinatorOptions' type CoordinatorAssignmentDialogBodyModule = { default: typeof import('./CoordinatorAssignmentDialogBody').CoordinatorAssignmentDialogBody } const loadCoordinatorAssignmentDialogBody = (): Promise => import('./CoordinatorAssignmentDialogBody').then((module) => ({ default: module.CoordinatorAssignmentDialogBody, })) export type CoordinatorAssignmentFormAction = ( state: CoordinatorAssignmentFormState, formData: FormData, ) => Promise export type CoordinatorAssignmentOptionsResult = { expectedUpdatedAt: string options: NucleusCoordinatorOption[] } type CoordinatorAssignmentOptionsAction = () => Promise export const CoordinatorAssignmentDialog = ({ action, coordinators, initialOpen = false, loadBodyModule = loadCoordinatorAssignmentDialogBody, loadOptions, }: { action: CoordinatorAssignmentFormAction coordinators: AssignedCoordinatorViewModel[] initialOpen?: boolean loadBodyModule?: () => Promise loadOptions: CoordinatorAssignmentOptionsAction }) => { const [open, setOpen] = useState(initialOpen) const [result, setResult] = useState(null) const [error, setError] = useState(false) const [mutationPending, setMutationPending] = useState(false) const requestId = useRef(0) const LazyBody = useMemo(() => lazy(loadBodyModule), [loadBodyModule]) const triggerLabel = coordinators.length ? 'Alterar coordenadores' : 'Designar coordenadores' const load = useCallback(async () => { const currentRequest = ++requestId.current setResult(null) setError(false) try { const [, nextResult] = await Promise.all([loadBodyModule(), loadOptions()]) if (requestId.current === currentRequest) setResult(nextResult) } catch { if (requestId.current === currentRequest) setError(true) } }, [loadBodyModule, loadOptions]) useEffect(() => { if (open) void load() return () => { requestId.current += 1 } }, [load, open]) const closeDialog = () => { requestId.current += 1 setResult(null) setError(false) setMutationPending(false) setOpen(false) } const setDialogOpen = (nextOpen: boolean) => { if (!nextOpen) { if (mutationPending) return closeDialog() return } setOpen(true) } return ( { if (mutationPending) event.preventDefault() }} onPointerDownOutside={(event) => { if (mutationPending) event.preventDefault() }} > {triggerLabel} Selecione zero ou mais pessoas. Salvar sem seleção deixa o núcleo sem cobertura. {error ? ( ) : result ? ( ) : (
)}
) }