import { useEffect, useState } from 'react'; import { useNavigate } from 'react-router-dom'; import Layout from '../../components/Layout'; import StatusBadge from '../../components/StatusBadge'; import { supabase } from '../../lib/supabase'; import { useAuth } from '../../context/AuthContext'; import { readPageCache, writePageCache } from '../../lib/pageCache'; const STATUS_BADGE = { draft: 'not_started', submitted: 'in_progress', paid: 'client_approved' }; const STATUS_LABEL = { draft: 'Draft', submitted: 'Submitted', paid: 'Paid' }; function fmt(val) { return `$${Number(val || 0).toFixed(2)}`; } function invoiceTotal(items) { return (items || []).reduce((s, i) => s + Number(i.unit_price || 0) * Number(i.quantity || 1), 0); } export default function MyInvoices() { const { currentUser } = useAuth(); const navigate = useNavigate(); const cacheKey = `ext-invoices:${currentUser?.id}`; const cached = readPageCache(cacheKey, 3 * 60_000); const [invoices, setInvoices] = useState(() => cached || []); const [loading, setLoading] = useState(() => !cached); const [error, setError] = useState(''); useEffect(() => { if (!currentUser?.id) { setLoading(false); return; } supabase .from('subcontractor_invoices') .select('*, items:subcontractor_invoice_items(*)') .order('created_at', { ascending: false }) .then(({ data, error: err }) => { if (err) setError(err.message); else { setInvoices(data || []); writePageCache(cacheKey, data || []); } setLoading(false); }); }, [currentUser?.id]); // eslint-disable-line react-hooks/exhaustive-deps return (
Invoices
Submit invoices to Fourge Branding for your completed work.
Payment terms: NET 30 from the date Fourge receives payment from the client.
{error &&
{error}
} {loading ? (
Loading invoices...
) : invoices.length === 0 ? (

No invoices yet

Create your first invoice to get paid for your completed work.

) : (
{invoices.map(inv => { const total = invoiceTotal(inv.items); return ( navigate(`/my-invoices-sub/${inv.id}`)}> ); })}
Invoice # Submitted Status Total
{inv.invoice_number} {inv.submitted_at ? new Date(inv.submitted_at).toLocaleDateString() : } {fmt(total)}
)}
); }