All files / src/pages/inventory/dialogs/DeleteItemDialog DeleteFormFields.tsx

74.38% Statements 212/285
50% Branches 3/6
50% Functions 3/6
74.38% Lines 212/285

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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 2861x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                         1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 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  
/**
 * DeleteFormFields - Isolated field components for delete workflow
 * 
 * @module dialogs/DeleteItemDialog/DeleteFormFields
 * @description
 * Individual form field components for the 4-step delete item workflow.
 * Each field is isolated for easier testing, reuse, and composition.
 * 
 * @enterprise
 * - Single Responsibility: each component manages one field
 * - Testable: can be tested independently with mock state
 * - Reusable: can be composed into custom forms
 * - Type-safe: all props are strictly typed
 * - i18n-ready: all labels and messages are translated
 * 
 * @components
 * - SupplierSelectField: Step 1 - Choose supplier from dropdown
 * - ItemSelectField: Step 2 - Search and autocomplete select item
 * - DeletionReasonField: Step 3 - Choose predefined deletion reason
 * - ItemInfoDisplay: Step 4 - Show item details for confirmation
 */
 
import {
  Box,
  FormControl,
  InputLabel,
  Select,
  MenuItem,
  Typography,
  CircularProgress,
  Alert,
  Autocomplete,
  TextField,
} from '@mui/material';
import { useTranslation } from 'react-i18next';
import type { UseDeleteItemDialogReturn } from './DeleteItemDialog.types';
 
/**
 * SupplierSelectField - Step 1: Select supplier from dropdown
 * 
 * @param state - Complete dialog state from useDeleteItemDialog hook
 * 
 * @behavior
 * - Shows loading spinner while fetching suppliers from API
 * - Displays dropdown with all available suppliers
 * - On selection: updates state.selectedSupplier (triggers item search reset)
 * - On change: clears previously selected item and search query
 * 
 * @visibility
 * - Always visible in form view
 * - First step that must be completed
 * - Enables Step 2 (item selection) when completed
 * 
 * @performance
 * - Suppliers loaded once via useSuppliersQuery (5-minute cache)
 */
export function SupplierSelectField({ state }: { state: UseDeleteItemDialogReturn }) {
  const { t } = useTranslation(['common', 'inventory']);
  
  // Show loading state while suppliers are being fetched from API
  if (state.suppliersQuery.isLoading) {
    return (
      <Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
        <CircularProgress size={20} />
        <Typography variant="body2" color="text.secondary">
          {t('common:loading', 'Loading...')}
        </Typography>
      </Box>
    );
  }
 
  return (
    <FormControl fullWidth>
      <InputLabel id="supplier-select-label">
        {t('inventory:table.supplier', 'Supplier')}
      </InputLabel>
      <Select
        labelId="supplier-select-label"
        value={state.selectedSupplier?.id ?? ''}
        onChange={(e) => {
          // Find supplier by ID and update state
          // Handles both string and numeric IDs via String comparison
          const supplier = state.suppliersQuery.data?.find(
            (s) => String(s.id) === String(e.target.value)
          );
          state.setSelectedSupplier(supplier ?? null);
        }}
        label={t('inventory:table.supplier', 'Supplier')}
      >
        {/* Render all suppliers as menu items for selection */}
        {state.suppliersQuery.data?.map((supplier) => (
          <MenuItem key={supplier.id} value={supplier.id}>
            {supplier.label}
          </MenuItem>
        ))}
      </Select>
    </FormControl>
  );
}
 
/**
 * ItemSelectField - Step 2: Search and select item via autocomplete
 * 
 * @param state - Complete dialog state from useDeleteItemDialog hook
 * 
 * @behavior
 * - Disabled until supplier is selected (shows info alert)
 * - Shows loading spinner while searching items
 * - Autocomplete with debounced search (2+ characters required)
 * - On selection: updates state.selectedItem, clears search query
 * - On input change: updates state.itemQuery (triggers search via API)
 * 
 * @visibility
 * - Visible only after Step 1 (supplier) is completed
 * - Conditional render: {state.selectedSupplier && (...)}
 * - Enables Step 3 (reason) when item is selected
 * 
 * @performance
 * - Debounced search (350ms) via useDebounced hook
 * - Minimal API calls: only when 2+ chars and supplier selected
 * - Uses Autocomplete internal value management for efficiency
 */
export function ItemSelectField({ state }: { state: UseDeleteItemDialogReturn }) {
  const { t } = useTranslation(['common', 'inventory']);
  
  // Guard: cannot search items without selecting supplier first
  if (!state.selectedSupplier) {
    return (
      <Alert severity="info">
        {t('inventory:search.selectSupplierFirst', 'Select a supplier to enable search.')}
      </Alert>
    );
  }
  
  // Show loading state while API searches items matching query
  if (state.itemsQuery.isLoading) {
    return (
      <Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
        <CircularProgress size={20} />
        <Typography variant="body2" color="text.secondary">
          {t('common:loading', 'Loading...')}
        </Typography>
      </Box>
    );
  }

  return (
    <Autocomplete
      key={state.selectedSupplier?.id}
      disabled={!state.selectedSupplier}
      options={state.itemsQuery.data ?? []}
      getOptionLabel={(option) => option.name}
      value={state.selectedItem}
      onChange={(_e, value) => {
        state.setSelectedItem(value);
        // Clear search query after selection for clean slate
        state.setItemQuery('');
      }}
      inputValue={state.itemQuery}
      onInputChange={(_e, value) => state.setItemQuery(value)}
      noOptionsText={
        state.itemQuery.length < 2
          ? t('inventory:search.typeToSearch', 'Type at least 2 characters to search')
          : t('inventory:search.noItemsFound', 'No items found for this search.')
      }
      renderInput={(params) => (
        <TextField
          {...params}
          label={t('inventory:item', 'Item')}
          placeholder={t('inventory:search.typeToSearchItems', 'Type to search items...')}
        />
      )}
    />
  );
}
 
/**
 * DeletionReasonField - Step 3: Select predefined deletion reason
 * 
 * @param state - Complete dialog state from useDeleteItemDialog hook
 * 
 * @behavior
 * - Dropdown menu with 6 predefined business reasons
 * - On change: updates state.deletionReason
 * - Required for audit trail and inventory accounting
 * 
 * @visibility
 * - Visible only after Step 2 (item) is completed
 * - Conditional render: {state.selectedItem && (...)}
 * - Enables Step 4 (preview) when reason selected
 * 
 */
export function DeletionReasonField({ state }: { state: UseDeleteItemDialogReturn }) {
  const { t } = useTranslation(['inventory']);
 
  return (
    <FormControl fullWidth>
      <InputLabel>
        {t('inventory:deleteFlow.deletionReasonLabel', 'Deletion Reason')}
      </InputLabel>
      <Select
        label={t('inventory:deleteFlow.deletionReasonLabel', 'Deletion Reason')}
        value={state.deletionReason}
        onChange={(e) => state.setDeletionReason(e.target.value)}
      >
        {/* Predefined deletion reasons with business descriptions */}
        <MenuItem value="SCRAPPED">
          {t('inventory:reasons.reasonScrapped', 'Scrapped - Quality control removal')}
        </MenuItem>
        <MenuItem value="DESTROYED">
          {t('inventory:reasons.reasonDestroyed', 'Destroyed - Catastrophic loss')}
        </MenuItem>
        <MenuItem value="DAMAGED">
          {t('inventory:reasons.reasonDamaged', 'Damaged - Quality hold')}
        </MenuItem>
        <MenuItem value="EXPIRED">
          {t('inventory:reasons.reasonExpired', 'Expired - Expiration date breach')}
        </MenuItem>
        <MenuItem value="LOST">
          {t('inventory:reasons.reasonLost', 'Lost - Inventory shrinkage')}
        </MenuItem>
        <MenuItem value="RETURNED_TO_SUPPLIER">
          {t('inventory:reasons.reasonReturnedToSupplier', 'Returned to Supplier - Defective merchandise')}
        </MenuItem>
      </Select>
    </FormControl>
  );
}
 
/**
 * ItemInfoDisplay - Step 4: Show item details for final confirmation
 * 
 * @param state - Complete dialog state from useDeleteItemDialog hook
 * @returns Item preview card or null if no item selected/loaded
 * 
 * @behavior
 * - Only renders when item is selected AND details are loaded
 * - Shows: item name and on-hand quantity
 * - Non-interactive: displays information for user verification
 * - Helps user confirm they're deleting the correct item
 * 
 * @visibility
 * - Visible only after Step 2 (item) is completed
 * - Only renders if itemDetailsQuery has data
 * - Conditional render: {state.selectedItem && state.itemDetailsQuery.data && (...)}
 * 
 * @styling
 * - Light background (action.hover) to visually separate from form
 * - Padding and border radius for readable presentation
 * - Typography hierarchy: label → value for clarity
 */
export function ItemInfoDisplay({ state }: { state: UseDeleteItemDialogReturn }) {
  const { t } = useTranslation(['inventory']);
 
  // Guard: only render if item is selected and details are loaded
  if (!state.selectedItem || !state.itemDetailsQuery.data) {
    return null;
  }
 
  return (
    <Box
      sx={{
        p: 1.5,
        bgcolor: 'action.hover',
        borderRadius: 1,
      }}
    >
      {/* Item name section */}
      <Typography variant="body2" color="text.secondary">
        {t('inventory:table.name', 'Name')}
      </Typography>
      <Typography variant="body1" sx={{ fontWeight: 600 }}>
        {state.itemDetailsQuery.data.name}
      </Typography>
 
      {/* Item on-hand quantity section */}
      <Typography variant="body2" color="text.secondary" sx={{ mt: 1 }}>
        {t('inventory:table.onHand', 'On-hand')}
      </Typography>
      <Typography variant="body1" sx={{ fontWeight: 600 }}>
        {state.itemDetailsQuery.data.onHand}
      </Typography>
    </Box>
  );
}