import React, { useState, useContext } from "react"; import { FileUploader } from "react-drag-drop-files"; import { AppContext } from "../../Hooks/useContext/appContext"; import Modal from "react-bootstrap/Modal"; import Button from "../../pageElements/Button"; import Form from "react-bootstrap/Form"; import Row from "react-bootstrap/Row"; import Col from "react-bootstrap/Col"; import Radiogroup from "../../pageElements/Radiogroup"; import { useNavigate } from "react-router-dom"; import { collection, setDoc, doc } from "firebase/firestore"; import { db } from "../../firebase"; import { v4 as uuidv4 } from "uuid"; import TextInput from "../../pageElements/TextInput"; import { ExclamationTriangle } from "react-bootstrap-icons"; import Radio from "../../pageElements/Radio"; import "../../styles/modals.scss"; const MAX_LENGTH_TITLE = 27; const MAX_LENGTH_DESC = 35; const MAX_LENGTH_DATE = 20; const UploadModal = (props) => { const { appState } = useContext(AppContext); const { group } = appState; const appUserId = group.appUserId; const { setShowModal, caseData, handleSuccess, setIsLoading } = props; const { caseId, caseNumber, jurisdiction, caption, captionTwo } = caseData; const [docTitle, setDocTitle] = useState(""); const [docDescription, setDocDescription] = useState(""); const [dateServed, setDateServed] = useState(""); const [fileToUpload, setFileToUpload] = useState(); const [fileToParse, setFileToParse] = useState(""); const [fileChanged, setFileChanged] = useState(); const [radioValue, setRadioValue] = useState(null); const [radioValueError, setRadioValueError] = useState(""); const [showFileDetails, setShowFileDetails] = useState(""); const [titleError, setShowTitleError] = useState(""); const [docError, setShowDocError] = useState(""); const fileTypes = ["PDF"]; const fileName = fileToParse ? fileToParse.name : undefined; const apiUrl = process.env.NODE_ENV === "development" ? process.env.REACT_APP_API_DEV : process.env.REACT_APP_API_PROD; const radioOptions = [ { name: "Discovery request", label: "Discovery request", value: "discovery-request", }, { name: "Complaint", label: "Complaint", value: "complaint" }, ]; const handleSetRadioValue = (e) => { setRadioValue(e.value); }; async function saveToDb(uuidName) { const createdAt = new Date(); const data = { ownerId: appUserId, createdAt: createdAt, documentName: `${uuidName}.pdf`, docDescription: docDescription, dateServed: dateServed, docTitle: docTitle, parentCaseId: caseId, parentCaseName: `${caption} v. ${captionTwo}`, parentCaseNumber: caseNumber, parentCaseJurisdiction: jurisdiction, responseGenerations: 0, }; try { const documentsRef = collection(db, "documents"); await setDoc(doc(documentsRef, `${uuidName}`), data); } catch (err) { console.log("Error saving case to db:", err); } } async function handleFileChange(file) { setFileToParse(file); setFileChanged(true); } async function uploadFile(file) { try { if (radioValue === "discovery-request") { const response = await fetch(`${apiUrl}/gen-disc-request`, { method: "POST", body: file, }); const res = response; return res; } else { const response = await fetch(`${apiUrl}/parseNewDoc`, { method: "POST", body: file, }); const res = response; return res; } } catch (error) { console.error("Error:", error); } } async function processFile() { const uuidName = uuidv4(); const newName = `${uuidName}.pdf`; const title = docTitle; const description = docDescription || ""; const formData = new FormData(); if (fileToParse && fileChanged) { formData.append("file", fileToParse[0], newName); } if (fileName) { formData.append("fileName", fileName); } formData.append("title", title); formData.append("description", description); setFileToUpload(formData); let response = {}; const res = await uploadFile(formData); response["uuidName"] = uuidName; response["res"] = res; return response; } async function handleUpload() { if (!docTitle && !fileToParse && !radioValue) { setShowDocError("Please select a document to upload."); setShowTitleError("Please enter a document title"); setRadioValueError("Please select document type"); return; } else if (!docTitle) { setShowTitleError("Please enter a document title"); return; } else if (!fileToParse) { setShowDocError("Please select a document to upload."); return; } setIsLoading(true); try { const response = await processFile(); if (response.res?.status !== 200) { saveToDb(response.uuidName); return; } else { saveToDb(response.uuidName); } } catch (err) { console.log("err", err); } handleSuccess(); } const handleUploadCancel = () => { setShowFileDetails(false); setFileToParse(null); setFileChanged(false); setShowModal(false); }; const handleTitleInput = (e) => { if (e.target.value.length <= MAX_LENGTH_TITLE) { setDocTitle(e.target.value); } }; const handleDescInput = (e) => { if (e.target.value.length <= MAX_LENGTH_DESC) { setDocDescription(e.target.value); } }; const handleDateInput = (e) => { if (e.target.value.length <= MAX_LENGTH_DATE) { setDateServed(e.target.value); } }; return ( Document Upload

Select a .pdf file for upload, then 1. enter title 2. choose document type. (the other fields are optional.)

Selecting "complaint" will automatically begin creation of discovery requests.

Incorrect selection will result in errors but 1 document credit will still be debited from your account

The document will be associated with{" "} {caption} v. {captionTwo} {caseNumber ? `, ${caseNumber}` : ""}.

If that is not the correct case: click cancel to go back and select the intended case from your active cases.

handleSetRadioValue(e, "documentType")} />
Select Document
{fileToParse ? fileToParse[0]?.name : "Click to select a file or drag and drop here"}
{docError ? (
{docError}
) : ( <> )}