"use client"; import React, { useEffect, useState } from "react"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { X } from "lucide-react"; import CustomButton from "@/components/customs/custom-button"; import CustomLoading from "@/components/customs/custom-loading"; import FormCustom from "@/components/utils/FormCustom"; import InputForm from "@/components/utils/FormCustom/components/InputForm"; import TextArea from "@/components/utils/FormCustom/components/TextArea"; import { ContextFormCustom } from "@/components/utils/FormCustom/contexts"; import { QUERY_KEY, TYPE_GENDER } from "@/constant/config/enum"; import { httpRequest } from "@/services"; import patientServices, { PatientDetail } from "@/services/patientServices"; import { toastWarn } from "@/common/funcs/toast"; import { timeSubmit } from "@/common/funcs/optionConvert"; interface PopupUpdatePatientProps { onClose: () => void; id: string; } interface IUpdatePatient { fullName: string; gender: string; phone: string; email: string; address: string; emergencyContactName: string; emergencyContactPhone: string; allergyNotes: string; chronicDiseaseNotes: string; insuranceNumber: string; notes: string; } const defaultForm: IUpdatePatient = { fullName: "", gender: "", phone: "", email: "", address: "", emergencyContactName: "", emergencyContactPhone: "", allergyNotes: "", chronicDiseaseNotes: "", insuranceNumber: "", notes: "", }; const PopupUpdatePatient = ({ onClose, id }: PopupUpdatePatientProps) => { const queryClient = useQueryClient(); const [form, setForm] = useState(defaultForm); const patientDetailQuery = useQuery({ queryKey: [QUERY_KEY.chi_tiet_benh_nhan, id], enabled: !!id, queryFn: async () => { const res = await httpRequest({ showMessageFailed: true, http: patientServices.detailPatient(id), }); return ( res || { id: "", patientCode: "", identificationNumber: "", fullName: "", dateOfBirth: "", gender: "", phone: "", email: "", address: "", emergencyContactName: null, emergencyContactPhone: null, allergyNotes: null, chronicDiseaseNotes: null, insuranceNumber: null, notes: null, isActive: false, } ); }, }); useEffect(() => { const patient = patientDetailQuery.data; if (!patient) return; setForm((prev) => { /** * tránh render loop */ if ( prev.fullName === patient.fullName && prev.phone === patient.phone && prev.email === patient.email ) { return prev; } return { fullName: patient.fullName || "", gender: patient.gender || TYPE_GENDER.MALE, phone: patient.phone || "", email: patient.email || "", address: patient.address || "", emergencyContactName: patient.emergencyContactName || "", emergencyContactPhone: patient.emergencyContactPhone || "", allergyNotes: patient.allergyNotes || "", chronicDiseaseNotes: patient.chronicDiseaseNotes || "", insuranceNumber: patient.insuranceNumber || "", notes: patient.notes || "", }; }); }, [patientDetailQuery.data]); const updatePatientMutation = useMutation({ mutationFn: async () => { return httpRequest({ showMessageFailed: true, showMessageSuccess: true, msgSuccess: "Cập nhật bệnh nhân thành công!", http: patientServices.putPatient(id, { fullName: form.fullName, gender: form.gender, phone: form.phone, email: form.email, address: form.address, emergencyContactName: form.emergencyContactName, emergencyContactPhone: form.emergencyContactPhone, allergyNotes: form.allergyNotes, chronicDiseaseNotes: form.chronicDiseaseNotes, insuranceNumber: form.insuranceNumber, notes: form.notes, }), }); }, onSuccess(data) { if (!data) return; queryClient.invalidateQueries({ queryKey: [QUERY_KEY.table_list_patient], }); queryClient.invalidateQueries({ queryKey: [QUERY_KEY.chi_tiet_benh_nhan, id], }); onClose(); }, }); const handleSubmit = () => { if (!form.gender) { return toastWarn({ msg: "Vui lòng chọn giới tính!" }); } updatePatientMutation.mutate(); }; if (patientDetailQuery.isLoading) { return (
); } return (
{/* CLOSE */} {/* HEADER */}

Cập nhật thông tin bệnh nhân

{/* BODY */}
Tên bệnh nhân * } placeholder="Tên bệnh nhân" name="fullName" type="text" value={form.fullName} isRequired /> {/* GENDER */}
{/* NAM */}
setForm((prev) => ({ ...prev, gender: TYPE_GENDER.MALE, })) } />
{/* NỮ */}
setForm((prev) => ({ ...prev, gender: TYPE_GENDER.FEMALE, })) } />
Số điện thoại * } placeholder="Số điện thoại" name="phone" type="phone" isPhone value={form.phone} isRequired /> Email} placeholder="Email" name="email" type="email" isEmail value={form.email} />