All files / src/providers UserDataProvider.tsx

100% Statements 8/8
100% Branches 8/8
100% Functions 2/2
100% Lines 8/8

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                                  38x 458x 458x           458x           458x   458x 458x                 458x    
// src/providers/UserDataProvider.tsx
import React, { useMemo, ReactNode } from 'react';
import { UserDataContext } from '../contexts/UserDataContext';
import { useAuth } from '../hooks/useAuth';
import { useWatchedItemsQuery } from '../hooks/queries/useWatchedItemsQuery';
import { useShoppingListsQuery } from '../hooks/queries/useShoppingListsQuery';
 
/**
 * Provider for user-specific data using TanStack Query (ADR-0005).
 *
 * This provider uses TanStack Query for automatic caching, refetching, and state management.
 * Data is automatically cleared when the user logs out (query is disabled),
 * and refetched when a new user logs in.
 *
 * Phase 4 Update: Removed deprecated setWatchedItems and setShoppingLists setters.
 * Use mutation hooks directly from src/hooks/mutations instead.
 */
export const UserDataProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
  const { userProfile } = useAuth();
  const isEnabled = !!userProfile;
 
  const {
    data: watchedItems = [],
    isLoading: isLoadingWatched,
    error: watchedError,
  } = useWatchedItemsQuery(isEnabled);
 
  const {
    data: shoppingLists = [],
    isLoading: isLoadingLists,
    error: listsError,
  } = useShoppingListsQuery(isEnabled);
 
  const value = useMemo(
    () => ({
      watchedItems,
      shoppingLists,
      isLoading: isEnabled && (isLoadingWatched || isLoadingLists),
      error: watchedError?.message || listsError?.message || null,
    }),
    [watchedItems, shoppingLists, isEnabled, isLoadingWatched, isLoadingLists, watchedError, listsError]
  );
 
  return <UserDataContext.Provider value={value}>{children}</UserDataContext.Provider>;
};