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

100% Statements 158/158
100% Branches 9/9
100% Functions 4/4
100% Lines 158/158

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 1591x 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 8x 8x 8x 8x 8x 8x 8x 8x 2x 2x 2x 2x 2x 2x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x  
/**
 * @file EditSupplierInfoStep.tsx
 * @module dialogs/EditSupplierDialog/EditSupplierInfoStep
 *
 * @summary
 * Step 2 component for editing supplier contact information.
 * Displays form fields for contact info (name is read-only).
 *
 * @enterprise
 * - Pure presentation component
 * - Supplier name is read-only (immutable constraint)
 * - Editable fields: contactName, phone, email
 * - React Hook Form integration
 */
 
import * as React from 'react';
import { Controller, type Control, type FieldErrors } from 'react-hook-form';
import {
  Box,
  TextField,
  Typography,
  Alert,
  Divider,
} from '@mui/material';
import { useTranslation } from 'react-i18next';
import type { EditSupplierForm } from '../../../../api/suppliers';
import type { SupplierRow } from '../../../../api/suppliers/types';
 
/**
 * Props for EditSupplierInfoStep component.
 *
 * @interface EditSupplierInfoStepProps
 */
interface EditSupplierInfoStepProps {
  /** Currently selected supplier */
  selectedSupplier: SupplierRow | null;
  /** React Hook Form control */
  control: Control<EditSupplierForm>;
  /** Form field errors */
  errors: FieldErrors<EditSupplierForm>;
  /** Whether form is submitting */
  isSubmitting: boolean;
}
 
/**
 * Step 2: Edit supplier contact information.
 *
 * Displays form fields for editable supplier info.
 * Supplier name is displayed but read-only.
 *
 * @component
 * @param props - Component props
 * @returns JSX element with form fields, or info message if no supplier selected
 *
 * @example
 * ```tsx
 * <EditSupplierInfoStep
 *   selectedSupplier={supplier}
 *   control={control}
 *   errors={errors}
 *   isSubmitting={isSubmitting}
 * />
 * ```
 */
export const EditSupplierInfoStep: React.FC<EditSupplierInfoStepProps> = ({
  selectedSupplier,
  control,
  errors,
  isSubmitting,
}) => {
  const { t } = useTranslation(['suppliers']);
 
  if (!selectedSupplier) {
    return (
      <Alert severity="info">
        {t('suppliers:search.selectSupplierFirst', 'Search and select a supplier to enable editing.')}
      </Alert>
    );
  }
 
  return (
    <Box>
      <Typography variant="subtitle2" sx={{ fontWeight: 700, mb: 1 }}>
        {t('suppliers:steps.editInfo', 'Step 2: Edit Contact Information')}
      </Typography>
 
      {/* Supplier Name Display (Read-only) */}
      <Box sx={{ p: 1.5, bgcolor: 'action.hover', borderRadius: 1, mb: 2 }}>
        <Typography variant="body2" color="text.secondary">
          {t('suppliers:table.name', 'Supplier Name')}
        </Typography>
        <Typography variant="body1" sx={{ fontWeight: 600, mt: 0.5 }}>
          {selectedSupplier.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 }} />
 
      {/* Contact Name */}
      <Controller
        name="contactName"
        control={control}
        render={({ field }) => (
          <TextField
            {...field}
            fullWidth
            label={t('suppliers:table.contactName', 'Contact Name')}
            placeholder={t('suppliers:form.contactNamePlaceholder', 'Enter contact name')}
            error={!!errors.contactName}
            helperText={errors.contactName?.message}
            disabled={isSubmitting}
            margin="normal"
          />
        )}
      />
 
      {/* Phone */}
      <Controller
        name="phone"
        control={control}
        render={({ field }) => (
          <TextField
            {...field}
            fullWidth
            label={t('suppliers:table.phone', 'Phone')}
            placeholder={t('suppliers:form.phonePlaceholder', 'Enter phone number')}
            error={!!errors.phone}
            helperText={errors.phone?.message}
            disabled={isSubmitting}
            margin="normal"
          />
        )}
      />
 
      {/* Email */}
      <Controller
        name="email"
        control={control}
        render={({ field }) => (
          <TextField
            {...field}
            fullWidth
            type="email"
            label={t('suppliers:table.email', 'Email')}
            placeholder={t('suppliers:form.emailPlaceholder', 'Enter email address')}
            error={!!errors.email}
            helperText={errors.email?.message}
            disabled={isSubmitting}
            margin="normal"
          />
        )}
      />
    </Box>
  );
};