|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// Copyright 2026 Aaron K. Clark |
| 3 | +import { useEffect, useState, useCallback } from 'react'; |
| 4 | +import { useAuth } from '../auth.jsx'; |
| 5 | +import { listCustomers, listJobs, listTimeEntries, createTimeEntry, listFrom } from '../api.js'; |
| 6 | + |
| 7 | +function toIso(date, time) { |
| 8 | + if (!date || !time) return null; |
| 9 | + const d = new Date(`${date}T${time}:00`); |
| 10 | + return Number.isNaN(d.getTime()) ? null : d.toISOString(); |
| 11 | +} |
| 12 | +const today = () => new Date().toISOString().slice(0, 10); |
| 13 | +function fmtMins(m) { |
| 14 | + if (m == null) return '—'; |
| 15 | + return `${Math.floor(m / 60)}h ${m % 60}m`; |
| 16 | +} |
| 17 | + |
| 18 | +export default function TimeTracking() { |
| 19 | + const { user } = useAuth(); |
| 20 | + const companyId = user?.companyId; |
| 21 | + const [customers, setCustomers] = useState([]); |
| 22 | + const [jobs, setJobs] = useState([]); |
| 23 | + const [entries, setEntries] = useState([]); |
| 24 | + const [loading, setLoading] = useState(true); |
| 25 | + const [error, setError] = useState(null); |
| 26 | + const [busy, setBusy] = useState(false); |
| 27 | + const [form, setForm] = useState({ |
| 28 | + teCustId: '', teJobId: '', date: today(), start: '09:00', end: '10:00', |
| 29 | + teDescription: '', teBillable: true, |
| 30 | + }); |
| 31 | + |
| 32 | + const loadEntries = useCallback(async () => { |
| 33 | + if (!companyId) return; |
| 34 | + setEntries(listFrom(await listTimeEntries(companyId))); |
| 35 | + }, [companyId]); |
| 36 | + |
| 37 | + useEffect(() => { |
| 38 | + let live = true; |
| 39 | + (async () => { |
| 40 | + if (!companyId) return; |
| 41 | + setLoading(true); |
| 42 | + try { |
| 43 | + setCustomers(listFrom(await listCustomers(companyId))); |
| 44 | + await loadEntries(); |
| 45 | + if (live) setError(null); |
| 46 | + } catch (err) { |
| 47 | + if (live) setError(err.message); |
| 48 | + } finally { |
| 49 | + if (live) setLoading(false); |
| 50 | + } |
| 51 | + })(); |
| 52 | + return () => { live = false; }; |
| 53 | + }, [companyId, loadEntries]); |
| 54 | + |
| 55 | + // Load the selected client's jobs. |
| 56 | + useEffect(() => { |
| 57 | + let live = true; |
| 58 | + (async () => { |
| 59 | + if (!form.teCustId) { setJobs([]); return; } |
| 60 | + try { |
| 61 | + const js = listFrom(await listJobs(Number(form.teCustId))); |
| 62 | + if (live) setJobs(js); |
| 63 | + } catch { |
| 64 | + if (live) setJobs([]); |
| 65 | + } |
| 66 | + })(); |
| 67 | + return () => { live = false; }; |
| 68 | + }, [form.teCustId]); |
| 69 | + |
| 70 | + const set = (k) => (e) => |
| 71 | + setForm((f) => ({ ...f, [k]: e.target.type === 'checkbox' ? e.target.checked : e.target.value })); |
| 72 | + |
| 73 | + async function onLog(e) { |
| 74 | + e.preventDefault(); |
| 75 | + setError(null); |
| 76 | + setBusy(true); |
| 77 | + try { |
| 78 | + const teStartedAt = toIso(form.date, form.start); |
| 79 | + const teEndedAt = toIso(form.date, form.end); |
| 80 | + if (!teStartedAt || !teEndedAt) throw new Error('Enter a valid date, start, and end time.'); |
| 81 | + const body = { teCustId: Number(form.teCustId), teStartedAt, teEndedAt, teBillable: form.teBillable }; |
| 82 | + if (form.teJobId) body.teJobId = Number(form.teJobId); |
| 83 | + if (form.teDescription) body.teDescription = form.teDescription; |
| 84 | + await createTimeEntry(body); |
| 85 | + setForm((f) => ({ ...f, teDescription: '' })); |
| 86 | + await loadEntries(); |
| 87 | + } catch (err) { |
| 88 | + setError(err.message); |
| 89 | + } finally { |
| 90 | + setBusy(false); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + const custName = (id) => { |
| 95 | + const c = customers.find((x) => x.custId === id); |
| 96 | + return c ? (c.custCompanyName || [c.custFName, c.custLName].filter(Boolean).join(' ')) : `#${id}`; |
| 97 | + }; |
| 98 | + |
| 99 | + return ( |
| 100 | + <div> |
| 101 | + <h1>Time tracking</h1> |
| 102 | + <div className="card" style={{ marginBottom: 20 }}> |
| 103 | + <h2>Log time</h2> |
| 104 | + <form onSubmit={onLog} className="row-form"> |
| 105 | + <select value={form.teCustId} onChange={set('teCustId')} required> |
| 106 | + <option value="">Select client…</option> |
| 107 | + {customers.map((c) => ( |
| 108 | + <option key={c.custId} value={c.custId}> |
| 109 | + {c.custCompanyName || [c.custFName, c.custLName].filter(Boolean).join(' ')} |
| 110 | + </option> |
| 111 | + ))} |
| 112 | + </select> |
| 113 | + <select value={form.teJobId} onChange={set('teJobId')}> |
| 114 | + <option value="">(no job)</option> |
| 115 | + {jobs.map((j) => <option key={j.jobId} value={j.jobId}>{j.jobDesc}</option>)} |
| 116 | + </select> |
| 117 | + <input type="date" value={form.date} onChange={set('date')} required /> |
| 118 | + <input type="time" value={form.start} onChange={set('start')} required /> |
| 119 | + <input type="time" value={form.end} onChange={set('end')} required /> |
| 120 | + <input placeholder="Notes (optional)" value={form.teDescription} onChange={set('teDescription')} /> |
| 121 | + <label className="inline"><input type="checkbox" checked={form.teBillable} onChange={set('teBillable')} /> Billable</label> |
| 122 | + <button disabled={busy} type="submit">{busy ? 'Logging…' : 'Log time'}</button> |
| 123 | + </form> |
| 124 | + </div> |
| 125 | + |
| 126 | + {error && <p className="error">{error}</p>} |
| 127 | + {loading ? <p className="muted">Loading…</p> : ( |
| 128 | + <table className="table"> |
| 129 | + <thead><tr><th>Date</th><th>Client</th><th>Duration</th><th>Billable</th><th>Notes</th></tr></thead> |
| 130 | + <tbody> |
| 131 | + {entries.length === 0 && <tr><td colSpan={5} className="muted">No time logged yet.</td></tr>} |
| 132 | + {entries.map((t) => ( |
| 133 | + <tr key={t.teId}> |
| 134 | + <td>{t.teStartedAt ? t.teStartedAt.slice(0, 10) : '—'}</td> |
| 135 | + <td>{custName(t.teCustId)}</td> |
| 136 | + <td>{fmtMins(t.teMinutes)}</td> |
| 137 | + <td className="muted">{t.teBillable ? 'Yes' : 'No'}</td> |
| 138 | + <td className="muted">{t.teDescription || '—'}</td> |
| 139 | + </tr> |
| 140 | + ))} |
| 141 | + </tbody> |
| 142 | + </table> |
| 143 | + )} |
| 144 | + </div> |
| 145 | + ); |
| 146 | +} |
0 commit comments