All files / src/hooks/useUserAccount useUserAccount.ts

80.64% Statements 50/62
100% Branches 0/0
75% Functions 27/36
80.64% Lines 50/62

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 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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197                    2x 9x 7x 5x 7x           2x 8x   4x                 2x 6x     3x 2x                   2x 4x     2x 2x                   2x 6x   3x                 2x 9x   9x 5x     4x   4x               2x 7x   7x   4x     4x               2x 7x   7x 4x     4x               2x 9x   9x   5x     5x   5x               2x 9x   9x 5x     4x   4x                 2x                           2x                             2x                  
/**
 * React Query Hooks for User Account Management
 * Story 2.6: User Account Management Frontend
 */
 
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import type { User, UserPreferences, UserSettings } from '@/types/userAccount.types';
import * as userAccountApi from '@/services/api/userAccountApi';
 
// Query keys
export const userAccountKeys = {
  profile: (userId: string) => ['user-profile', userId] as const,
  preferences: (userId: string) => ['user-preferences', userId] as const,
  settings: (userId: string) => ['user-settings', userId] as const,
  activity: (userId: string, limit?: number) => ['user-activity', userId, { limit }] as const,
};
 
/**
 * Hook to fetch user profile with consolidated data
 */
export const useUserProfile = (userId: string) => {
  return useQuery({
    queryKey: userAccountKeys.profile(userId),
    queryFn: () => userAccountApi.getUserProfile(),
    staleTime: 1000 * 60 * 5, // 5 minutes
    gcTime: 1000 * 60 * 30, // 30 minutes (formerly cacheTime)
  });
};
 
/**
 * Hook to fetch user preferences
 */
export const useUserPreferences = (userId: string) => {
  return useQuery({
    queryKey: userAccountKeys.preferences(userId),
    queryFn: async () => {
      const data = await userAccountApi.getUserProfile(['preferences']);
      return data.preferences;
    },
    staleTime: 1000 * 60 * 10, // 10 minutes
    gcTime: 1000 * 60 * 60, // 1 hour
  });
};
 
/**
 * Hook to fetch user settings
 */
export const useUserSettings = (userId: string) => {
  return useQuery({
    queryKey: userAccountKeys.settings(userId),
    queryFn: async () => {
      const data = await userAccountApi.getUserProfile(['settings']);
      return data.settings;
    },
    staleTime: 1000 * 60 * 10, // 10 minutes
    gcTime: 1000 * 60 * 60, // 1 hour
  });
};
 
/**
 * Hook to fetch user activity
 */
export const useUserActivity = (userId: string, limit?: number) => {
  return useQuery({
    queryKey: userAccountKeys.activity(userId, limit),
    queryFn: () => userAccountApi.getUserActivity(limit),
    staleTime: 1000 * 60, // 1 minute
    gcTime: 1000 * 60 * 10, // 10 minutes
  });
};
 
/**
 * Hook to update user profile
 */
export const useUpdateUserProfile = () => {
  const queryClient = useQueryClient();
 
  return useMutation({
    mutationFn: (updates: Partial<User>) => userAccountApi.updateUserProfile(updates),
    onSuccess: () => {
      // Invalidate and refetch user profile
      queryClient.invalidateQueries({ queryKey: ['user-profile'] });
      // Also invalidate user list queries so updates are visible in admin screens
      queryClient.invalidateQueries({ queryKey: ['users'] });
    },
  });
};
 
/**
 * Hook to update user preferences
 */
export const useUpdateUserPreferences = () => {
  const queryClient = useQueryClient();
 
  return useMutation({
    mutationFn: (preferences: Partial<UserPreferences>) =>
      userAccountApi.updateUserPreferences(preferences),
    onSuccess: () => {
      // Invalidate and refetch preferences
      queryClient.invalidateQueries({ queryKey: ['user-preferences'] });
    },
  });
};
 
/**
 * Hook to update user settings
 */
export const useUpdateUserSettings = () => {
  const queryClient = useQueryClient();
 
  return useMutation({
    mutationFn: (settings: Partial<UserSettings>) => userAccountApi.updateUserSettings(settings),
    onSuccess: () => {
      // Invalidate and refetch settings
      queryClient.invalidateQueries({ queryKey: ['user-settings'] });
    },
  });
};
 
/**
 * Hook to upload profile picture
 */
export const useUploadProfilePicture = () => {
  const queryClient = useQueryClient();
 
  return useMutation({
    mutationFn: ({ file, onProgress }: { file: File; onProgress?: (progress: number) => void }) =>
      userAccountApi.uploadProfilePicture(file, onProgress),
    onSuccess: () => {
      // Invalidate and refetch user profile to get new picture URL
      queryClient.invalidateQueries({ queryKey: ['user-profile'] });
      // Also invalidate user list queries so updates are visible in admin screens
      queryClient.invalidateQueries({ queryKey: ['users'] });
    },
  });
};
 
/**
 * Hook to remove profile picture
 */
export const useRemoveProfilePicture = () => {
  const queryClient = useQueryClient();
 
  return useMutation({
    mutationFn: () => userAccountApi.removeProfilePicture(),
    onSuccess: () => {
      // Invalidate and refetch user profile
      queryClient.invalidateQueries({ queryKey: ['user-profile'] });
      // Also invalidate user list queries so updates are visible in admin screens
      queryClient.invalidateQueries({ queryKey: ['users'] });
    },
  });
};
 
/**
 * Story 10.32 — register an additional email on the current user's profile.
 * Invalidates the user-profile cache so the UI shows the new row immediately.
 */
export const useAddAdditionalEmail = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (payload: userAccountApi.AddAdditionalEmailPayload) =>
      userAccountApi.addAdditionalEmail(payload),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['user-profile'] });
    },
  });
};
 
/**
 * Story 10.32 — remove an additional email from the current user's profile.
 */
export const useDeleteAdditionalEmail = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (email: string) => userAccountApi.deleteAdditionalEmail(email),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['user-profile'] });
    },
  });
};
 
/**
 * Additional-email verification (v2) — resend the verification email for one of
 * the caller's own unverified additional emails. Invalidates the user-profile
 * cache so any status changes are reflected.
 */
export const useResendVerification = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (email: string) => userAccountApi.resendAdditionalEmailVerification(email),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['user-profile'] });
    },
  });
};