diff --git a/src/components/Documents/DocumentVersionList.jsx b/src/components/Documents/DocumentVersionList.jsx index aea49267..5c01b9c7 100644 --- a/src/components/Documents/DocumentVersionList.jsx +++ b/src/components/Documents/DocumentVersionList.jsx @@ -13,6 +13,9 @@ const DocumentVersionList = ({ isPending, setOpenDocument, VerifyDocument, + RejectDocument, + showVersions, + latestDoc, }) => { const canVerifyDocument = useHasUserPermission(VERIFY_DOCUMENT); const canDownloadDocument = useHasUserPermission(DOWNLOAD_DOCUMENT); @@ -23,146 +26,267 @@ const DocumentVersionList = ({ } }; - const sortedVersions = versionList?.data - ? [...versionList.data].sort((a, b) => b.version - a.version) - : []; - if (versionLoding) { return ; } + const sortedVersions = versionList?.data + ? [...versionList.data].sort((a, b) => b.version - a.version) + : []; + + const currentDoc = sortedVersions.length ? sortedVersions[0] : latestDoc; + + if (!showVersions) { + if (!currentDoc) { + return

No documents available.

; + } + return ( + <> +
+
+
+ {/* Left Side: Document Details */} +
+ +
+
+ + {currentDoc.name} + +
+
+ {getDocuementsStatus(currentDoc.isVerified)} + + File Size: {currentDoc.fileSize} Kb + +
+
+ + Uploaded by + + + + + {`${currentDoc.uploadedBy?.firstName ?? ""} ${currentDoc.uploadedBy?.lastName ?? ""}`.trim() || "N/A"} + + +
+ + {formatUTCToLocalTime(currentDoc?.uploadedAt)} + +
+
+ +
+ + latest + + +
+ {/* Right Side: Status and Info */} +
+ {/* + latest + */} +
+ {currentDoc?.updatedBy && ( + <> + Updated by + + + + {`${currentDoc.updatedBy?.firstName ?? ""} ${currentDoc.updatedBy?.lastName ?? ""}`.trim() || "N/A"} + + + + )} +
+ {/* Conditionally render updated date */} + {currentDoc?.updatedAt && ( + + {formatUTCToLocalTime(currentDoc?.updatedAt)} + + )} +
+
+
+
+ {/* Buttons Div - only for the latest pending document when showVersions is false */} + {currentDoc.isVerified === null && canVerifyDocument && ( +
+ {isPending ? ( + Please Wait... + ) : ( + <> + + + + )} +
+ )} + + ); + } + + // If showVersions is true, display all versions if (!sortedVersions.length) { return

No documents available.

; } return ( - <> -
-
Documents
-
- - -
-
-
-
-
-
- {sortedVersions.map((document, index) => ( -
-
- {/* Left Side: Document Details */} -
- -
-
- - {document.name} - -
-
- {getDocuementsStatus(document.isVerified)} - - File Size: {document.fileSize} Kb - -
-
- - Uploaded by +
+
+
+
+ {sortedVersions.map((document, index) => ( +
+
+ {/* Left Side: Document Details */} +
+ +
+
+ + {document.name} + +
+
+ {getDocuementsStatus(document.isVerified)} + + File Size: {document.fileSize} Kb + +
+
+ + Uploaded by + + + + + {`${document.uploadedBy?.firstName ?? ""} ${document.uploadedBy?.lastName ?? ""}`.trim() || "N/A"} + + +
+ + {formatUTCToLocalTime(document?.uploadedAt)} + + {document?.verifiedAt && ( +
+ + {document.isVerified ? "Approved by" : "Rejected by"} - - {`${document.uploadedBy?.firstName ?? ""} ${document.uploadedBy?.lastName ?? ""}`.trim() || "N/A"} + + {`${document.verifiedBy?.firstName ?? ""} ${document.verifiedBy?.lastName ?? ""}`.trim() || "N/A"} - - {formatUTCToLocalTime(document?.uploadedAt)} -
- {document?.verifiedAt && ( -
- - {document.isVerified ? "Approved by" : "Rejected by"} - - - - - {`${document.verifiedBy?.firstName ?? ""} ${document.verifiedBy?.lastName ?? ""}`.trim() || "N/A"} - - - - {formatUTCToLocalTime(document?.verifiedAt)} - -
- )} -
-
- - {/* Right Side: Status and Actions */} -
- {document.isLatest && ( - - latest - )} - -
- {document?.updatedBy && ( - <> - Updated by - - - - {`${document.updatedBy?.firstName ?? ""} ${document.updatedBy?.lastName ?? ""}`.trim() || "N/A"} - - - - {formatUTCToLocalTime(document?.updatedAt)} - - - )} -
- - version {document.version} - + {/* Conditionally render verified date */} + {document?.verifiedAt && ( + + {formatUTCToLocalTime(document?.verifiedAt)} + + )}
+
+ + version {document.version} + + {document.isLatest && ( + + latest + + )} +
+ {/* Right Side: Status and Actions */} +
+ {/* + version {document.version} + */} + {document.isLatest && ( + + latest + + )} +
+ {document?.updatedBy && ( + <> + Updated by + + + + {`${document.updatedBy?.firstName ?? ""} ${document.updatedBy?.lastName ?? ""}`.trim() || "N/A"} + + + + )} +
+ {/* Conditionally render updated date */} + {document?.updatedAt && ( + + {formatUTCToLocalTime(document?.updatedAt)} + + )} + +
- ))} -
+
+ ))}
- +
); }; diff --git a/src/components/Documents/ViewDocument.jsx b/src/components/Documents/ViewDocument.jsx index 5eb46f37..8270bb08 100644 --- a/src/components/Documents/ViewDocument.jsx +++ b/src/components/Documents/ViewDocument.jsx @@ -1,3 +1,4 @@ +// ViewDocument.jsx import React, { useState } from "react"; import { useDocumentDetails, @@ -12,9 +13,6 @@ import { ITEMS_PER_PAGE, VERIFY_DOCUMENT, } from "../../utils/constants"; -import Pagination from "../common/Pagination"; -import VersionListSkeleton from "./VersionListSkeleton"; -// import DocumentDetailsSkeleton from "./DocumentDetailsSkeleton"; import DocumentDetailsSkeleton from "./DocumentDetailsSkeleton "; import { useHasUserPermission } from "../../hooks/useHasUserPermission"; import DocumentVersionList from "./DocumentVersionList"; @@ -22,15 +20,20 @@ import DocumentVersionList from "./DocumentVersionList"; const ViewDocument = () => { const { viewDoc, setOpenDocument } = useDocumentContext(); const [currentPage, setCurrentPage] = useState(1); + const [showVersions, setShowVersions] = useState(false); const canVerifyDocument = useHasUserPermission(VERIFY_DOCUMENT); + + // Document Details const { data, isLoading, isError, error } = useDocumentDetails( viewDoc?.document ); + + // Document Versions (fetch only if toggle is ON) const { data: versionList, - isLoading: versionLoding, + isLoading: versionLoading, } = useDocumentVersionList( - data?.parentAttachmentId, + showVersions ? data?.parentAttachmentId : null, ITEMS_PER_PAGE - 10, currentPage ); @@ -41,6 +44,7 @@ const ViewDocument = () => { } }; + // Verify / Reject const { mutate: VerifyDoc, isPending } = useVerifyDocument(); const VerifyDocument = () => { VerifyDoc({ documentId: viewDoc?.document, isVerify: true }); @@ -57,11 +61,12 @@ const ViewDocument = () => {

{error?.response?.status}

); - console.log("kartik", data) + return (

Document Details

+ {/* Document Info Rows */}
@@ -103,14 +108,7 @@ const ViewDocument = () => { {formatUTCToLocalTime(data.uploadedAt)}
-
- - Updated At: - - - {formatUTCToLocalTime(data.updatedAt) || "-"} - -
+
@@ -144,39 +142,32 @@ const ViewDocument = () => {
- {/* Verify / Reject */} - {data.isVerified === null && canVerifyDocument && ( -
- {isPending ? ( - Please Wait... - ) : ( - <> - - - - )} + {/* Toggle for Versions */} +
+

Documents:

+
+ + setShowVersions(e.target.checked)} + />
- )} - +
);