All files / src/pages/inventory/dialogs/DeleteItemDialog useDeleteItemDialog.ts

100% Statements 81/81
100% Branches 2/2
100% Functions 1/1
100% Lines 81/81

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 821x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x  
/**
 * useDeleteItemDialog - Orchestrator hook for delete workflow
 *
 * Composition: Coordinates state, queries, and handlers into single hook
 * Purpose: Provides backwards-compatible interface for DeleteItemDialog components
 * Pattern: Thin wrapper that delegates to specialized sub-hooks
 */
 
import { useDeleteItemState } from './useDeleteItemState';
import { useDeleteItemQueries } from './useDeleteItemQueries';
import { useDeleteItemHandlers } from './useDeleteItemHandlers';
import type { UseDeleteItemDialogReturn } from './DeleteItemDialog.types';
 
/**
 * Main delete dialog hook - composes state, queries, and handlers
 *
 * @param dialogOpen - Dialog visibility (enables supplier query)
 * @param onClose - Callback when dialog closes
 * @param onItemDeleted - Callback after successful deletion
 * @param readOnly - Demo mode: allows flow but prevents actual deletion
 * @returns Complete state, queries, and handlers for DeleteItemDialog
 */
export function useDeleteItemDialog(
  dialogOpen: boolean,
  onClose: () => void,
  onItemDeleted: () => void,
  readOnly: boolean = false
): UseDeleteItemDialogReturn {
  // Step 1: Manage all selection and dialog state
  const state = useDeleteItemState();
 
  // Step 2: Coordinate data queries based on selection state
  const queries = useDeleteItemQueries(
    dialogOpen,
    state.selectedSupplier,
    state.itemQuery,
    state.selectedItem?.id
  );
 
  // Step 3: Bind event handlers with state and queries
  const handlers = useDeleteItemHandlers(
    state,
    queries,
    onClose,
    onItemDeleted,
    readOnly
  );
 
  // Compose: return backwards-compatible shape for DeleteItemDialog components
  return {
    // Selection state - from useDeleteItemState
    selectedSupplier: state.selectedSupplier,
    selectedItem: state.selectedItem,
    itemQuery: state.itemQuery,
    deletionReason: state.deletionReason,
 
    // Dialog state - from useDeleteItemState
    formError: state.formError,
    showConfirmation: state.showConfirmation,
    isSubmitting: state.isSubmitting,
 
    // Queries - from useDeleteItemQueries
    suppliersQuery: queries.suppliersQuery,
    itemsQuery: queries.itemsQuery,
    itemDetailsQuery: queries.itemDetailsQuery,
 
    // State setters - from useDeleteItemState
    setSelectedSupplier: state.setSelectedSupplier,
    setSelectedItem: state.setSelectedItem,
    setItemQuery: state.setItemQuery,
    setFormError: state.setFormError,
    setShowConfirmation: state.setShowConfirmation,
    setDeletionReason: state.setDeletionReason,
 
    // Handlers - from useDeleteItemHandlers
    handleClose: handlers.handleClose,
    handleCancelConfirmation: handlers.handleCancelConfirmation,
    onSubmit: handlers.onSubmit,
    onConfirmedDelete: handlers.onConfirmedDelete,
  };
}