Changes
Co-authored-by: renee-png <262607627+renee-png@users.noreply.github.com>
This commit is contained in:
co-authored by
renee-png
parent
46f2d14c5d
commit
1f3fd28159
@@ -0,0 +1,200 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Textarea } from "@/components/ui/textarea";
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
||||
import { supabase } from "@/integrations/supabase/client";
|
||||
import { useAuth } from "@/lib/auth";
|
||||
import { toast } from "sonner";
|
||||
import { Loader2 } from "lucide-react";
|
||||
|
||||
export const CONTACT_TYPES = [
|
||||
{ value: "opposing_counsel", label: "Opposing counsel" },
|
||||
{ value: "co_counsel", label: "Co-counsel" },
|
||||
{ value: "expert", label: "Expert" },
|
||||
{ value: "witness", label: "Witness" },
|
||||
{ value: "vendor", label: "Vendor" },
|
||||
{ value: "court", label: "Court / clerk" },
|
||||
{ value: "process_server", label: "Process server" },
|
||||
{ value: "client_contact", label: "Client contact" },
|
||||
{ value: "other", label: "Other" },
|
||||
] as const;
|
||||
|
||||
export interface ContactRecord {
|
||||
id?: string;
|
||||
name: string;
|
||||
company?: string | null;
|
||||
title?: string | null;
|
||||
email?: string | null;
|
||||
phone?: string | null;
|
||||
address_line1?: string | null;
|
||||
address_line2?: string | null;
|
||||
city?: string | null;
|
||||
state?: string | null;
|
||||
postal_code?: string | null;
|
||||
notes?: string | null;
|
||||
contact_type?: string | null;
|
||||
}
|
||||
|
||||
export function ContactFormDialog({
|
||||
open,
|
||||
onOpenChange,
|
||||
contact,
|
||||
onSaved,
|
||||
}: {
|
||||
open: boolean;
|
||||
onOpenChange: (v: boolean) => void;
|
||||
contact?: ContactRecord | null;
|
||||
onSaved?: (saved: { id: string; name: string }) => void;
|
||||
}) {
|
||||
const { user } = useAuth();
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [form, setForm] = useState<ContactRecord>({
|
||||
name: "",
|
||||
contact_type: "other",
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (open) {
|
||||
setForm(
|
||||
contact
|
||||
? { ...contact }
|
||||
: { name: "", contact_type: "other" },
|
||||
);
|
||||
}
|
||||
}, [open, contact]);
|
||||
|
||||
const set = <K extends keyof ContactRecord>(k: K, v: ContactRecord[K]) =>
|
||||
setForm((f) => ({ ...f, [k]: v }));
|
||||
|
||||
const onSave = async () => {
|
||||
if (!form.name.trim()) {
|
||||
toast.error("Name is required");
|
||||
return;
|
||||
}
|
||||
setSaving(true);
|
||||
const payload = {
|
||||
name: form.name.trim(),
|
||||
company: form.company || null,
|
||||
title: form.title || null,
|
||||
email: form.email || null,
|
||||
phone: form.phone || null,
|
||||
address_line1: form.address_line1 || null,
|
||||
address_line2: form.address_line2 || null,
|
||||
city: form.city || null,
|
||||
state: form.state || null,
|
||||
postal_code: form.postal_code || null,
|
||||
notes: form.notes || null,
|
||||
contact_type: form.contact_type || "other",
|
||||
};
|
||||
let saved: { id: string; name: string } | null = null;
|
||||
if (contact?.id) {
|
||||
const { data, error } = await supabase
|
||||
.from("contacts")
|
||||
.update(payload)
|
||||
.eq("id", contact.id)
|
||||
.select("id, name")
|
||||
.single();
|
||||
if (error) {
|
||||
toast.error(error.message);
|
||||
setSaving(false);
|
||||
return;
|
||||
}
|
||||
saved = data;
|
||||
} else {
|
||||
const { data, error } = await supabase
|
||||
.from("contacts")
|
||||
.insert({ ...payload, created_by: user?.id })
|
||||
.select("id, name")
|
||||
.single();
|
||||
if (error) {
|
||||
toast.error(error.message);
|
||||
setSaving(false);
|
||||
return;
|
||||
}
|
||||
saved = data;
|
||||
}
|
||||
setSaving(false);
|
||||
toast.success(contact?.id ? "Contact updated" : "Contact created");
|
||||
onOpenChange(false);
|
||||
if (saved) onSaved?.(saved);
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className="max-w-2xl">
|
||||
<DialogHeader>
|
||||
<DialogTitle>{contact?.id ? "Edit contact" : "New contact"}</DialogTitle>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
||||
<div className="space-y-1.5 sm:col-span-2">
|
||||
<Label>Name *</Label>
|
||||
<Input value={form.name} onChange={(e) => set("name", e.target.value)} />
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label>Type</Label>
|
||||
<Select value={form.contact_type ?? "other"} onValueChange={(v) => set("contact_type", v)}>
|
||||
<SelectTrigger><SelectValue /></SelectTrigger>
|
||||
<SelectContent>
|
||||
{CONTACT_TYPES.map((t) => (
|
||||
<SelectItem key={t.value} value={t.value}>{t.label}</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label>Title</Label>
|
||||
<Input value={form.title ?? ""} onChange={(e) => set("title", e.target.value)} />
|
||||
</div>
|
||||
<div className="space-y-1.5 sm:col-span-2">
|
||||
<Label>Company / Firm</Label>
|
||||
<Input value={form.company ?? ""} onChange={(e) => set("company", e.target.value)} />
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label>Email</Label>
|
||||
<Input type="email" value={form.email ?? ""} onChange={(e) => set("email", e.target.value)} />
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label>Phone</Label>
|
||||
<Input value={form.phone ?? ""} onChange={(e) => set("phone", e.target.value)} />
|
||||
</div>
|
||||
<div className="space-y-1.5 sm:col-span-2">
|
||||
<Label>Address line 1</Label>
|
||||
<Input value={form.address_line1 ?? ""} onChange={(e) => set("address_line1", e.target.value)} />
|
||||
</div>
|
||||
<div className="space-y-1.5 sm:col-span-2">
|
||||
<Label>Address line 2</Label>
|
||||
<Input value={form.address_line2 ?? ""} onChange={(e) => set("address_line2", e.target.value)} />
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label>City</Label>
|
||||
<Input value={form.city ?? ""} onChange={(e) => set("city", e.target.value)} />
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label>State</Label>
|
||||
<Input value={form.state ?? ""} onChange={(e) => set("state", e.target.value)} />
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label>Postal code</Label>
|
||||
<Input value={form.postal_code ?? ""} onChange={(e) => set("postal_code", e.target.value)} />
|
||||
</div>
|
||||
<div className="space-y-1.5 sm:col-span-2">
|
||||
<Label>Notes</Label>
|
||||
<Textarea rows={3} value={form.notes ?? ""} onChange={(e) => set("notes", e.target.value)} />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<DialogFooter>
|
||||
<Button variant="outline" onClick={() => onOpenChange(false)}>Cancel</Button>
|
||||
<Button onClick={onSave} disabled={saving}>
|
||||
{saving && <Loader2 className="h-4 w-4 mr-2 animate-spin" />}
|
||||
{contact?.id ? "Save" : "Create"}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,240 @@
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { Card, CardContent } from "@/components/ui/card";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog";
|
||||
import { supabase } from "@/integrations/supabase/client";
|
||||
import { useAuth } from "@/lib/auth";
|
||||
import { toast } from "sonner";
|
||||
import { Plus, Trash2, Mail, Phone, Building2, MapPin, Search, UserPlus } from "lucide-react";
|
||||
import { ContactFormDialog, CONTACT_TYPES } from "./contact-form-dialog";
|
||||
|
||||
interface LinkedContact {
|
||||
id: string; // join row id
|
||||
role: string | null;
|
||||
contact: {
|
||||
id: string;
|
||||
name: string;
|
||||
company: string | null;
|
||||
title: string | null;
|
||||
email: string | null;
|
||||
phone: string | null;
|
||||
contact_type: string;
|
||||
address_line1: string | null;
|
||||
city: string | null;
|
||||
state: string | null;
|
||||
postal_code: string | null;
|
||||
};
|
||||
}
|
||||
|
||||
export function ContactsLinkTab({
|
||||
parentId,
|
||||
parentTable,
|
||||
}: {
|
||||
parentId: string;
|
||||
parentTable: "case_contacts" | "client_contacts";
|
||||
}) {
|
||||
const { user } = useAuth();
|
||||
const fkColumn = parentTable === "case_contacts" ? "case_id" : "client_id";
|
||||
const [linked, setLinked] = useState<LinkedContact[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [pickerOpen, setPickerOpen] = useState(false);
|
||||
const [createOpen, setCreateOpen] = useState(false);
|
||||
|
||||
const load = async () => {
|
||||
setLoading(true);
|
||||
const { data, error } = await supabase
|
||||
.from(parentTable)
|
||||
.select("id, role, contact:contacts(id, name, company, title, email, phone, contact_type, address_line1, city, state, postal_code)")
|
||||
.eq(fkColumn, parentId);
|
||||
if (error) toast.error(error.message);
|
||||
setLinked(((data ?? []) as any[]).filter((d) => d.contact));
|
||||
setLoading(false);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
load();
|
||||
}, [parentId, parentTable]);
|
||||
|
||||
const linkContact = async (contactId: string) => {
|
||||
const payload: Record<string, unknown> = {
|
||||
contact_id: contactId,
|
||||
created_by: user?.id,
|
||||
};
|
||||
payload[fkColumn] = parentId;
|
||||
const { error } = await supabase.from(parentTable).insert(payload as any);
|
||||
if (error) {
|
||||
if (error.code === "23505") toast.info("Already linked");
|
||||
else toast.error(error.message);
|
||||
return;
|
||||
}
|
||||
toast.success("Contact linked");
|
||||
setPickerOpen(false);
|
||||
load();
|
||||
};
|
||||
|
||||
const unlink = async (joinId: string) => {
|
||||
const { error } = await supabase.from(parentTable).delete().eq("id", joinId);
|
||||
if (error) {
|
||||
toast.error(error.message);
|
||||
return;
|
||||
}
|
||||
setLinked((rows) => rows.filter((r) => r.id !== joinId));
|
||||
};
|
||||
|
||||
const linkedIds = useMemo(() => new Set(linked.map((l) => l.contact.id)), [linked]);
|
||||
|
||||
return (
|
||||
<Card className="border-border/60">
|
||||
<CardContent className="p-5">
|
||||
<div className="flex items-center justify-between mb-4">
|
||||
<div>
|
||||
<div className="text-xs uppercase tracking-wider text-muted-foreground font-medium">Contacts</div>
|
||||
<div className="text-sm text-muted-foreground">
|
||||
{linked.length} linked contact{linked.length === 1 ? "" : "s"}
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex gap-2">
|
||||
<Button variant="outline" size="sm" onClick={() => setCreateOpen(true)}>
|
||||
<UserPlus className="h-3.5 w-3.5 mr-1.5" /> New contact
|
||||
</Button>
|
||||
<Button size="sm" onClick={() => setPickerOpen(true)}>
|
||||
<Plus className="h-3.5 w-3.5 mr-1.5" /> Link existing
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{loading ? (
|
||||
<p className="text-sm text-muted-foreground">Loading…</p>
|
||||
) : linked.length === 0 ? (
|
||||
<p className="text-sm text-muted-foreground">No contacts linked yet.</p>
|
||||
) : (
|
||||
<ul className="divide-y">
|
||||
{linked.map((row) => {
|
||||
const c = row.contact;
|
||||
const typeLabel = CONTACT_TYPES.find((t) => t.value === c.contact_type)?.label ?? c.contact_type;
|
||||
const addr = [c.address_line1, c.city, c.state, c.postal_code].filter(Boolean).join(", ");
|
||||
return (
|
||||
<li key={row.id} className="py-3 flex items-start justify-between gap-4">
|
||||
<div className="min-w-0 flex-1">
|
||||
<div className="flex items-center gap-2 flex-wrap">
|
||||
<span className="font-medium">{c.name}</span>
|
||||
<Badge variant="outline" className="text-[10px]">{typeLabel}</Badge>
|
||||
{c.title && <span className="text-xs text-muted-foreground">{c.title}</span>}
|
||||
</div>
|
||||
<div className="mt-1 grid sm:grid-cols-2 gap-x-4 gap-y-1 text-xs text-muted-foreground">
|
||||
{c.company && <span className="flex items-center gap-1.5"><Building2 className="h-3 w-3" />{c.company}</span>}
|
||||
{c.email && <a href={`mailto:${c.email}`} className="flex items-center gap-1.5 hover:text-primary"><Mail className="h-3 w-3" />{c.email}</a>}
|
||||
{c.phone && <a href={`tel:${c.phone}`} className="flex items-center gap-1.5 hover:text-primary"><Phone className="h-3 w-3" />{c.phone}</a>}
|
||||
{addr && <span className="flex items-center gap-1.5"><MapPin className="h-3 w-3" />{addr}</span>}
|
||||
</div>
|
||||
</div>
|
||||
<Button variant="ghost" size="icon" onClick={() => unlink(row.id)} title="Unlink">
|
||||
<Trash2 className="h-4 w-4 text-destructive" />
|
||||
</Button>
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
)}
|
||||
|
||||
<ContactPickerDialog
|
||||
open={pickerOpen}
|
||||
onOpenChange={setPickerOpen}
|
||||
excludeIds={linkedIds}
|
||||
onPick={linkContact}
|
||||
/>
|
||||
<ContactFormDialog
|
||||
open={createOpen}
|
||||
onOpenChange={setCreateOpen}
|
||||
onSaved={async (saved) => {
|
||||
await linkContact(saved.id);
|
||||
}}
|
||||
/>
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
function ContactPickerDialog({
|
||||
open,
|
||||
onOpenChange,
|
||||
excludeIds,
|
||||
onPick,
|
||||
}: {
|
||||
open: boolean;
|
||||
onOpenChange: (v: boolean) => void;
|
||||
excludeIds: Set<string>;
|
||||
onPick: (id: string) => void;
|
||||
}) {
|
||||
const [q, setQ] = useState("");
|
||||
const [rows, setRows] = useState<any[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return;
|
||||
setQ("");
|
||||
supabase
|
||||
.from("contacts")
|
||||
.select("id, name, company, contact_type, email")
|
||||
.order("name")
|
||||
.limit(200)
|
||||
.then(({ data }) => setRows(data ?? []));
|
||||
}, [open]);
|
||||
|
||||
const filtered = useMemo(() => {
|
||||
const term = q.trim().toLowerCase();
|
||||
return rows
|
||||
.filter((r) => !excludeIds.has(r.id))
|
||||
.filter((r) => {
|
||||
if (!term) return true;
|
||||
return (
|
||||
r.name.toLowerCase().includes(term) ||
|
||||
(r.company ?? "").toLowerCase().includes(term) ||
|
||||
(r.email ?? "").toLowerCase().includes(term)
|
||||
);
|
||||
});
|
||||
}, [rows, q, excludeIds]);
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className="max-w-lg">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Link existing contact</DialogTitle>
|
||||
</DialogHeader>
|
||||
<div className="relative">
|
||||
<Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground" />
|
||||
<Input className="pl-9" placeholder="Search by name, company, email…" value={q} onChange={(e) => setQ(e.target.value)} autoFocus />
|
||||
</div>
|
||||
<div className="max-h-[400px] overflow-auto -mx-2">
|
||||
{filtered.length === 0 ? (
|
||||
<p className="text-sm text-muted-foreground p-3">No matching contacts.</p>
|
||||
) : (
|
||||
<ul className="divide-y">
|
||||
{filtered.map((c) => {
|
||||
const typeLabel = CONTACT_TYPES.find((t) => t.value === c.contact_type)?.label ?? c.contact_type;
|
||||
return (
|
||||
<li key={c.id}>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => onPick(c.id)}
|
||||
className="w-full text-left px-3 py-2.5 hover:bg-muted/60 flex items-center justify-between gap-3"
|
||||
>
|
||||
<div className="min-w-0">
|
||||
<div className="font-medium text-sm truncate">{c.name}</div>
|
||||
<div className="text-xs text-muted-foreground truncate">
|
||||
{[c.company, c.email].filter(Boolean).join(" · ") || "—"}
|
||||
</div>
|
||||
</div>
|
||||
<Badge variant="outline" className="text-[10px] shrink-0">{typeLabel}</Badge>
|
||||
</button>
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
)}
|
||||
</div>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user