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 (
Upload your business tax documents securely
Upload any supplementary business documents
Send your business documents for review.