import { useEffect, useState, useRef, useCallback } from 'react'; import { Link } from 'react-router-dom'; import { useAuth } from '../context/AuthContext'; import { useToast } from '../context/ToastContext'; export default function UploadBusiness() { const { authFetch } = useAuth(); const { showToast } = useToast(); const [taxYear, setTaxYear] = useState(2025); const [docs, setDocs] = useState([]); const [templates, setTemplates] = useState([]); const [loading, setLoading] = useState(false); const fileInputRefs = useRef({}); const extraFileRef = useRef(null); const fetchTemplatesAndDocs = useCallback(async () => { setLoading(true); try { const tplRes = await authFetch(`/api/upload/templates?category=business&tax_year=${taxYear}`); if (tplRes.ok) { setTemplates(await tplRes.json()); } const docRes = await authFetch(`/api/upload/business-documents?tax_year=${taxYear}`); if (docRes.ok) { setDocs(await docRes.json()); } } catch (err) { console.error(err); } finally { setLoading(false); } }, [authFetch, taxYear]); useEffect(() => { fetchTemplatesAndDocs(); }, [taxYear, fetchTemplatesAndDocs]); const uploadFile = async (file, docType, skipFetch = false) => { if (file.size > 10 * 1024 * 1024) { showToast("File size exceeds 10MB limit", "error"); return; } const fd = new FormData(); fd.append("file", file); fd.append("tax_year", taxYear); fd.append("doc_type", docType); try { const res = await authFetch("/api/upload/business-documents", { method: "POST", body: fd }); if (res.ok) { showToast("Document uploaded successfully", "success"); if (!skipFetch) fetchTemplatesAndDocs(); } else { const data = await res.json().catch(() => ({})); showToast(data.detail || "Upload failed", "error"); } } catch (err) { console.error(err); showToast("Upload failed due to network error", "error"); } }; const deleteDoc = async (id) => { if (!window.confirm("Delete this document?\nThis action cannot be undone.")) return; try { const res = await authFetch(`/api/upload/business-documents/${id}`, { method: "DELETE" }); if (res.ok) { showToast("Document deleted", "success"); fetchTemplatesAndDocs(); } else { showToast("Delete failed", "error"); } } catch (err) { console.error(err); showToast("Delete failed", "error"); } }; const viewDoc = async (storageKey) => { try { const res = await authFetch(`/api/upload/view-url?key=${encodeURIComponent(storageKey)}`); if (res.ok) { const data = await res.json(); window.open(data.url, "_blank"); } else { showToast("Could not generate view link", "error"); } } catch (err) { console.error(err); showToast("Could not generate view link", "error"); } }; const handleExtraUpload = async (e) => { const files = Array.from(e.target.files); const validFiles = []; for (let file of files) { if (file.size > 10 * 1024 * 1024) { showToast(`File ${file.name} exceeds 10MB limit`, "error"); continue; } validFiles.push(file); } if (validFiles.length > 0) { await Promise.all(validFiles.map((file, idx) => uploadFile(file, `other_${Date.now()}_${idx}`, true) )); fetchTemplatesAndDocs(); } e.target.value = ""; }; const notifyComplete = async () => { try { const res = await authFetch("/api/upload/notify-upload-complete", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ file_count: docs.length, doc_category: "Business" }) }); if (res.ok) { showToast("Documents sent for approval", "success"); } else { showToast("Failed to send for approval", "error"); } } catch (err) { console.error(err); showToast("Error sending for approval", "error"); } }; const years = [new Date().getFullYear(), new Date().getFullYear() - 1, new Date().getFullYear() - 2]; return (
← Back to Dashboard

Business Documents

Upload your business tax documents securely

Required Documents

{loading ? (
Loading templates...
) : templates.length === 0 ? (
No required documents for this tax year.
) : templates.map((dt, i) => { const uploaded = docs.filter(d => d.doc_type === dt.name); const latestDoc = uploaded[0] || null; const isRejected = latestDoc?.review_status === 'rejected'; const isApproved = latestDoc?.review_status === 'approved'; const isFiled = latestDoc?.review_status === 'filed'; return (
{i+1}. {dt.name}
{isRejected ? (
cancel REJECTED {latestDoc.review_note && (
⚠️ Reason: "{latestDoc.review_note}"
)}
Please correct and re-upload below.
) : isApproved ? ( check_circle APPROVED ) : isFiled ? ( inventory_2 FILED ) : uploaded.length ? ( pending Pending Review ) : dt.download ? ( View Template ) : ( "Missing" )}
fileInputRefs.current[dt.name] = el} onChange={(e) => { if (e.target.files[0]) { uploadFile(e.target.files[0], dt.name); e.target.value = null; } }} />
{dt.download && ( download Template )} {/* Show Upload button if no doc OR doc is rejected (re-upload allowed) */} {(!uploaded.length || isRejected) && ( )}
{uploaded.map(doc => (
{!isApproved && !isFiled && ( )}
))}
); })}

Additional Documents

Upload any supplementary business documents

extraFileRef.current?.click()} tabIndex={0} onKeyDown={(e) => { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); extraFileRef.current?.click(); } }} >
folder
Drag & drop files here
or click to browse · PDF, JPG, PNG, WEBP, DOC, EXCEL · Max 10MB
{docs.filter(d => d.doc_type.startsWith("other")).map(doc => (
check_circle
{doc.filename}
))}

Finished Uploading?

Send your business documents for review.

); }