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 | 24x 2x 102x 102x 102x 102x 102x 12x 12x 131x 34x 34x 102x 5x 5x 5x 1x 1x 4x 2x 2x 2x 2x 2x 102x 102x 82x 1x 1x 5x 1x | import {
Box,
Button,
Card,
CardContent,
Dialog,
DialogContent,
DialogTitle,
IconButton,
Stack,
TextField,
Typography,
} from '@mui/material';
import DeleteIcon from '@mui/icons-material/Delete';
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 { Product } from '../models/Product';
import { BarcodeScannerView } from './BarcodeScannerView';
interface ProductRowProps {
product: Product;
categories: string[];
onSave: (
productId: string,
updates: {
name: string;
category: string;
barcode?: string;
},
) => Promise<void> | void;
onDelete: (productId: string) => Promise<void> | void;
}
interface ProductFormState {
name: string;
category: string;
barcode: string;
}
const getInitialFormState = (product: Product): ProductFormState => ({
name: product.name,
category: product.category,
barcode: product.barcode ?? '',
});
export const ProductRow = ({ product, categories, onSave, onDelete }: ProductRowProps) => {
const [isEditing, setIsEditing] = useState(false);
const [formState, setFormState] = useState<ProductFormState>(() => getInitialFormState(product));
const [isScannerOpen, setIsScannerOpen] = useState(false);
const [fieldErrors, setFieldErrors] = useState<{ name?: string; barcode?: string }>({});
useEffect(() => {
setFormState(getInitialFormState(product));
setFieldErrors({});
}, [product]);
const handleChange = (field: keyof ProductFormState) => (event: ChangeEvent<HTMLInputElement>) => {
setFormState((prev) => ({ ...prev, [field]: event.target.value }));
setFieldErrors((prev) => ({ ...prev, [field]: undefined }));
};
const handleSave = async () => {
Iif (!formState.name) return;
try {
await onSave(product.id, {
name: formState.name,
category: formState.category,
barcode: formState.barcode || undefined,
});
setIsEditing(false);
setFieldErrors({});
} catch (error) {
if (error instanceof Error && error.name === 'DuplicateNameError') {
setFieldErrors({ name: 'A product with this name already exists.' });
return;
}
Eif (error instanceof Error && error.name === 'DuplicateBarcodeError') {
setFieldErrors({ barcode: 'This barcode is already assigned to another product.' });
return;
}
throw error;
}
};
const handleCancel = () => {
setIsEditing(false);
setFormState(getInitialFormState(product));
setFieldErrors({});
};
return (
<Card variant="outlined" sx={{ mb: 1 }}>
<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"
error={Boolean(fieldErrors.name)}
helperText={fieldErrors.name || undefined}
/>
<TextField
select
SelectProps={{ native: true }}
label="Category"
value={formState.category}
onChange={handleChange('category')}
size="small"
>
{categories.map((cat) => (
<option key={cat} value={cat}>
{cat}
</option>
))}
</TextField>
{formState.barcode ? (
<TextField
label="Barcode"
value={formState.barcode}
onChange={handleChange('barcode')}
size="small"
error={Boolean(fieldErrors.barcode)}
helperText={fieldErrors.barcode || undefined}
InputProps={{
endAdornment: (
<Button
size="small"
onClick={() => {
setFormState((prev) => ({ ...prev, barcode: '' }));
setFieldErrors((prev) => ({ ...prev, barcode: undefined }));
}}
>
Clear
</Button>
),
}}
/>
) : (
<Button variant="outlined" onClick={() => setIsScannerOpen(true)}>
Scan Barcode
</Button>
)}
<Stack direction="row" spacing={0.5} justifyContent="flex-end" alignItems="center">
<IconButton
aria-label={`Delete ${product.name}`}
onClick={() => onDelete(product.id)}
size="small"
color="error"
>
<DeleteIcon fontSize="small" />
</IconButton>
<IconButton aria-label="Save product" onClick={handleSave} disabled={!formState.name} color="primary">
<CheckIcon />
</IconButton>
<IconButton aria-label="Cancel edit" onClick={handleCancel}>
<CloseIcon />
</IconButton>
</Stack>
</Stack>
) : (
<Stack direction="row" alignItems="center" spacing={1} justifyContent="space-between">
<Stack direction="row" spacing={1.5} alignItems="center" sx={{ flex: 1, minWidth: 0 }}>
<Stack spacing={0.25} sx={{ minWidth: 0 }}>
<Typography variant="subtitle1" noWrap>
{product.name}
</Typography>
<Typography variant="caption" color="text.secondary" noWrap>
{product.category}
</Typography>
</Stack>
{product.barcode ? (
<Typography variant="caption" color="text.secondary" noWrap>
Barcode: {product.barcode}
</Typography>
) : null}
</Stack>
<Box display="flex" alignItems="center" gap={0.5} sx={{ ml: 1 }}>
<IconButton aria-label={`Edit ${product.name}`} onClick={() => setIsEditing(true)} size="small">
<EditIcon fontSize="small" />
</IconButton>
<IconButton aria-label={`Delete ${product.name}`} onClick={() => onDelete(product.id)} size="small">
<DeleteIcon fontSize="small" />
</IconButton>
</Box>
</Stack>
)}
</CardContent>
<Dialog open={isScannerOpen} onClose={() => setIsScannerOpen(false)} fullWidth>
<DialogTitle>Scan Barcode</DialogTitle>
<DialogContent>
<BarcodeScannerView
onDetected={(code) => {
setFormState((prev) => ({ ...prev, barcode: code }));
setIsScannerOpen(false);
}}
/>
</DialogContent>
</Dialog>
</Card>
);
};
|