All files / src/pages/suppliers/dialogs/EditSupplierDialog EditSupplierConfirmation.tsx

100% Statements 187/187
43.75% Branches 7/16
100% Functions 1/1
100% Lines 187/187

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 1881x 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 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 4x 4x 4x 4x 4x 4x 4x 4x 4x 5x 5x 5x 4x 4x 4x 4x 4x 4x 4x 4x 5x 5x 5x 4x 4x 4x 4x 4x 4x 4x 4x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 1x 4x 4x 5x 5x 5x 5x 5x 5x  
/**
 * @file EditSupplierConfirmation.tsx
 * @module dialogs/EditSupplierDialog/EditSupplierConfirmation
 *
 * @summary
 * Confirmation dialog for supplier changes.
 * Displays changes summary before applying updates.
 *
 * @enterprise
 * - Pure presentation component
 * - Shows supplier info (name is read-only)
 * - Displays what will change (before → after)
 * - Requires user confirmation before proceeding
 */
 
import * as React from 'react';
import {
  Dialog,
  DialogTitle,
  DialogContent,
  DialogActions,
  Button,
  Box,
  Typography,
  Alert,
  Divider,
  CircularProgress,
} from '@mui/material';
import { useTranslation } from 'react-i18next';
import type { EditSupplierForm } from '../../../../api/suppliers';
import type { SupplierRow } from '../../../../api/suppliers/types';
 
/**
 * Props for EditSupplierConfirmation component.
 *
 * @interface EditSupplierConfirmationProps
 */
interface EditSupplierConfirmationProps {
  /** Whether confirmation dialog is open */
  open: boolean;
  /** Selected supplier to update */
  supplier: SupplierRow | null;
  /** Pending changes to apply */
  changes: EditSupplierForm | null;
  /** Form error message, if any */
  formError: string;
  /** Whether update is in progress */
  isSubmitting: boolean;
  /** Called when user confirms changes */
  onConfirm: () => Promise<void>;
  /** Called when user cancels */
  onCancel: () => void;
}
 
/**
 * Confirmation dialog for supplier changes.
 *
 * Displays supplier details and a summary of changes
 * that will be applied. Requires user confirmation.
 *
 * @component
 * @param props - Component props
 * @returns JSX element with confirmation dialog
 *
 * @example
 * ```tsx
 * <EditSupplierConfirmation
 *   open={showConfirmation}
 *   supplier={selectedSupplier}
 *   changes={pendingChanges}
 *   formError={error}
 *   isSubmitting={isSubmitting}
 *   onConfirm={handleConfirm}
 *   onCancel={handleCancel}
 * />
 * ```
 */
export const EditSupplierConfirmation: React.FC<EditSupplierConfirmationProps> = ({
  open,
  supplier,
  changes,
  formError,
  isSubmitting,
  onConfirm,
  onCancel,
}) => {
  const { t } = useTranslation(['suppliers', 'common']);
 
  return (
    <Dialog open={open} onClose={onCancel} fullWidth maxWidth="sm">
      <DialogTitle>{t('suppliers:dialogs.confirmChanges', 'Confirm Changes')}</DialogTitle>
 
      <DialogContent dividers>
        <Box sx={{ display: 'grid', gap: 2 }}>
          {formError && (
            <Alert severity="error" onClose={onCancel}>
              {formError}
            </Alert>
          )}
 
          <Alert severity="warning">
            {t('suppliers:confirmations.changesCannotBeReversed', 'These changes cannot be reversed.')}
          </Alert>
 
          {supplier && (
            <Box>
              {/* Supplier Name (Read-only for display) */}
              <Box sx={{ mb: 2, p: 1.5, bgcolor: 'action.hover', borderRadius: 1 }}>
                <Typography variant="body2" color="text.secondary">
                  {t('suppliers:table.name', 'Supplier Name')}
                </Typography>
                <Typography variant="body1" sx={{ fontWeight: 600, mt: 0.5 }}>
                  {supplier.name}
                </Typography>
                <Typography variant="caption" color="text.secondary" sx={{ mt: 1, display: 'block' }}>
                  {t('suppliers:hints.nameCannotBeChanged', '(Cannot be changed)')}
                </Typography>
              </Box>
 
              <Divider sx={{ my: 2 }} />
 
              {/* Changes Summary */}
              <Typography variant="subtitle2" sx={{ fontWeight: 600, mb: 1.5 }}>
                {t('suppliers:confirmations.changes', 'Changes')}
              </Typography>
 
              {changes?.contactName !== (supplier.contactName || '') && (
                <Box sx={{ mb: 1.5, p: 1, bgcolor: 'info.lighter', borderRadius: 1 }}>
                  <Typography variant="body2" sx={{ fontWeight: 600 }}>
                    {t('suppliers:table.contactName', 'Contact Name')}
                  </Typography>
                  <Typography variant="caption" color="text.secondary">
                    {supplier.contactName || '(empty)'} →{' '}
                    {changes?.contactName || '(empty)'}
                  </Typography>
                </Box>
              )}
 
              {changes?.phone !== (supplier.phone || '') && (
                <Box sx={{ mb: 1.5, p: 1, bgcolor: 'info.lighter', borderRadius: 1 }}>
                  <Typography variant="body2" sx={{ fontWeight: 600 }}>
                    {t('suppliers:table.phone', 'Phone')}
                  </Typography>
                  <Typography variant="caption" color="text.secondary">
                    {supplier.phone || '(empty)'} → {changes?.phone || '(empty)'}
                  </Typography>
                </Box>
              )}
 
              {changes?.email !== (supplier.email || '') && (
                <Box sx={{ mb: 1.5, p: 1, bgcolor: 'info.lighter', borderRadius: 1 }}>
                  <Typography variant="body2" sx={{ fontWeight: 600 }}>
                    {t('suppliers:table.email', 'Email')}
                  </Typography>
                  <Typography variant="caption" color="text.secondary">
                    {supplier.email || '(empty)'} → {changes?.email || '(empty)'}
                  </Typography>
                </Box>
              )}
            </Box>
          )}
        </Box>
      </DialogContent>
 
      <DialogActions sx={{ gap: 1 }}>
        <Button onClick={onCancel} disabled={isSubmitting}>
          {t('common:actions.no', 'No')}
        </Button>
        <Button
          onClick={onConfirm}
          variant="contained"
          color="primary"
          disabled={isSubmitting}
        >
          {isSubmitting ? (
            <>
              <CircularProgress size={16} sx={{ mr: 1 }} />
              {t('common:saving', 'Saving...')}
            </>
          ) : (
            t('common:actions.yes', 'Yes')
          )}
        </Button>
      </DialogActions>
    </Dialog>
  );
};