4f92b8bffb
* feat: appointment scheduling, client/pet/service/staff CRUD UI - Weekly calendar view with navigation, color-coded by status - Booking form with client→pet→service→staff→date/time flow - Double-booking conflict detection on POST/PATCH appointments - DELETE /api/appointments endpoint - Staff API route (/api/staff) with full CRUD - Clients page: searchable list, create/edit clients, add/edit pets - Services page: table with create/edit/toggle-active - Staff page: table with create/edit/toggle-active - Nav bar with active-link highlighting, Staff link added Resolves GitHub groombook/groombook#1, #2, #8 Co-Authored-By: Paperclip <noreply@paperclip.ing> * fix: remove unused import, fix useCallback deps - Remove unused `or` import from drizzle-orm in appointments route - Compute week end directly in loadAppointments callback to avoid exhaustive-deps lint warning (weekEnd derived from weekStart) Co-Authored-By: Paperclip <noreply@paperclip.ing> * chore: add pnpm lockfile Required for CI --frozen-lockfile installs. Co-Authored-By: Paperclip <noreply@paperclip.ing> * fix: resolve all typecheck, lint, and test failures - Add @types/node to packages/db devDependencies (typecheck was missing process) - Re-export drizzle-orm helpers (eq, gte, etc.) from @groombook/db to avoid duplicate-instance type conflicts; remove drizzle-orm direct dep from API - Add @hono/zod-validator and jose as direct API dependencies - Merge duplicate @groombook/db imports in all route files - Fix noUncheckedIndexedAccess errors: appointments PATCH, web calendar grid - Fix weightKg/dateOfBirth type conversion in pets route (numeric→string, string→Date) - Add eslint.config.js for API and web (ESLint 9 flat config format) - Add vitest.config.ts with passWithNoTests for API and web Co-Authored-By: Paperclip <noreply@paperclip.ing> --------- Co-authored-by: Groom Book CTO <cto@groombook.app> Co-authored-by: Paperclip <noreply@paperclip.ing>
163 lines
7.3 KiB
TypeScript
163 lines
7.3 KiB
TypeScript
import { useEffect, useState } from "react";
|
|
import type { Staff } from "@groombook/types";
|
|
|
|
interface StaffForm {
|
|
name: string;
|
|
email: string;
|
|
role: "groomer" | "receptionist" | "manager";
|
|
}
|
|
|
|
const EMPTY_FORM: StaffForm = { name: "", email: "", role: "groomer" };
|
|
|
|
export function StaffPage() {
|
|
const [staff, setStaff] = useState<Staff[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [editing, setEditing] = useState<Staff | null>(null);
|
|
const [showForm, setShowForm] = useState(false);
|
|
const [form, setForm] = useState<StaffForm>(EMPTY_FORM);
|
|
const [formError, setFormError] = useState<string | null>(null);
|
|
const [saving, setSaving] = useState(false);
|
|
|
|
async function load() {
|
|
const r = await fetch("/api/staff?includeInactive=true");
|
|
if (!r.ok) throw new Error(`HTTP ${r.status}`);
|
|
setStaff((await r.json()) as Staff[]);
|
|
}
|
|
|
|
useEffect(() => {
|
|
load()
|
|
.catch((e: unknown) => setError(e instanceof Error ? e.message : "Unknown error"))
|
|
.finally(() => setLoading(false));
|
|
}, []);
|
|
|
|
function openNew() {
|
|
setEditing(null);
|
|
setForm(EMPTY_FORM);
|
|
setFormError(null);
|
|
setShowForm(true);
|
|
}
|
|
|
|
function openEdit(s: Staff) {
|
|
setEditing(s);
|
|
setForm({ name: s.name, email: s.email, role: s.role });
|
|
setFormError(null);
|
|
setShowForm(true);
|
|
}
|
|
|
|
async function submit(e: React.FormEvent) {
|
|
e.preventDefault();
|
|
setSaving(true);
|
|
setFormError(null);
|
|
try {
|
|
const res = editing
|
|
? await fetch(`/api/staff/${editing.id}`, { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name: form.name, role: form.role }) })
|
|
: await fetch("/api/staff", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(form) });
|
|
if (!res.ok) {
|
|
const err = (await res.json()) as { error?: string };
|
|
throw new Error(err.error ?? `HTTP ${res.status}`);
|
|
}
|
|
setShowForm(false);
|
|
await load();
|
|
} catch (e: unknown) {
|
|
setFormError(e instanceof Error ? e.message : "Failed to save");
|
|
} finally {
|
|
setSaving(false);
|
|
}
|
|
}
|
|
|
|
async function toggleActive(s: Staff) {
|
|
await fetch(`/api/staff/${s.id}`, { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ active: !s.active }) });
|
|
await load();
|
|
}
|
|
|
|
if (loading) return <p style={{ padding: "1rem" }}>Loading…</p>;
|
|
if (error) return <p style={{ padding: "1rem", color: "red" }}>Error: {error}</p>;
|
|
|
|
return (
|
|
<div style={{ fontFamily: "system-ui, sans-serif" }}>
|
|
<div style={{ display: "flex", alignItems: "center", gap: "1rem", marginBottom: "1rem" }}>
|
|
<h1 style={{ margin: 0 }}>Staff</h1>
|
|
<button onClick={openNew} style={{ ...btnStyle, backgroundColor: "#3b82f6", color: "#fff", borderColor: "#3b82f6", marginLeft: "auto" }}>
|
|
+ Add Staff
|
|
</button>
|
|
</div>
|
|
|
|
{staff.length === 0 ? (
|
|
<p>No staff members yet.</p>
|
|
) : (
|
|
<table style={{ width: "100%", borderCollapse: "collapse", fontSize: 14 }}>
|
|
<thead>
|
|
<tr style={{ background: "#f8fafc" }}>
|
|
{["Name", "Email", "Role", "Status", ""].map((h) => (
|
|
<th key={h} style={{ textAlign: "left", padding: "0.5rem 0.75rem", borderBottom: "1px solid #e2e8f0" }}>{h}</th>
|
|
))}
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{staff.map((s) => (
|
|
<tr key={s.id} style={{ opacity: s.active ? 1 : 0.5 }}>
|
|
<td style={tdStyle}>{s.name}</td>
|
|
<td style={tdStyle}>{s.email}</td>
|
|
<td style={tdStyle}><span style={{ textTransform: "capitalize" }}>{s.role}</span></td>
|
|
<td style={tdStyle}>
|
|
<span style={{ padding: "2px 8px", borderRadius: 12, fontSize: 11, fontWeight: 600, background: s.active ? "#d1fae5" : "#f3f4f6", color: s.active ? "#065f46" : "#6b7280" }}>
|
|
{s.active ? "Active" : "Inactive"}
|
|
</span>
|
|
</td>
|
|
<td style={{ ...tdStyle, whiteSpace: "nowrap" }}>
|
|
<button onClick={() => openEdit(s)} style={{ ...btnStyle, marginRight: "0.4rem" }}>Edit</button>
|
|
<button onClick={() => toggleActive(s)} style={btnStyle}>{s.active ? "Deactivate" : "Activate"}</button>
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
)}
|
|
|
|
{showForm && (
|
|
<div
|
|
style={{ position: "fixed", inset: 0, background: "rgba(0,0,0,0.45)", display: "flex", alignItems: "center", justifyContent: "center", zIndex: 100 }}
|
|
onClick={(e) => { if (e.target === e.currentTarget) setShowForm(false); }}
|
|
>
|
|
<div style={{ background: "#fff", borderRadius: 8, padding: "1.5rem", maxWidth: 400, width: "calc(100% - 2rem)", boxShadow: "0 20px 60px rgba(0,0,0,0.3)" }}>
|
|
<h2 style={{ marginTop: 0 }}>{editing ? "Edit Staff" : "New Staff Member"}</h2>
|
|
<form onSubmit={submit}>
|
|
<div style={{ marginBottom: "0.75rem" }}>
|
|
<label style={labelStyle}>Full name</label>
|
|
<input value={form.name} onChange={(e) => setForm((f) => ({ ...f, name: e.target.value }))} required style={inputStyle} />
|
|
</div>
|
|
{!editing && (
|
|
<div style={{ marginBottom: "0.75rem" }}>
|
|
<label style={labelStyle}>Email</label>
|
|
<input type="email" value={form.email} onChange={(e) => setForm((f) => ({ ...f, email: e.target.value }))} required style={inputStyle} />
|
|
</div>
|
|
)}
|
|
<div style={{ marginBottom: "0.75rem" }}>
|
|
<label style={labelStyle}>Role</label>
|
|
<select value={form.role} onChange={(e) => setForm((f) => ({ ...f, role: e.target.value as StaffForm["role"] }))} style={inputStyle}>
|
|
<option value="groomer">Groomer</option>
|
|
<option value="receptionist">Receptionist</option>
|
|
<option value="manager">Manager</option>
|
|
</select>
|
|
</div>
|
|
{formError && <p style={{ color: "red", margin: "0.5rem 0 0" }}>{formError}</p>}
|
|
<div style={{ display: "flex", gap: "0.5rem", marginTop: "1rem" }}>
|
|
<button type="submit" disabled={saving} style={{ ...btnStyle, backgroundColor: "#3b82f6", color: "#fff", borderColor: "#3b82f6" }}>
|
|
{saving ? "Saving…" : editing ? "Save Changes" : "Add Staff"}
|
|
</button>
|
|
<button type="button" onClick={() => setShowForm(false)} style={btnStyle}>Cancel</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
const btnStyle: React.CSSProperties = { padding: "0.35rem 0.75rem", border: "1px solid #d1d5db", borderRadius: 4, background: "#f9fafb", cursor: "pointer", fontSize: 13 };
|
|
const inputStyle: React.CSSProperties = { width: "100%", padding: "0.4rem 0.5rem", border: "1px solid #d1d5db", borderRadius: 4, fontSize: 14, boxSizing: "border-box" };
|
|
const labelStyle: React.CSSProperties = { display: "block", fontWeight: 600, marginBottom: "0.25rem", fontSize: 13, color: "#374151" };
|
|
const tdStyle: React.CSSProperties = { padding: "0.5rem 0.75rem", borderBottom: "1px solid #e2e8f0" };
|