Merge branch 'main' into codex/refactor-productrow-layout-g62j3n
This commit is contained in:
7 files changed
+468
-49
No files matched your search
@@ -0,0 +1,35 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { ProductRow } from './ProductRow';
|
||||
import { Product } from '../models/Product';
|
||||
|
||||
const product: Product = {
|
||||
id: 'prod-1',
|
||||
name: 'Sparkling Water',
|
||||
category: 'Drinks',
|
||||
unit_type: 'bottle',
|
||||
barcode: '123456',
|
||||
archived: false,
|
||||
created_at: 0,
|
||||
updated_at: 0,
|
||||
};
|
||||
|
||||
const categories = ['Drinks', 'Snacks'];
|
||||
|
||||
describe('ProductRow', () => {
|
||||
it('shows name, category, and barcode without unit text in read-only mode', () => {
|
||||
render(
|
||||
<ProductRow
|
||||
product={product}
|
||||
categories={categories}
|
||||
onSave={vi.fn()}
|
||||
onDelete={vi.fn()}
|
||||
/>,
|
||||
);
|
||||
|
||||
expect(screen.getByText('Sparkling Water')).toBeInTheDocument();
|
||||
expect(screen.getByText('Drinks')).toBeInTheDocument();
|
||||
expect(screen.getByText(/Barcode: 123456/)).toBeInTheDocument();
|
||||
expect(screen.queryByText(/bottle/i)).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -17,7 +17,7 @@ import EditIcon from '@mui/icons-material/Edit';
|
||||
import CheckIcon from '@mui/icons-material/Check';
|
||||
import CloseIcon from '@mui/icons-material/Close';
|
||||
import { ChangeEvent, useEffect, useState } from 'react';
|
||||
import { DEFAULT_UNIT_TYPE, Product } from '../models/Product';
|
||||
import { Product } from '../models/Product';
|
||||
import { BarcodeScannerView } from './BarcodeScannerView';
|
||||
|
||||
interface ProductRowProps {
|
||||
@@ -50,16 +50,16 @@ export const ProductRow = ({ product, categories, onSave, onDelete }: ProductRow
|
||||
const [isEditing, setIsEditing] = useState(false);
|
||||
const [formState, setFormState] = useState<ProductFormState>(() => getInitialFormState(product));
|
||||
const [isScannerOpen, setIsScannerOpen] = useState(false);
|
||||
const [saveError, setSaveError] = useState('');
|
||||
const [fieldErrors, setFieldErrors] = useState<{ name?: string; barcode?: string }>({});
|
||||
|
||||
useEffect(() => {
|
||||
setFormState(getInitialFormState(product));
|
||||
setSaveError('');
|
||||
setFieldErrors({});
|
||||
}, [product]);
|
||||
|
||||
const handleChange = (field: keyof ProductFormState) => (event: ChangeEvent<HTMLInputElement>) => {
|
||||
setFormState((prev) => ({ ...prev, [field]: event.target.value }));
|
||||
setSaveError('');
|
||||
setFieldErrors((prev) => ({ ...prev, [field]: undefined }));
|
||||
};
|
||||
|
||||
const handleSave = async () => {
|
||||
@@ -71,10 +71,14 @@ export const ProductRow = ({ product, categories, onSave, onDelete }: ProductRow
|
||||
barcode: formState.barcode || undefined,
|
||||
});
|
||||
setIsEditing(false);
|
||||
setSaveError('');
|
||||
setFieldErrors({});
|
||||
} catch (error) {
|
||||
if (error instanceof Error && error.name === 'DuplicateNameError') {
|
||||
setFieldErrors({ name: 'A product with this name already exists.' });
|
||||
return;
|
||||
}
|
||||
if (error instanceof Error && error.name === 'DuplicateBarcodeError') {
|
||||
setSaveError('This barcode is already assigned to another product.');
|
||||
setFieldErrors({ barcode: 'This barcode is already assigned to another product.' });
|
||||
return;
|
||||
}
|
||||
throw error;
|
||||
@@ -84,7 +88,7 @@ export const ProductRow = ({ product, categories, onSave, onDelete }: ProductRow
|
||||
const handleCancel = () => {
|
||||
setIsEditing(false);
|
||||
setFormState(getInitialFormState(product));
|
||||
setSaveError('');
|
||||
setFieldErrors({});
|
||||
};
|
||||
|
||||
return (
|
||||
@@ -92,7 +96,14 @@ export const ProductRow = ({ product, categories, onSave, onDelete }: ProductRow
|
||||
<CardContent sx={{ p: 1.25, '&:last-child': { pb: 1.25 } }}>
|
||||
{isEditing ? (
|
||||
<Stack spacing={1}>
|
||||
<TextField label="Name" value={formState.name} onChange={handleChange('name')} size="small" />
|
||||
<TextField
|
||||
label="Name"
|
||||
value={formState.name}
|
||||
onChange={handleChange('name')}
|
||||
size="small"
|
||||
error={Boolean(fieldErrors.name)}
|
||||
helperText={fieldErrors.name || undefined}
|
||||
/>
|
||||
<TextField
|
||||
select
|
||||
label="Category"
|
||||
@@ -112,15 +123,15 @@ export const ProductRow = ({ product, categories, onSave, onDelete }: ProductRow
|
||||
value={formState.barcode}
|
||||
onChange={handleChange('barcode')}
|
||||
size="small"
|
||||
error={Boolean(saveError)}
|
||||
helperText={saveError || undefined}
|
||||
error={Boolean(fieldErrors.barcode)}
|
||||
helperText={fieldErrors.barcode || undefined}
|
||||
InputProps={{
|
||||
endAdornment: (
|
||||
<Button
|
||||
size="small"
|
||||
onClick={() => {
|
||||
setFormState((prev) => ({ ...prev, barcode: '' }));
|
||||
setSaveError('');
|
||||
setFieldErrors((prev) => ({ ...prev, barcode: undefined }));
|
||||
}}
|
||||
>
|
||||
Clear
|
||||
|
||||
Reference in new issue
Block a user