File size: 6,244 Bytes
9ed5724 2a3a36f 713b4f3 9ed5724 713b4f3 9ed5724 713b4f3 9ed5724 2a3a36f 9ed5724 2a3a36f 9ed5724 2a3a36f 9ed5724 2a3a36f 9ed5724 2a3a36f 9ed5724 2a3a36f 9ed5724 2a3a36f 9ed5724 2a3a36f 9ed5724 2a3a36f 9ed5724 2a3a36f 9ed5724 2a3a36f 9ed5724 2a3a36f 9ed5724 2a3a36f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | import React, { useEffect, useState, useRef } from 'react';
import { Loader2 } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { cn } from '@/lib/utils';
/** API/CSV may return numbers; coerce before trim/compare so Inputs never throw. */
function strField(v) {
return String(v ?? '').trim();
}
function baselineFromProps(firstName, lastName, email, title) {
return {
firstName: String(firstName ?? ''),
lastName: String(lastName ?? ''),
email: String(email ?? ''),
title: String(title ?? ''),
};
}
function formsEqual(a, b) {
return (
strField(a.firstName) === strField(b.firstName) &&
strField(a.lastName) === strField(b.lastName) &&
strField(a.email) === strField(b.email) &&
strField(a.title) === strField(b.title)
);
}
/**
* Editable person fields (first / last name, email, title).
* `autoSave`: debounced PATCH after typing pauses (no Save button).
* Otherwise: Save button commits all fields.
*/
export default function ContactIdentityEditor({
firstName = '',
lastName = '',
email = '',
title = '',
onSave,
disabled = false,
className,
heading = 'Contact details',
autoSave = false,
}) {
const [form, setForm] = useState(() => baselineFromProps(firstName, lastName, email, title));
const [saving, setSaving] = useState(false);
const baselineRef = useRef(baselineFromProps(firstName, lastName, email, title));
useEffect(() => {
const b = baselineFromProps(firstName, lastName, email, title);
baselineRef.current = b;
setForm(b);
}, [firstName, lastName, email, title]);
const setField = (key, value) => setForm((f) => ({ ...f, [key]: value }));
const handleSave = async () => {
if (!onSave || disabled) return;
setSaving(true);
try {
await onSave({
first_name: form.firstName,
last_name: form.lastName,
email: form.email,
title: form.title,
});
} finally {
setSaving(false);
}
};
useEffect(() => {
if (!autoSave || !onSave || disabled) return;
const baseline = baselineRef.current;
if (formsEqual(form, baseline)) return;
const t = setTimeout(async () => {
setSaving(true);
try {
await onSave({
first_name: form.firstName,
last_name: form.lastName,
email: form.email,
title: form.title,
});
baselineRef.current = {
firstName: form.firstName,
lastName: form.lastName,
email: form.email,
title: form.title,
};
} finally {
setSaving(false);
}
}, 650);
return () => clearTimeout(t);
}, [form, autoSave, disabled, onSave, firstName, lastName, email, title]);
return (
<div className={cn('rounded-xl border border-slate-200 bg-white p-4 mb-6', className)}>
<div className="mb-3 flex items-center justify-between gap-2">
<h4 className="text-sm font-semibold text-slate-800">{heading}</h4>
{autoSave && saving ? (
<span className="flex items-center gap-1 text-xs text-slate-500">
<Loader2 className="h-3 w-3 animate-spin shrink-0" aria-hidden />
Saving…
</span>
) : null}
</div>
<div className="grid grid-cols-1 gap-3 text-sm sm:grid-cols-2">
<div>
<label className="mb-1 block text-xs font-medium text-slate-500">First name</label>
<Input
value={form.firstName}
onChange={(e) => setField('firstName', e.target.value)}
disabled={disabled}
className="text-sm"
/>
</div>
<div>
<label className="mb-1 block text-xs font-medium text-slate-500">Last name</label>
<Input
value={form.lastName}
onChange={(e) => setField('lastName', e.target.value)}
disabled={disabled}
className="text-sm"
/>
</div>
<div className="sm:col-span-2">
<label className="mb-1 block text-xs font-medium text-slate-500">Email</label>
<Input
type="email"
value={form.email}
onChange={(e) => setField('email', e.target.value)}
disabled={disabled}
className="text-sm"
/>
</div>
<div className="sm:col-span-2">
<label className="mb-1 block text-xs font-medium text-slate-500">Title</label>
<Input
value={form.title}
onChange={(e) => setField('title', e.target.value)}
disabled={disabled}
className="text-sm"
/>
</div>
</div>
{!autoSave ? (
<div className="mt-4">
<Button
type="button"
className="w-full sm:w-auto"
onClick={handleSave}
disabled={disabled || saving}
>
{saving ? (
<>
<Loader2 className="mr-2 h-4 w-4 animate-spin" aria-hidden />
Saving…
</>
) : (
'Save'
)}
</Button>
</div>
) : null}
</div>
);
}
|