import React, { useState } from "react"; import { useCollections } from "../../hooks/useCollections"; import { ADDPAYMENT_COLLECTION, ADMIN_COLLECTION, CREATE_COLLECTION, EDIT_COLLECTION, ITEMS_PER_PAGE, VIEW_COLLECTION, } from "../../utils/constants"; import { formatFigure, localToUtc, useDebounce } from "../../utils/appUtils"; import { formatUTCToLocalTime } from "../../utils/dateUtils"; import Pagination from "../common/Pagination"; import { useCollectionContext } from "../../pages/collections/CollectionPage"; import { CollectionTableSkeleton } from "./CollectionSkeleton"; import { useSelectedProject } from "../../slices/apiDataManager"; import { useHasUserPermission } from "../../hooks/useHasUserPermission"; const CollectionList = ({ fromDate, toDate, isPending, searchString }) => { const [currentPage, setCurrentPage] = useState(1); const isAdmin = useHasUserPermission(ADMIN_COLLECTION); const canAddPayment = useHasUserPermission(ADDPAYMENT_COLLECTION); const canViewCollection = useHasUserPermission(VIEW_COLLECTION); const canEditCollection = useHasUserPermission(EDIT_COLLECTION); const canCreate = useHasUserPermission(CREATE_COLLECTION); const selectedProject = useSelectedProject(); const searchDebounce = useDebounce(searchString, 500); const { data, isLoading, isError, error } = useCollections( selectedProject, searchDebounce, localToUtc(fromDate), localToUtc(toDate), ITEMS_PER_PAGE, currentPage, true, isPending ); const { setProcessedPayment, setAddPayment, setViewCollection } = useCollectionContext(); const paginate = (page) => { if (page >= 1 && page <= (data?.totalPages ?? 1)) { setCurrentPage(page); } }; const collectionColumns = [ { key: "invoiceDate", label: "Invoice Date", getValue: (col) => ( {formatUTCToLocalTime(col.invoiceDate)} ), align: "text-start", }, { key: "invoiceId", label: "Invoice No", getValue: (col) => ( {col?.invoiceNumber ?? "-"} ), align: "text-start", }, { key: "project", label: "Project", getValue: (col) => ( {col?.project?.name ?? "-"} ), align: "text-start", }, { key: "submittedDate", label: "Submission Date", getValue: (col) => ( {formatUTCToLocalTime(col.createdAt)} ), align: "text-center", }, { key: "expectedSubmittedDate", label: "Expected Payment Date", getValue: (col) => ( {formatUTCToLocalTime(col.exceptedPaymentDate) ?? "-"} ), align: "text-center", }, { key: "amount", label: "Total Amount", getValue: (col) => ( {formatFigure(col?.basicAmount + col?.taxAmount, { type: "currency", currency: "INR", }) ?? 0} ), align: "text-end", }, { key: "balance", label: "Balance", getValue: (col) => ( {formatFigure(col?.balanceAmount, { type: "currency", currency: "INR", }) ?? 0} ), align: "text-end", }, ]; if (isLoading) return ; if (isError) return

{error.message}

; return (
{collectionColumns.map((col) => ( ))} {(isAdmin || canAddPayment || canViewCollection || canEditCollection || canCreate) && } {Array.isArray(data?.data) && data.data.length > 0 ? ( data.data.map((row, i) => ( {collectionColumns.map((col) => ( ))} {(isAdmin || canAddPayment || canViewCollection || canEditCollection || canCreate) && ( )} )) ) : ( )}
{col.label} Action
{col.getValue(row)}
No Collections Found
{data?.data?.length > 0 && (
)}
); }; export default CollectionList;