({ isOpen, onClose, userId, onGroupsChange }: GroupsDialogProps)
| 36 | } |
| 37 | |
| 38 | export function GroupsDialog({ isOpen, onClose, userId, onGroupsChange }: GroupsDialogProps) { |
| 39 | const [groups, setGroups] = useState<Group[]>([]) |
| 40 | const supabase = createClient() |
| 41 | const router = useRouter() |
| 42 | const [newGroupId, setNewGroupId] = useState<string | null>(null) |
| 43 | const [hasChanges, setHasChanges] = useState(false) |
| 44 | const [openColorPicker, setOpenColorPicker] = useState<string | null>(null) |
| 45 | |
| 46 | const saveGroups = useCallback(async (groupsToSave: Group[]) => { |
| 47 | const { error } = await supabase.from("groups").upsert(groupsToSave) |
| 48 | if (error) { |
| 49 | clientLogger.error('Error updating groups', { error }) |
| 50 | } else { |
| 51 | setHasChanges(true) |
| 52 | router.refresh() |
| 53 | } |
| 54 | }, [supabase, router]) |
| 55 | |
| 56 | const debouncedSave = useCallback(debounce(saveGroups, 500), [saveGroups]) |
| 57 | |
| 58 | useEffect(() => { |
| 59 | return () => { |
| 60 | debouncedSave.cancel() |
| 61 | } |
| 62 | }, [debouncedSave]) |
| 63 | |
| 64 | useEffect(() => { |
| 65 | fetchGroups() |
| 66 | }, []) |
| 67 | |
| 68 | const fetchGroups = async () => { |
| 69 | const { data, error } = await supabase |
| 70 | .from("groups") |
| 71 | .select("*") |
| 72 | .eq("created_by", userId) |
| 73 | |
| 74 | if (error) { |
| 75 | clientLogger.error('Error fetching groups', { error }) |
| 76 | toast({ variant: "destructive", description: "Failed to fetch groups" }) |
| 77 | } else { |
| 78 | setGroups(data || []) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | const handleGroupChange = ( |
| 83 | index: number, |
| 84 | field: keyof Group, |
| 85 | value: string |
| 86 | ) => { |
| 87 | const newGroups = [...groups] |
| 88 | newGroups[index] = { ...newGroups[index], [field]: value } |
| 89 | setGroups(newGroups) |
| 90 | debouncedSave(newGroups) |
| 91 | } |
| 92 | |
| 93 | const handleRemoveGroup = async (id: string) => { |
| 94 | const { error } = await supabase.from("groups").delete().eq("id", id) |
| 95 |
nothing calls this directly
no test coverage detected