diff --git a/src/app/(dashboard)/pacientes/[id]/informacoes/page.tsx b/src/app/(dashboard)/pacientes/[id]/informacoes/page.tsx index 993a5f5..558aabe 100644 --- a/src/app/(dashboard)/pacientes/[id]/informacoes/page.tsx +++ b/src/app/(dashboard)/pacientes/[id]/informacoes/page.tsx @@ -1,12 +1,12 @@ -import { ForwardIcon, UserRoundIcon } from 'lucide-react' +import { UserRoundIcon } from 'lucide-react' import type { Metadata } from 'next' import { redirect } from 'next/navigation' import { getPatient } from '@/actions/patients/get-patient' -import { Button } from '@/components/ui/button' import { ROUTES } from '@/constants/routes' import { PatientForm } from '@/modules/patients/form' import { InactivatePatientButton } from '@/modules/patients/inactivate-button' +import { ReferPatientButton } from '@/modules/referrals/referral-button' export const metadata: Metadata = { title: 'Informações do paciente', @@ -45,10 +45,7 @@ export default async function Page({
{isPatientActive && } - +
diff --git a/src/modules/referrals/referral-button.tsx b/src/modules/referrals/referral-button.tsx new file mode 100644 index 0000000..06946e3 --- /dev/null +++ b/src/modules/referrals/referral-button.tsx @@ -0,0 +1,32 @@ +'use client' + +import { ForwardIcon } from 'lucide-react' +import { useState } from 'react' + +import { Dialog, DialogTrigger } from '@/components/ui/dialog' + +import type { ButtonProps } from '../../components/ui/button' +import { ReferralPatientModal } from './referrals-modal' + +interface ReferPatientButtonProps extends ButtonProps { + id?: string +} +export function ReferPatientButton({ + id, + ...props +}: Readonly) { + const [modalOpen, setModalOpen] = useState(false) + + return ( + + + + Encaminhar paciente + + + {modalOpen && ( + setModalOpen(false)} /> + )} + + ) +} diff --git a/src/modules/referrals/referrals-modal.tsx b/src/modules/referrals/referrals-modal.tsx new file mode 100644 index 0000000..3c8a22d --- /dev/null +++ b/src/modules/referrals/referrals-modal.tsx @@ -0,0 +1,170 @@ +'use client' + +import { zodResolver } from '@hookform/resolvers/zod' +import { ForwardIcon } from 'lucide-react' +import { FormProvider, useForm } from 'react-hook-form' +import { toast } from 'sonner' +import { z } from 'zod' + +import { revalidateCache } from '@/actions/cache' +import { ComboboxInput } from '@/components/form/combobox-input' +import { DateInput } from '@/components/form/date-input' +import { FormContainer } from '@/components/form/form-container' +import { SelectInput } from '@/components/form/select-input' +import { TextInput } from '@/components/form/text-input' +import { TextareaInput } from '@/components/form/textarea-input' +import { Button } from '@/components/ui/button' +import { + DialogClose, + DialogContainer, + DialogContent, + DialogFooter, + DialogHeader, + DialogIcon, + DialogTitle, +} from '@/components/ui/dialog' +import { NEXT_CACHE_TAGS, QUERY_CACHE_KEYS } from '@/constants/cache' +import { usePatientOptions } from '@/hooks/use-patient-otions' +import { api } from '@/lib/api' +import { queryClient } from '@/lib/tanstack-query' +import { + PATIENT_CONDITION_ENUM, + PATIENT_CONDITION_OPTIONS, +} from '@/types/patients' +import { + REFERRAL_CATEGORY_ENUM, + REFERRAL_CATEGORY_OPTIONS, +} from '@/types/referrals' + +interface ReferralModalProps { + onClose(): void + id?: string +} + +export function ReferralPatientModal({ onClose, id }: ReferralModalProps) { + const { patientOptions } = usePatientOptions() + const referralFormSchema = z.object({ + patient_id: z.string().uuid('Paciente é obrigatório'), + date: z.string().datetime('A data é obrigatória'), + category: z.enum(REFERRAL_CATEGORY_ENUM, { + message: 'Categoria é obrigatório', + }), + condition: z.enum(PATIENT_CONDITION_ENUM, { + message: 'O quadro é obrigatório', + }), + annotation: z + .string() + .max(500) + .nullable() + .transform((value) => (!value ? null : value)), + referred_to: z + .string() + .nullable() + .transform((value) => (!value ? null : value)), + }) + type ReferralsFormSchema = z.infer + + const formMethods = useForm({ + resolver: zodResolver(referralFormSchema), + defaultValues: { + patient_id: id ?? '', + date: '', + category: '', + referred_to: '', + condition: '', + annotation: '', + } as unknown as ReferralsFormSchema, + mode: 'onBlur', + }) + + async function submitForm(data: ReferralsFormSchema) { + const response = await api('/referrals', { + method: 'POST', + body: JSON.stringify(data), + }) + if (!response.success) { + toast.error(response.message) + return + } + queryClient.invalidateQueries({ + queryKey: [QUERY_CACHE_KEYS.referrals.list], + }) + revalidateCache(NEXT_CACHE_TAGS.patient(data.patient_id)) + toast.success(response.message) + onClose() + } + + return ( + + }> + Encaminhar paciente + + + + + + + + + + + + + + + + + + + Cancelar + + + + ) +}