Changes
Co-authored-by: renee-png <262607627+renee-png@users.noreply.github.com>
This commit is contained in:
co-authored by
renee-png
parent
2236a45d91
commit
3483e065ec
@@ -0,0 +1,220 @@
|
||||
import { createFileRoute, useNavigate } from "@tanstack/react-router";
|
||||
import { useMemo, useState } from "react";
|
||||
import { ProtectedLayout } from "@/components/protected-layout";
|
||||
import { PageContainer, PageHeader } from "@/components/app-shell";
|
||||
import { Card, CardContent } from "@/components/ui/card";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { Textarea } from "@/components/ui/textarea";
|
||||
import {
|
||||
Select, SelectContent, SelectItem, SelectTrigger, SelectValue,
|
||||
} from "@/components/ui/select";
|
||||
import { FL_CIRCUITS } from "@/lib/florida";
|
||||
import { Packer } from "docx";
|
||||
import { buildPleadingDoc, downloadPleading } from "@/lib/docx-pleading";
|
||||
import { supabase } from "@/integrations/supabase/client";
|
||||
import { useAuth } from "@/lib/auth";
|
||||
import { toast } from "sonner";
|
||||
import { Download, Save, Loader2 } from "lucide-react";
|
||||
|
||||
export const Route = createFileRoute("/documents/pleading/new")({
|
||||
component: PleadingNewPage,
|
||||
});
|
||||
|
||||
function PleadingNewPage() {
|
||||
const { user } = useAuth();
|
||||
const navigate = useNavigate();
|
||||
const [courtType, setCourtType] = useState<"CIRCUIT" | "COUNTY">("CIRCUIT");
|
||||
const [circuit, setCircuit] = useState<string>("ELEVENTH");
|
||||
const [county, setCounty] = useState<string>("Miami-Dade");
|
||||
const [plaintiffs, setPlaintiffs] = useState("");
|
||||
const [defendants, setDefendants] = useState("");
|
||||
const [caseNumber, setCaseNumber] = useState("");
|
||||
const [title, setTitle] = useState("");
|
||||
const [body, setBody] = useState("");
|
||||
const [docName, setDocName] = useState("Pleading");
|
||||
const [saving, setSaving] = useState(false);
|
||||
|
||||
const counties = useMemo(() => {
|
||||
const c = FL_CIRCUITS.find((x) => x.value === circuit);
|
||||
return c?.counties ?? [];
|
||||
}, [circuit]);
|
||||
|
||||
// Reset county when circuit changes if current county isn't valid
|
||||
const handleCircuitChange = (v: string) => {
|
||||
setCircuit(v);
|
||||
const c = FL_CIRCUITS.find((x) => x.value === v);
|
||||
if (c && !c.counties.includes(county)) setCounty(c.counties[0]);
|
||||
};
|
||||
|
||||
const input = {
|
||||
courtType, circuit, county,
|
||||
plaintiffs, defendants, caseNumber,
|
||||
title, body,
|
||||
};
|
||||
|
||||
const headerLine1 = `IN THE ${courtType} COURT OF THE ${circuit} JUDICIAL CIRCUIT,`;
|
||||
const headerLine2 = `IN AND FOR ${county.toUpperCase()} COUNTY, FLORIDA`;
|
||||
|
||||
const onDownload = async () => {
|
||||
await downloadPleading(input, docName || "Pleading");
|
||||
};
|
||||
|
||||
const onSave = async () => {
|
||||
setSaving(true);
|
||||
try {
|
||||
const doc = buildPleadingDoc(input);
|
||||
const blob = await Packer.toBlob(doc);
|
||||
const safe = (docName || "Pleading").replace(/[^a-zA-Z0-9._-]/g, "_");
|
||||
const path = `${user?.id}/${Date.now()}-${safe}.docx`;
|
||||
const { error: upErr } = await supabase.storage.from("generated-documents").upload(path, blob, {
|
||||
contentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||
});
|
||||
if (upErr) throw upErr;
|
||||
const { error: insErr } = await supabase.from("generated_documents").insert({
|
||||
name: docName || "Pleading",
|
||||
kind: "pleading",
|
||||
payload: input,
|
||||
storage_path: path,
|
||||
created_by: user?.id,
|
||||
});
|
||||
if (insErr) throw insErr;
|
||||
toast.success("Pleading saved");
|
||||
navigate({ to: "/documents" });
|
||||
} catch (e: any) {
|
||||
toast.error(e.message ?? "Save failed");
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<ProtectedLayout>
|
||||
<PageContainer>
|
||||
<PageHeader
|
||||
title="New Pleading"
|
||||
description="Florida court caption · Bookman Old Style, 12pt"
|
||||
actions={
|
||||
<>
|
||||
<Button variant="outline" onClick={onDownload}><Download className="h-4 w-4 mr-2" /> Download .docx</Button>
|
||||
<Button onClick={onSave} disabled={saving}>
|
||||
{saving ? <Loader2 className="h-4 w-4 mr-2 animate-spin" /> : <Save className="h-4 w-4 mr-2" />}
|
||||
Save & download
|
||||
</Button>
|
||||
</>
|
||||
}
|
||||
/>
|
||||
|
||||
<div className="grid lg:grid-cols-2 gap-6">
|
||||
{/* Form */}
|
||||
<Card>
|
||||
<CardContent className="p-5 space-y-5">
|
||||
<div className="space-y-1.5">
|
||||
<Label>Document name</Label>
|
||||
<Input value={docName} onChange={(e) => setDocName(e.target.value)} placeholder="e.g. Complaint - Smith v. Jones" />
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 sm:grid-cols-3 gap-3">
|
||||
<div className="space-y-1.5">
|
||||
<Label>Court</Label>
|
||||
<Select value={courtType} onValueChange={(v) => setCourtType(v as any)}>
|
||||
<SelectTrigger><SelectValue /></SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="CIRCUIT">CIRCUIT</SelectItem>
|
||||
<SelectItem value="COUNTY">COUNTY</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label>Judicial circuit</Label>
|
||||
<Select value={circuit} onValueChange={handleCircuitChange}>
|
||||
<SelectTrigger><SelectValue /></SelectTrigger>
|
||||
<SelectContent className="max-h-72">
|
||||
{FL_CIRCUITS.map((c) => (
|
||||
<SelectItem key={c.value} value={c.value}>{c.label} ({c.value})</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label>County</Label>
|
||||
<Select value={county} onValueChange={setCounty}>
|
||||
<SelectTrigger><SelectValue /></SelectTrigger>
|
||||
<SelectContent className="max-h-72">
|
||||
{counties.map((co) => (
|
||||
<SelectItem key={co} value={co}>{co}</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
|
||||
<div className="space-y-1.5">
|
||||
<Label>Plaintiff(s)</Label>
|
||||
<Textarea rows={3} value={plaintiffs} onChange={(e) => setPlaintiffs(e.target.value)} placeholder="One name per line" />
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label>Defendant(s)</Label>
|
||||
<Textarea rows={3} value={defendants} onChange={(e) => setDefendants(e.target.value)} placeholder="One name per line" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="space-y-1.5">
|
||||
<Label>Case No.</Label>
|
||||
<Input value={caseNumber} onChange={(e) => setCaseNumber(e.target.value)} placeholder="e.g. 2025-CA-001234" />
|
||||
</div>
|
||||
|
||||
<div className="space-y-1.5">
|
||||
<Label>Pleading title (optional)</Label>
|
||||
<Input value={title} onChange={(e) => setTitle(e.target.value)} placeholder="e.g. COMPLAINT FOR DAMAGES" />
|
||||
</div>
|
||||
|
||||
<div className="space-y-1.5">
|
||||
<Label>Body (optional)</Label>
|
||||
<Textarea rows={10} value={body} onChange={(e) => setBody(e.target.value)} placeholder="Type the body of the pleading..." />
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Preview */}
|
||||
<Card>
|
||||
<CardContent className="p-0">
|
||||
<div className="bg-muted/30 px-4 py-2 border-b text-xs uppercase tracking-wider text-muted-foreground">Preview</div>
|
||||
<div
|
||||
className="p-10 bg-white text-black min-h-[600px]"
|
||||
style={{ fontFamily: '"Bookman Old Style", "URW Bookman", Georgia, serif', fontSize: 12 }}
|
||||
>
|
||||
<div className="text-center font-bold leading-snug">
|
||||
<div>{headerLine1}</div>
|
||||
<div>{headerLine2}</div>
|
||||
</div>
|
||||
|
||||
<div className="mt-6 flex">
|
||||
<div className="flex-1 pr-4 border-r border-black">
|
||||
<div className="whitespace-pre-line min-h-[1.5em]">{plaintiffs || " "}</div>
|
||||
<div className="mt-3">Plaintiff(s),</div>
|
||||
<div className="mt-3">v.</div>
|
||||
<div className="mt-3 whitespace-pre-line min-h-[1.5em]">{defendants || " "}</div>
|
||||
<div className="mt-3">Defendant(s).</div>
|
||||
</div>
|
||||
<div className="w-[40%] pl-4">
|
||||
<div className="font-bold">CASE NO.: {caseNumber}</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{title && (
|
||||
<div className="text-center font-bold mt-8">{title.toUpperCase()}</div>
|
||||
)}
|
||||
{body && (
|
||||
<div className="mt-4 whitespace-pre-line">{body}</div>
|
||||
)}
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
</PageContainer>
|
||||
</ProtectedLayout>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user