- Undo Vessel→Cost Centre rename in admin (admin shows "Vessel Management" again) - Sidebar: "Cost Centres"→"Vessels", "Accounts"→"Accounting Codes" - PO forms (new/edit/import/manager-edit) now show both Vessels (with code) and Sites in the Cost Centre dropdown, encoded as v:<id> / s:<id> via a costCentreRef field - vesselId on PurchaseOrder is now nullable; siteId is set when a site is the cost centre - History, approvals, dashboard, my-orders, payments display vessel.name ?? site.name as Cost Centre - History and approvals cost centre filters use costCentreRef URL param supporting both types - Admin vessel form: adds Site assignment dropdown - Admin accounts: renamed to "Accounting Code" throughout (pages, forms, sidebar) - PO detail and exports: "Account" label renamed to "Accounting Code" - Site detail: "Assigned Vessels (Cost Centres)" heading; vessel detail breadcrumb fixed - Create PO links from vessel/site detail use ?costCentreRef= param - Export routes handle costCentreRef filter param (with legacy vesselId fallback) - DB migration: ALTER TABLE PurchaseOrder ALTER COLUMN vesselId DROP NOT NULL - CLAUDE.md updated with Cost Centre Model documentation Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
146 lines
5.5 KiB
TypeScript
146 lines
5.5 KiB
TypeScript
"use client";
|
|
|
|
import { useState } from "react";
|
|
import { useRouter } from "next/navigation";
|
|
import { AdminDialog } from "@/components/ui/admin-dialog";
|
|
import { createVessel, updateVessel, toggleVesselActive } from "./actions";
|
|
|
|
type SiteOption = { id: string; name: string };
|
|
|
|
type VesselRow = {
|
|
id: string;
|
|
name: string;
|
|
code: string;
|
|
siteId: string | null;
|
|
isActive: boolean;
|
|
};
|
|
|
|
const INPUT = "w-full rounded-lg border border-neutral-300 px-3 py-2 text-sm focus:border-primary-500 focus:outline-none focus:ring-2 focus:ring-primary-500/20";
|
|
|
|
function VesselFormFields({ vessel, sites }: { vessel?: VesselRow; sites: SiteOption[] }) {
|
|
return (
|
|
<div className="space-y-3">
|
|
{vessel && (
|
|
<div>
|
|
<label className="block text-xs font-medium text-neutral-700 mb-1">Code</label>
|
|
<p className="font-mono text-sm text-neutral-700 px-3 py-2 bg-neutral-50 rounded-lg border border-neutral-200">{vessel.code}</p>
|
|
</div>
|
|
)}
|
|
<div>
|
|
<label className="block text-xs font-medium text-neutral-700 mb-1">Vessel Name *</label>
|
|
<input name="name" defaultValue={vessel?.name} required className={INPUT} />
|
|
</div>
|
|
{sites.length > 0 && (
|
|
<div>
|
|
<label className="block text-xs font-medium text-neutral-700 mb-1">Assigned Site</label>
|
|
<select name="siteId" defaultValue={vessel?.siteId ?? ""} className={INPUT}>
|
|
<option value="">— No site —</option>
|
|
{sites.map((s) => (
|
|
<option key={s.id} value={s.id}>{s.name}</option>
|
|
))}
|
|
</select>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export function AddVesselButton({ sites }: { sites: SiteOption[] }) {
|
|
const router = useRouter();
|
|
const [open, setOpen] = useState(false);
|
|
const [pending, setPending] = useState(false);
|
|
const [error, setError] = useState("");
|
|
|
|
async function handleSubmit(e: React.FormEvent<HTMLFormElement>) {
|
|
e.preventDefault();
|
|
setPending(true);
|
|
setError("");
|
|
const result = await createVessel(new FormData(e.currentTarget));
|
|
if ("error" in result) { setError(result.error); setPending(false); }
|
|
else { setPending(false); setOpen(false); router.refresh(); }
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<button onClick={() => setOpen(true)}
|
|
className="rounded-lg bg-primary-600 px-4 py-2.5 text-sm font-semibold text-white hover:bg-primary-700 transition-colors">
|
|
+ Add Vessel
|
|
</button>
|
|
<AdminDialog title="Add Vessel" open={open} onClose={() => setOpen(false)}>
|
|
<form onSubmit={handleSubmit} className="space-y-4">
|
|
<VesselFormFields sites={sites} />
|
|
{error && <p className="text-sm text-danger-700 bg-danger-50 rounded-lg px-3 py-2">{error}</p>}
|
|
<div className="flex justify-end gap-3 pt-1">
|
|
<button type="button" onClick={() => setOpen(false)}
|
|
className="rounded-lg border border-neutral-300 px-4 py-2 text-sm font-medium text-neutral-700 hover:bg-neutral-50">
|
|
Cancel
|
|
</button>
|
|
<button type="submit" disabled={pending}
|
|
className="rounded-lg bg-primary-600 px-4 py-2 text-sm font-semibold text-white hover:bg-primary-700 disabled:opacity-60">
|
|
{pending ? "Creating…" : "Create Vessel"}
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</AdminDialog>
|
|
</>
|
|
);
|
|
}
|
|
|
|
export function EditVesselButton({
|
|
vessel,
|
|
sites,
|
|
open: controlledOpen,
|
|
onOpenChange,
|
|
}: {
|
|
vessel: VesselRow;
|
|
sites: SiteOption[];
|
|
open?: boolean;
|
|
onOpenChange?: (v: boolean) => void;
|
|
}) {
|
|
const router = useRouter();
|
|
const [internalOpen, setInternalOpen] = useState(false);
|
|
const [pending, setPending] = useState(false);
|
|
const [error, setError] = useState("");
|
|
|
|
const isControlled = controlledOpen !== undefined;
|
|
const open = isControlled ? controlledOpen : internalOpen;
|
|
const setOpen = isControlled ? (onOpenChange ?? (() => {})) : setInternalOpen;
|
|
|
|
async function handleSubmit(e: React.FormEvent<HTMLFormElement>) {
|
|
e.preventDefault();
|
|
setPending(true);
|
|
setError("");
|
|
const fd = new FormData(e.currentTarget);
|
|
fd.set("id", vessel.id);
|
|
const result = await updateVessel(fd);
|
|
if ("error" in result) { setError(result.error); setPending(false); }
|
|
else { setPending(false); setOpen(false); router.refresh(); }
|
|
}
|
|
|
|
return (
|
|
<>
|
|
{!isControlled && (
|
|
<button onClick={() => setOpen(true)}
|
|
className="rounded border border-primary-200 bg-primary-50 px-2.5 py-1 text-xs font-medium text-primary-700 hover:bg-primary-100 transition-colors">
|
|
Edit
|
|
</button>
|
|
)}
|
|
<AdminDialog title="Edit Vessel" open={open} onClose={() => setOpen(false)}>
|
|
<form onSubmit={handleSubmit} className="space-y-4">
|
|
<VesselFormFields vessel={vessel} sites={sites} />
|
|
{error && <p className="text-sm text-danger-700 bg-danger-50 rounded-lg px-3 py-2">{error}</p>}
|
|
<div className="flex justify-end gap-3 pt-1">
|
|
<button type="button" onClick={() => setOpen(false)}
|
|
className="rounded-lg border border-neutral-300 px-4 py-2 text-sm font-medium text-neutral-700 hover:bg-neutral-50">
|
|
Cancel
|
|
</button>
|
|
<button type="submit" disabled={pending}
|
|
className="rounded-lg bg-primary-600 px-4 py-2 text-sm font-semibold text-white hover:bg-primary-700 disabled:opacity-60">
|
|
{pending ? "Saving…" : "Save Changes"}
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</AdminDialog>
|
|
</>
|
|
);
|
|
}
|