import React from 'react' import axios from 'axios' import { toast } from 'react-hot-toast' import { StockPosition, StockEntry } from '@/types' interface Props { onClose: () => void selectedPosition: StockPosition } const ChangeCountModal: React.FC = ({ onClose, selectedPosition }) => { const [selectedEntry, setSelectedEntry] = React.useState(null) const [isDropdownOpen, setIsDropdownOpen] = React.useState(false) const [newCount, setNewCount] = React.useState(null) const [loading, setLoading] = React.useState(false) const handleChangeCount = async () => { setLoading(true) const toastId = toast.loading('Changing count…') try { const { data } = await axios.post( '/api/pdaView/changeCount', { entry_id: selectedEntry.id, section_id: selectedEntry.pivot.section_id, count: selectedEntry.pivot.count, new_count: newCount, }, { withCredentials: true } ) if (!data.success) { toast.dismiss(toastId) switch (data.error) { case 'validation_failed': toast.error('Validation failed. Check inputs.') break case 'not_found': toast.error('Entry or section not found.') break case 'section_occupied': toast.error('Target section is already occupied.') break case 'insufficient_capacity': toast.error('Not enough capacity in target section.') break default: toast.error(data.message ?? 'Server error during count change.') break } setLoading(false) return } toast.dismiss(toastId) toast.success('Count changed on position.') resetAndClose() } catch (err: any) { toast.dismiss() if (err.response && err.response.data) { const payload = err.response.data if (payload.error === 'validation_failed') { toast.error('Validation failed. Check inputs.') } else if (payload.error === 'not_found') { toast.error('Entry or section not found.') } else if (payload.error === 'section_occupied') { toast.error('Target section is already occupied.') } else if (payload.error === 'insufficient_capacity') { toast.error('Not enough capacity in target section.') } else { toast.error(payload.message || 'Unknown error occurred.') } } else { toast.error('Network error. Please try again.') } setLoading(false) } } const resetAndClose = () => { setSelectedEntry(null) setNewCount(null) setLoading(false) setIsDropdownOpen(false) onClose() } // Build flat list of entries grouped by section const entriesList = React.useMemo(() => { if (!selectedPosition.sections) return [] return selectedPosition.sections.flatMap((section) => section.entries.map((entry) => ({ ...entry, pivot: entry.pivot, _section: section })) ) }, [selectedPosition]) console.log(entriesList); return (

Change item count

{/* Entry Dropdown */}
    {entriesList.map((entry) => (
  • ))} {entriesList.length === 0 && (
  • No items in this position
  • )}
{selectedEntry ? ( ): ""}
{/* Simulate Scan Button */}
{/* Waiting for scan indicator */} {selectedEntry && newCount === null && !loading && (
Waiting for section scan...
)} {/* Cancel Button */}
) } export default ChangeCountModal