"use client"; import CustomButton from "@/components/customs/custom-button"; import CustomDialog from "@/components/customs/custom-dialog"; import CustomPopup from "@/components/customs/custom-popup"; import DataWrapper from "@/components/customs/DataWrapper"; import Search from "@/components/customs/custom-search"; import Table from "@/components/customs/custom-table"; import { QUERY_KEY } from "@/constant/config/enum"; import { httpRequest } from "@/services"; import patientServices, { PatientItem, PatientResponse, } from "@/services/patientServices"; import { useQuery } from "@tanstack/react-query"; import { Pencil, Trash2, Users } from "lucide-react"; import Link from "next/link"; import { usePathname, useRouter, useSearchParams } from "next/navigation"; import React, { useMemo, useState } from "react"; import PopupCreatePatient from "../PopupCreatePatient"; import PopupUpdatePatient from "../PopupUpdatePatient"; const MainPagePatient = () => { const router = useRouter(); const pathname = usePathname(); const searchParams = useSearchParams(); const [openDeleteDialog, setOpenDeleteDialog] = useState(false); const [deleteItemId, setDeleteItemId] = useState(""); const _page = searchParams.get("_page"); const _pageSize = searchParams.get("_pageSize"); const _keyword = searchParams.get("_keyword"); /** * PARAMS */ const createParam = searchParams.get("_create"); const updateParam = searchParams.get("_update"); /** * OPEN POPUP */ const openCreatePopup = createParam !== null; const openUpdatePopup = !!updateParam; /** * QUERY LIST PATIENT */ const patientQuery = useQuery({ queryKey: [QUERY_KEY.table_list_patient, _page, _pageSize, _keyword], queryFn: async () => { const res = await httpRequest({ showMessageFailed: true, http: patientServices.getPatient({ Page: Number(_page || 1), PageSize: Number(_pageSize || 10), Search: _keyword || "", SortBy: "id", Desc: true, }), }); return ( res || { items: [], page: 1, pageSize: 10, total: 0, totalPages: 0, } ); }, }); /** * UPDATE QUERY PARAMS */ const updateQuery = (key: string, value?: string | number) => { const params = new URLSearchParams(searchParams.toString()); if (!value || value === "") { params.delete(key); } else { params.set(key, String(value)); } const queryString = params.toString(); router.push(queryString ? `${pathname}?${queryString}` : pathname); }; /** * OPEN CREATE */ const handleOpenCreate = () => { const params = new URLSearchParams(searchParams.toString()); params.set("_create", ""); router.push(`${pathname}?${params.toString()}`); }; /** * OPEN UPDATE */ const handleOpenUpdate = (id: string) => { const params = new URLSearchParams(searchParams.toString()); params.delete("_create"); params.set("_update", id); router.push(`${pathname}?${params.toString()}`); }; /** * CLOSE ALL POPUP */ const handleClosePopup = () => { const params = new URLSearchParams(searchParams.toString()); params.delete("_create"); params.delete("_update"); const queryString = params.toString(); router.push(queryString ? `${pathname}?${queryString}` : pathname); setOpenDeleteDialog(false); setDeleteItemId(""); }; /** * DELETE */ const handleOpenDelete = (id: string) => { setDeleteItemId(id); setOpenDeleteDialog(true); }; /** * DATA */ const patientData = useMemo(() => { return patientQuery.data?.items || []; }, [patientQuery.data]); return (
{/* HEADER */}

Bệnh nhân

} onClick={handleOpenCreate} > Tạo mới
{/* SEARCH */}
updateQuery("_keyword", value)} placeholder="Tìm kiếm theo tên người dùng, ID" />
{/* TABLE */} item.id} data={patientData} column={[ { fixedLeft: true, title: "TÊN BỆNH NHÂN", render: (item: PatientItem) => ( {item.fullName} ), }, { title: "MÃ ĐỊNH DANH", render: (item: PatientItem) => ( {item.identificationNumber} ), }, { title: "SỐ ĐIỆN THOẠI", render: (item: PatientItem) => {item.phone}, }, { title: "EMAIL", render: (item: PatientItem) => {item.email}, }, { title: "GIỚI TÍNH", render: (item: PatientItem) => {item.gender}, }, { fixedRight: true, title: "ACTION", render: (item: PatientItem) => (
{/* UPDATE */} } onClick={() => handleOpenUpdate(item.id)} /> {/* DELETE */} } onClick={() => handleOpenDelete(item.id)} />
), }, ]} /> {/* DELETE DIALOG */} {}} titleCancel="Hủy" titleSubmit="Xác nhận" /> {/* CREATE POPUP */} {/* UPDATE POPUP */} ); }; export default MainPagePatient;