import { z } from 'zod'; import { t } from 'i18next'; import { toast } from 'sonner'; import { useMemo } from 'react'; import { zodResolver } from '@hookform/resolvers/zod'; import { SubmitHandler, useForm } from 'react-hook-form'; import { ASSESSMENT_STATUSES_MAP, UPDATEABLE_ASSESSMENT_STATUSES } from '@ballerine/common'; import { ctw, Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, TextArea, } from '@ballerine/ui'; import { useToggle } from '@/common/hooks/useToggle/useToggle'; import { Form } from '@/common/components/organisms/Form/Form'; import { Button } from '@/common/components/atoms/Button/Button'; import { FormItem } from '@/common/components/organisms/Form/Form.Item'; import { FormField } from '@/common/components/organisms/Form/Form.Field'; import { FormLabel } from '@/common/components/organisms/Form/Form.Label'; import { FormControl } from '@/common/components/organisms/Form/Form.Control'; import { FormMessage } from '@/common/components/organisms/Form/Form.Message'; import { KybAndUboChecksStatusButton } from './KybAndOwnershipAssessmentStatusButton'; import { useKybAndOwnershipStatusDialog } from './hooks/useKybAndOwnershipStatusDialog/useKybAndOwnershipStatusDialog'; import { useUpdateKybAndOwnershipAssessmentStatus } from './hooks/useUpdateKybAndOwnershipAssessmentStatus/useUpdateKybAndOwnershipAssessmentStatus'; import { KybAndOwnershipAssessmentStatusBadge, statusToData, } from './KybAndOwnershipAssessmentStatusBadge'; const AssessmentCompletedStatusFormSchema = z.object({ text: z.string().min(1, { message: 'Please provide additional details' }), }); export const KybAndOwnershipAssessmentStatus = ({ status, assessmentId, className, }: { assessmentId?: string; className?: string; status?: keyof typeof statusToData; }) => { const { mutate: mutateUpdateAssessmentStatus, isLoading: isUpdatingAssessmentStatus } = useUpdateKybAndOwnershipAssessmentStatus(); const formDefaultValues = { text: '', } satisfies z.infer; const form = useForm({ resolver: zodResolver(AssessmentCompletedStatusFormSchema), defaultValues: formDefaultValues, }); const [isStatusDropdownOpen, toggleStatusDropdownOpen] = useToggle(false); const { dialogState, toggleDialogOpenState, closeDialog } = useKybAndOwnershipStatusDialog(); const onSubmit: SubmitHandler> = async ({ text, }) => { if (!dialogState.status) { console.error('No status selected'); toast.error(t(`toast:assessment_status_update.unexpected_error`)); return; } mutateUpdateAssessmentStatus( { assessmentId, status: dialogState.status }, { onSuccess: () => { closeDialog(); form.reset(); }, }, ); }; const disabled = useMemo( () => isUpdatingAssessmentStatus || (status && [ ASSESSMENT_STATUSES_MAP['in-progress'], ASSESSMENT_STATUSES_MAP['approved'], ASSESSMENT_STATUSES_MAP['rejected'], ].includes(status)), [isUpdatingAssessmentStatus, status], ); if (!status || !assessmentId) { return null; } return ( toggleDialogOpenState()}> { if (dialogState.isOpen) { e.stopPropagation(); e.preventDefault(); } closeDialog(); }} > {UPDATEABLE_ASSESSMENT_STATUSES.map(selectableStatus => ( { if ( [ASSESSMENT_STATUSES_MAP.approved, ASSESSMENT_STATUSES_MAP.rejected].includes( selectableStatus, ) ) { setTimeout(() => { toggleDialogOpenState(selectableStatus); }, 0); return; } mutateUpdateAssessmentStatus({ assessmentId, status: selectableStatus }); toggleStatusDropdownOpen(); }} /> ))} { event.preventDefault(); document.body.style.pointerEvents = ''; }} > Confirm Review Completion Please provide any relevant details or findings regarding the review. This can include notes or conclusions drawn from the investigation.
{dialogState.status && (
Resolution Status
)} ( Additional details