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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 5x 5x 5x 5x 5x 9x 9x 9x 9x 9x 9x 9x 9x 7x 2x 2x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 1x 1x | /**
* useDeleteItemState - Selection and dialog state management
*
* Manages: supplier selection, item selection, search query, deletion reason, error/confirmation states
* Responsibility: Pure state management with dependency effects
*/
import * as React from 'react';
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import type { SupplierOption, ItemOption } from '../../../../api/analytics/types';
import { deleteItemSchema, type DeleteItemForm } from '../../../../api/inventory/validation';
export function useDeleteItemState() {
// Selection state - core domain entities
const [selectedSupplier, setSelectedSupplier] = React.useState<SupplierOption | null>(null);
const [selectedItem, setSelectedItem] = React.useState<ItemOption | null>(null);
// Form inputs
const [itemQuery, setItemQuery] = React.useState('');
const [deletionReason, setDeletionReason] = React.useState('');
// Dialog state
const [formError, setFormError] = React.useState('');
const [showConfirmation, setShowConfirmation] = React.useState(false);
// Form validation state
const { handleSubmit, formState: { isSubmitting }, reset, setValue } = useForm<DeleteItemForm>({
resolver: zodResolver(deleteItemSchema),
defaultValues: { itemId: '' },
});
/**
* Reset dependent state when supplier changes
* Ensures: item selection, search query, and form state are cleared
* Prevents: stale data from previous supplier selection
*/
React.useEffect(() => {
setSelectedItem(null);
setItemQuery('');
setValue('itemId', '');
setFormError('');
setShowConfirmation(false);
}, [selectedSupplier, setValue]);
/**
* Sync form validation state with UI item selection
* Ensures: form itemId matches selected item for submission
* Purpose: keep react-hook-form synchronized with local state
*/
React.useEffect(() => {
if (selectedItem) {
setValue('itemId', selectedItem.id);
}
}, [selectedItem, setValue]);
return {
// Selection state
selectedSupplier,
setSelectedSupplier,
selectedItem,
setSelectedItem,
// Form inputs
itemQuery,
setItemQuery,
deletionReason,
setDeletionReason,
// Dialog state
formError,
setFormError,
showConfirmation,
setShowConfirmation,
// Form validation
isSubmitting,
handleSubmit,
reset,
// Reset all state (called on close)
resetAll: () => {
setSelectedSupplier(null);
setSelectedItem(null);
setItemQuery('');
setFormError('');
setShowConfirmation(false);
setDeletionReason('');
reset();
},
};
}
export type UseDeleteItemStateReturn = ReturnType<typeof useDeleteItemState>;
|