import React, { useState, useContext, useEffect, useRef } from "react"; import { CaseCard } from "../../pageElements/Cards.js"; import { Link, useNavigate, createSearchParams } from "react-router-dom"; import MobileContent from "../MobileContent"; import { ArrowRight, Activity, FileEarmarkText, Pencil, Clock, } from "react-bootstrap-icons"; import { db } from "../../firebase.js"; import { collection, doc, setDoc, onSnapshot, query, where, } from "firebase/firestore"; import { Typeahead } from "react-bootstrap-typeahead"; import { AppContext } from "../../Hooks/useContext/appContext.js"; import UploadModal from "../Modals/UploadModal.js"; import Button from "react-bootstrap/Button"; import { getAuth } from "firebase/auth"; const Dashboard = () => { const size = window.innerWidth < 440; const [isMobile, setIsMobile] = useState(size); const auth = getAuth(); const count = useRef(null); const navigate = useNavigate(); const [allCases, setAllCases] = useState(null); const [selectedCase, setSelectedCase] = useState(null); const [showModal, setShowModal] = useState(); const [docCount, setDocCount] = useState(); const [isPromoFirstLogin, setIsPromoFirstLogin] = useState(true); const [responseCount, setResponseCount] = useState(); const { appState } = useContext(AppContext); const { group } = appState; const appUserId = group ? group.appUserId : null; const isDashboard = true; console.log("group on dashbaord page", group); console.log("auth in dashoabrd page", auth); 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 getCases() { if (!appUserId) { setAllCases([]); return; } const q = query(collection(db, "cases"), where("ownerId", "==", appUserId)); const unsub = onSnapshot(q, (snapshot) => { const data = snapshot.docs.map((doc) => doc.data()); data.sort((a, b) => { return a.filedDate < b.filedDate ? 1 : -1; }); setAllCases(data); }); return unsub; } function getStats() { if (!appUserId) { return; } const q = query( collection(db, "documents"), where("ownerId", "==", appUserId) ); const unsub = onSnapshot(q, (snapshot) => { const data = snapshot.docs.map((doc) => doc.data()); if (data) { setDocCount(data?.length); const respCount = data?.reduce(function (acc, obj) { return acc + obj.responseGenerations; }, 0); setResponseCount(respCount); } }); return unsub; } useEffect(() => { if (!appUserId) { return; } const unsubCases = getCases(); const unsubStats = getStats(); return () => { typeof unsubCases !== "undefined" && unsubCases(); typeof unsubStats !== "undefined" && unsubStats(); }; }, [appUserId]); const handleViewClick = (caseId) => { navigate(`/casedetails/${caseId}`); }; if (!group) { return null; } async function handleClick(e) { e.preventDefault(); console.log("handle lcick"); const updateRef = collection(db, "users"); await setDoc(doc(updateRef, appUserId), { isPromoFirstLogin: false }); navigate({ pathname: "/passwordreset/", search: `?${createSearchParams({ mode: "enterEmail", })}`, }); } const DesktopContent = () => { return allCases !== null ? (
{ let item = null; for (const currentItem of selected) { item = { ...currentItem }; } setSelectedCase(item); }} />
{allCases.slice(0, 3).map((c, i) => ( ))} {allCases?.length > 3 ? (
View All{" "}
) : null}
) : null; }; return ( <>

{!!(group.firstName && group.lastName) ? ( {group.firstName} {group.lastName} ) : ( <> )}

{group.firm}
{isPromoFirstLogin ? ( <>

Welcome to Novodraft, attorney {group.lastName}. We are excited to introduce you to our application. Review the step-by-step user guide by clicking "How-to" in the navigation bar.

{" "}

Your login is{" "} {`username: ${group.email} - with a temporary password: ${group.tpe}.`}

You should change your password now by{" "} clicking here

) : null}
{allCases?.length >= 1 ? (

{allCases?.length === 1 ? `${allCases?.length} active case` : `${allCases?.length} active cases`}

) : (

{allCases?.length} active cases - Click on the "Cases" tab, above, and create a case to get started.

)}
{allCases?.length >= 1 ? ( <>

{docCount === 1 ? `${docCount} document uploaded` : `${docCount} documents uploaded`}

) : ( <> )}
{responseCount > 0 ? ( <>

{responseCount} {responseCount < 2 ? " response Novodrafted" : " responses Novodrafted"}

) : ( <> )} {responseCount > 0 ? ( <>

{Math.round(responseCount * 3.1799 * 10) / 10} hours saved using Novodraft

) : ( <> )}
{isMobile ? ( ) : ( )} {showModal && selectedCase !== null ? ( ) : null} ); }; export default Dashboard;