import { useContext, useEffect, useState, useRef } from "react"; import { useNavigate } from "react-router-dom"; import { AppContext } from "../../Hooks/useContext/appContext"; import SelectDropdown from "../../pageElements/SelectDropdown"; import { DocCard } from "../../pageElements/Cards"; import { db } from "../../firebase"; import Toggle from "../../pageElements/Toggle.js"; import MobileContent from "../MobileContent"; import { collection, deleteDoc, doc, documentId, onSnapshot, query, where, } from "firebase/firestore"; import ListPagination from "../../pageElements/ListPagination"; import ConfirmModal from "../Modals/ConfirmModal"; import "../../styles/doclist-page.scss"; const DocumentListPage = ({ perPage }) => { const size = window.innerWidth < 440; const [isMobile, setIsMobile] = useState(size); const count = useRef(null); const navigate = useNavigate(); const [showDeleteModal, setShowDeleteModal] = useState(false); const [selectedDocumentId, setSelectedDocumentId] = useState(null); const [allDocs, setAllDocs] = useState(null); const { appState } = useContext(AppContext); const [totalPages, setTotalPages] = useState(1); const { group } = appState; const [page, setPage] = useState(1); const [order, setOrder] = useState("parentCaseName"); const appUserId = group ? group.appUserId : null; const [isBusy, setIsBusy] = useState(false); const [selectedDocumentType, setSelectedDocumentType] = useState(); const [showModal, setShowModal] = useState(false); const apiUrl = process.env.NODE_ENV === "development" ? process.env.REACT_APP_API_DEV : process.env.REACT_APP_API_PROD; useEffect(() => { if (count.current == null) { setIsMobile(window.innerWidth < 440); } if (count.current < 3) { setIsMobile(window.innerWidth < 440); } return () => { const temp = (count.current = count.current + 1); count.current = temp; }; }, []); function getDocuments() { if (!appUserId) { setAllDocs([]); return; } const q = query( collection(db, "documents"), where("ownerId", "==", appUserId) ); const unsub = onSnapshot(q, (snapshot) => { const data = snapshot.docs.map((doc) => ({ ...doc.data(), documentId: doc.id, })); if (order === "parentCaseName") { data.sort((a, b) => { return a.parentCaseName.toLowerCase() > b.parentCaseName.toLowerCase() ? 1 : -1; }); } else if (order === "parentCaseName2") { data.sort((a, b) => { return a.parentCaseName.toLowerCase() < b.parentCaseName.toLowerCase() ? 1 : -1; }); } const pages = Math.ceil(data.length / perPage); if (page > pages) { setPage(pages); } setTotalPages(pages); setAllDocs(data); }); return unsub; } useEffect(getDocuments, [appUserId, order]); async function deleteFromDb(documentId) { if (isBusy) { return; } setIsBusy(true); try { const docRef = doc(db, "documents", `${documentId}`); await deleteDoc(docRef); setShowDeleteModal(false); } catch (err) { console.log("Error deleting document from db:", err); } setIsBusy(false); } const handleNovos = (e) => { navigate("/how-to"); }; async function deleteFromStorage() { const selectedDoc = allDocs.filter((doc) => { return doc.documentId == selectedDocumentId; }); const respGens = selectedDoc[0].responseGenerations; try { const response = await fetch( `${apiUrl}/deleteDoc/${selectedDocumentId}/${selectedDocumentType}/${respGens}`, { method: "POST", } ); const res = response; return res; } catch (error) { console.error("Error deleting document from storage:", error); } } const dropDownOptions = [ { label: "Associated case name a - z", value: "parentCaseName" }, { label: "Associated case name z - a", value: "parentCaseName2" }, ]; function handleNavigate( documentId, caseId, documentType, clientPosition, responseGenerations ) { const docId = String(documentId); const docType = String(documentType); if (documentType === "interrogatories-out") { //may include other in future ie req for admission navigate(`/reqedit/${docId}/interrogatories-out/${caseId}`); } else { console.log("----------------------> handeNave else"); navigate( `/docedit/${docId}/${caseId}/${docType}/${clientPosition}/${responseGenerations}` ); } } function handleCancelDelete() { if (!isBusy) { setShowDeleteModal(false); } } function handleDeleteDocument() { deleteFromDb(selectedDocumentId); deleteFromStorage(selectedDocumentId, selectedDocumentType); } function confirmDelete(documentId, docType) { setSelectedDocumentId(documentId); setSelectedDocumentType(docType); setShowDeleteModal(true); } if (!group) { return null; } const DesktopContent = () => { return ( <>

Documents

{group.firm}
{allDocs === null ?
Loading...
: null} {allDocs !== null ? (
{allDocs.length > 0 ? (
) : ( <> )}
{allDocs.length > 0 ? ( allDocs .slice((page - 1) * perPage, page * perPage) .map((doc, i) => ( )) ) : (

{" "} You have not uploaded any documents yet.

To upload a document:

  1. Navigate to the cases view (above link in navigation bar).{" "}
  2. {" "} Click "View Case" to select the case with which your document will be associated. This will open the Case Detail view.{" "}
  3. Note: If you have not yet created any cases, you will need to create one first - click "Create Case" and follow the prompts.
  4. In the case detail view, click "Upload Case Document" and select a document from your computer for upload.
  5. Your document will be parsed into a processable form. You will then be able to generate a response to the document.
)}
{totalPages > 1 ? (
) : null}
) : null} {showDeleteModal && selectedDocumentId !== null ? ( ) : null} ); }; return isMobile ? : ; }; export default DocumentListPage; /* const [verbValue, setVerbValue] = useState(1); const handleToggle = (val) => setVerbValue(val);
{" "}

Novos

Min

Max

*/