All files / src/common/hooks useNewsletterSourceGroups.ts

100% Statements 98/98
80% Branches 20/25
100% Functions 13/13
100% Lines 98/98

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1191x 1x 1x   1x 285x     285x 285x 285x 70x 70x 285x     285x 285x 2x 2x 1x 1x 1x 2x 285x 1x 1x 285x     285x 285x 2x 2x 2x 2x       2x 2x 2x 1x 1x 1x 2x 285x 1x 1x 285x     285x 285x 2x 2x 1x 1x 1x 2x 285x 1x 1x 285x       285x 285x 1x 1x 285x     285x 285x 3x 3x 1x 1x 1x 3x 285x 1x 1x 1x 1x 1x 285x     285x 285x 3x 3x 1x 1x 1x 3x 285x 1x 1x 1x 1x 1x 285x   285x 285x 285x 285x 285x 285x 285x 285x 285x 285x 285x  
import { useMutation, useQuery } from '@tanstack/react-query';
import { useCache } from './useCache';
import { newsletterSourceGroupService } from '@common/services';
 
export const useNewsletterSourceGroups = () => {
  const { batchInvalidate } = useCache();
 
  // Fetch all groups for the current user with source counts
  const { data: groups = [], ...query } = useQuery({
    queryKey: ['newsletterSourceGroups'],
    queryFn: async () => {
      return await newsletterSourceGroupService.getGroups();
    },
  });
 
  // Create a new group with sources
  const createGroup = useMutation({
    mutationFn: async ({ name, sourceIds }: { name: string; sourceIds: string[] }) => {
      const result = await newsletterSourceGroupService.createGroup({ name, sourceIds });
      if (!result.success) {
        throw new Error(result.error || 'Failed to create group');
      }
      return result.group;
    },
    onSuccess: () => {
      batchInvalidate([{ queryKey: ['newsletterSourceGroups'] }]);
    },
  });
 
  // Update a group
  const updateGroup = useMutation({
    mutationFn: async ({
      id,
      name,
      sourceIds,
    }: {
      id: string;
      name?: string;
      sourceIds?: string[];
    }) => {
      const result = await newsletterSourceGroupService.updateGroup(id, { name, sourceIds });
      if (!result.success) {
        throw new Error(result.error || 'Failed to update group');
      }
      return result.group;
    },
    onSuccess: () => {
      batchInvalidate([{ queryKey: ['newsletterSourceGroups'] }]);
    },
  });
 
  // Delete a group
  const deleteGroup = useMutation({
    mutationFn: async (id: string) => {
      const result = await newsletterSourceGroupService.deleteGroup(id);
      if (!result.success) {
        throw new Error(result.error || 'Failed to delete group');
      }
      return result.success;
    },
    onSuccess: () => {
      batchInvalidate([{ queryKey: ['newsletterSourceGroups'] }]);
    },
  });
 
  // Get a single group with its sources
  // Get a single group
  const getGroup = useMutation({
    mutationFn: async (id: string) => {
      return await newsletterSourceGroupService.getGroup(id);
    },
  });
 
  // Add sources to a group
  const addSourcesToGroup = useMutation({
    mutationFn: async ({ groupId, sourceIds }: { groupId: string; sourceIds: string[] }) => {
      const result = await newsletterSourceGroupService.addSourcesToGroup(groupId, sourceIds);
      if (!result.success) {
        throw new Error(result.error || 'Failed to add sources to group');
      }
      return result.group;
    },
    onSuccess: (_, variables) => {
      batchInvalidate([
        { queryKey: ['newsletterSourceGroups'] },
        { queryKey: ['newsletterSourceGroup', variables.groupId] },
      ]);
    },
  });
 
  // Remove sources from a group
  const removeSourcesFromGroup = useMutation({
    mutationFn: async ({ groupId, sourceIds }: { groupId: string; sourceIds: string[] }) => {
      const result = await newsletterSourceGroupService.removeSourcesFromGroup(groupId, sourceIds);
      if (!result.success) {
        throw new Error(result.error || 'Failed to remove sources from group');
      }
      return sourceIds;
    },
    onSuccess: (_, variables) => {
      batchInvalidate([
        { queryKey: ['newsletterSourceGroups'] },
        { queryKey: ['newsletterSourceGroup', variables.groupId] },
      ]);
    },
  });
 
  return {
    groups,
    createGroup,
    updateGroup,
    deleteGroup,
    getGroup,
    addSourcesToGroup,
    removeSourcesFromGroup,
    ...query,
  } as const;
};