All files / src/hooks useDataExtraction.ts

100% Statements 10/10
83.33% Branches 5/6
100% Functions 2/2
100% Lines 9/9

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                                                                    2x       82x   9x     7x   7x 4x 3x         7x         82x        
// src/hooks/useDataExtraction.ts
import { useCallback } from 'react';
import type { Flyer } from '../types';
 
type ExtractionType = 'store_name' | 'dates';
 
interface UseDataExtractionOptions {
  selectedFlyer: Flyer | null;
  onFlyerUpdate: (flyer: Flyer) => void;
}
 
interface UseDataExtractionReturn {
  handleDataExtracted: (type: ExtractionType, value: string) => void;
}
 
/**
 * A custom hook to handle data extraction from the correction tool.
 * Updates the selected flyer with extracted store name or date information.
 *
 * Note: This currently only updates local state for immediate visual feedback.
 * A production implementation should also persist changes to the database.
 *
 * @param options.selectedFlyer - The currently selected flyer
 * @param options.onFlyerUpdate - Callback to update the flyer state
 * @returns Object with handleDataExtracted callback
 *
 * @example
 * ```tsx
 * const { handleDataExtracted } = useDataExtraction({
 *   selectedFlyer,
 *   onFlyerUpdate: setSelectedFlyer,
 * });
 * ```
 */
export const useDataExtraction = ({
  selectedFlyer,
  onFlyerUpdate,
}: UseDataExtractionOptions): UseDataExtractionReturn => {
  const handleDataExtracted = useCallback(
    (type: ExtractionType, value: string) => {
      if (!selectedFlyer) return;
 
      // Create an updated copy of the flyer
      const updatedFlyer = { ...selectedFlyer };
 
      if (type === 'store_name') {
        updatedFlyer.store = { ...updatedFlyer.store!, name: value };
      E} else if (type === 'dates') {
        // A more robust solution would parse the date string properly.
        // For now, this is a placeholder for future date extraction logic.
      }
 
      onFlyerUpdate(updatedFlyer);
    },
    [selectedFlyer, onFlyerUpdate],
  );
 
  return {
    handleDataExtracted,
  };
};