All files / src/pages/suppliers/dialogs/DeleteSupplierDialog DeleteSupplierDialog.tsx

97.69% Statements 127/130
100% Branches 8/8
100% Functions 2/2
97.69% Lines 127/130

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 1311x 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 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x       7x 7x 7x 7x 7x 7x 2x 1x 1x 1x 2x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 4x 4x 4x 4x 4x 4x 4x 4x 4x 7x 7x 7x 7x 3x 3x 3x 3x 3x 3x 3x 7x 7x 7x 7x 1x 1x  
/**
 * @file DeleteSupplierDialog.tsx
 * @module dialogs/DeleteSupplierDialog/DeleteSupplierDialog
 *
 * @summary
 * Dialog container for deleting suppliers.
 * Composes form logic hook and view components for two-step workflow.
 *
 * @enterprise
 * - Separation of concerns: Dialog wrapper, form logic, view components
 * - Two-step workflow: search → confirm deletion
 * - Orchestrates lifecycle (open/close/reset)
 * - Clean, readable component hierarchy
 */
 
import * as React from 'react';
import { Dialog } from '@mui/material';
import { useHelp } from '../../../../hooks/useHelp';
import { useToast } from '../../../../context/toast';
import { useTranslation } from 'react-i18next';
import { useDeleteSupplierForm } from './useDeleteSupplierForm';
import { DeleteSupplierSearch } from './DeleteSupplierSearch';
import { DeleteSupplierConfirmation } from './DeleteSupplierConfirmation';
import type { DeleteSupplierDialogProps } from './DeleteSupplierDialog.types';
 
/**
 * Dialog for deleting a supplier.
 *
 * @remarks
 * Two-step workflow:
 * 1. User searches for supplier to delete
 * 2. User confirms deletion with warning about irreversibility
 *
 * Business rules:
 * - Only ADMIN role can delete (enforced by backend)
 * - Cannot delete if supplier has linked items (409 Conflict)
 * - Backend returns 204 No Content on success
 *
 * @component
 * @param props - Dialog props
 * @returns JSX element with dialog and workflow
 *
 * @example
 * ```tsx
 * <DeleteSupplierDialog
 *   open={isOpen}
 *   onClose={() => setIsOpen(false)}
 *   onSupplierDeleted={handleSupplierDeleted}
 * />
 * ```
 */
export const DeleteSupplierDialog: React.FC<DeleteSupplierDialogProps> = ({
  open,
  onClose,
  onSupplierDeleted,
}) => {
  const { t } = useTranslation(['suppliers']);
  const toast = useToast();
  const { openHelp } = useHelp();
 
  // Form logic hook
  const form = useDeleteSupplierForm(() => {
    toast(t('suppliers:actions.deleteSuccess', 'Supplier removed from database'), 'success');
    onSupplierDeleted();
    onClose();
  });
 
  /**
   * Prevent dialog close during deletion.
   */
  const handleDialogClose = React.useCallback(() => {
    if (!form.isDeleting) {
      form.resetForm();
      onClose();
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [form.isDeleting, form.resetForm, onClose]);
 
  /**
   * Handle cancel button (go back to search step).
   */
  const handleCancelConfirmation = React.useCallback(() => {
    form.setShowConfirmation(false);
    form.setSelectedSupplier(null);
    form.setError(null);
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [form.setShowConfirmation, form.setSelectedSupplier, form.setError]);
 
  return (
    <Dialog
      open={open}
      onClose={handleDialogClose}
      maxWidth="sm"
      fullWidth
      slotProps={{
        paper: {
          sx: {
            borderRadius: 1,
          },
        },
      }}
    >
      {/* Search Step */}
      {!form.showConfirmation && (
        <DeleteSupplierSearch
          searchQuery={form.searchQuery}
          onSearchQueryChange={form.handleSearchQueryChange}
          searchResults={form.searchResults}
          searchLoading={form.searchLoading}
          onSelectSupplier={form.handleSelectSupplier}
          onCancel={handleDialogClose}
          onHelp={() => openHelp('suppliers.delete')}
        />
      )}
 
      {/* Confirmation Step */}
      {form.showConfirmation && form.selectedSupplier && (
        <DeleteSupplierConfirmation
          supplier={form.selectedSupplier}
          error={form.error}
          isDeleting={form.isDeleting}
          onConfirm={form.handleConfirmDelete}
          onCancel={handleCancelConfirmation}
        />
      )}
    </Dialog>
  );
};
 
export default DeleteSupplierDialog;