Co-authored-by: renee-png <262607627+renee-png@users.noreply.github.com>
This commit is contained in:
gpt-engineer-app[bot]
2026-04-18 07:33:52 +00:00
co-authored by renee-png
parent 160bfdda30
commit 340be32c93
@@ -56,12 +56,16 @@ export function ConvertToCollectionsDialog({
const [selected, setSelected] = useState<Set<string>>(new Set());
const [search, setSearch] = useState("");
const [status, setStatus] = useState("none");
const [includeEmpty, setIncludeEmpty] = useState(false);
const [emptyName, setEmptyName] = useState("");
useEffect(() => {
if (!open) return;
setSelected(new Set());
setSearch("");
setStatus("none");
setIncludeEmpty(false);
setEmptyName("");
(async () => {
setLoading(true);
const [{ data: hos }, { data: cols }] = await Promise.all([
@@ -77,7 +81,9 @@ export function ConvertToCollectionsDialog({
.eq("case_id", caseId),
]);
setHomeowners(hos ?? []);
setExistingIds(new Set((cols ?? []).map((c) => c.homeowner_id)));
setExistingIds(
new Set((cols ?? []).map((c) => c.homeowner_id).filter((id): id is string => !!id)),
);
setLoading(false);
})();
}, [open, caseId, clientId]);
@@ -123,17 +129,26 @@ export function ConvertToCollectionsDialog({
};
const submit = async () => {
if (selected.size === 0) {
toast.error("Pick at least one homeowner");
if (selected.size === 0 && !includeEmpty) {
toast.error("Pick at least one homeowner or enable an empty collection");
return;
}
setSubmitting(true);
const rows = Array.from(selected).map((homeowner_id) => ({
const rows: any[] = Array.from(selected).map((homeowner_id) => ({
case_id: caseId,
homeowner_id,
status,
created_by: user?.id,
}));
if (includeEmpty) {
rows.push({
case_id: caseId,
homeowner_id: null,
status,
name: emptyName.trim() || null,
created_by: user?.id,
});
}
const { error } = await supabase.from("collections").insert(rows);
setSubmitting(false);
if (error) {