Merge pull request #83 from beatz174-bit/codex/extend-product-validation-in-manageproductsscreen
Validate product name uniqueness
This commit is contained in:
3 files changed
+149
-12
No files matched your search
@@ -34,6 +34,7 @@ export const ManageProductsScreen = () => {
|
||||
const [category, setCategory] = useState('');
|
||||
const [barcode, setBarcode] = useState('');
|
||||
const [barcodeError, setBarcodeError] = useState('');
|
||||
const [nameError, setNameError] = useState('');
|
||||
const [scannerOpen, setScannerOpen] = useState(false);
|
||||
const [lookupStatus, setLookupStatus] = useState<'idle' | 'loading' | 'found' | 'notfound' | 'offline'>(
|
||||
'idle',
|
||||
@@ -79,6 +80,19 @@ export const ManageProductsScreen = () => {
|
||||
[products],
|
||||
);
|
||||
|
||||
const findNameConflict = useCallback(
|
||||
(value?: string, productId?: string) => {
|
||||
if (!value) return undefined;
|
||||
|
||||
const normalizedValue = value.trim().toLowerCase();
|
||||
|
||||
return products.find(
|
||||
(product) => product.id !== productId && product.name.trim().toLowerCase() === normalizedValue,
|
||||
);
|
||||
},
|
||||
[products],
|
||||
);
|
||||
|
||||
const assertUniqueBarcode = useCallback(
|
||||
async (value?: string, productId?: string) => {
|
||||
if (!value) return;
|
||||
@@ -95,6 +109,22 @@ export const ManageProductsScreen = () => {
|
||||
[db.products, findBarcodeConflict],
|
||||
);
|
||||
|
||||
const assertUniqueName = useCallback(
|
||||
async (value: string, productId?: string) => {
|
||||
const normalized = value.trim().toLowerCase();
|
||||
if (!normalized) return;
|
||||
|
||||
const conflict = findNameConflict(value, productId);
|
||||
|
||||
if (conflict) {
|
||||
const error = new Error('A product with this name already exists.');
|
||||
error.name = 'DuplicateNameError';
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
[findNameConflict],
|
||||
);
|
||||
|
||||
const addProductToAutoLists = useCallback(
|
||||
async (product: Product, timestamp: number) => {
|
||||
const pickLists = await db.pickLists.toArray();
|
||||
@@ -183,8 +213,13 @@ export const ManageProductsScreen = () => {
|
||||
if (!name || !category) return;
|
||||
|
||||
try {
|
||||
await assertUniqueName(name);
|
||||
await assertUniqueBarcode(barcode || undefined);
|
||||
} catch (error) {
|
||||
if (error instanceof Error && error.name === 'DuplicateNameError') {
|
||||
setNameError(error.message);
|
||||
return;
|
||||
}
|
||||
if (error instanceof Error && error.name === 'DuplicateBarcodeError') {
|
||||
setBarcodeError(error.message);
|
||||
return;
|
||||
@@ -211,6 +246,7 @@ export const ManageProductsScreen = () => {
|
||||
});
|
||||
setName('');
|
||||
setBarcode('');
|
||||
setNameError('');
|
||||
setFeedback({ text: 'Product added.', severity: 'success' });
|
||||
};
|
||||
|
||||
@@ -222,6 +258,7 @@ export const ManageProductsScreen = () => {
|
||||
barcode?: string;
|
||||
},
|
||||
) => {
|
||||
await assertUniqueName(updates.name, productId);
|
||||
await assertUniqueBarcode(updates.barcode, productId);
|
||||
|
||||
await db.products.update(productId, {
|
||||
@@ -284,12 +321,23 @@ export const ManageProductsScreen = () => {
|
||||
<TextField
|
||||
label="Name"
|
||||
value={name}
|
||||
onChange={(event) => setName(event.target.value)}
|
||||
onChange={(event) => {
|
||||
setName(event.target.value);
|
||||
setNameError('');
|
||||
}}
|
||||
error={Boolean(nameError)}
|
||||
helperText={nameError || undefined}
|
||||
InputProps={
|
||||
name
|
||||
? {
|
||||
endAdornment: (
|
||||
<Button onClick={() => setName('')} size="small">
|
||||
<Button
|
||||
onClick={() => {
|
||||
setName('');
|
||||
setNameError('');
|
||||
}}
|
||||
size="small"
|
||||
>
|
||||
Clear
|
||||
</Button>
|
||||
),
|
||||
|
||||
Reference in new issue
Block a user