import React, { useState, useEffect } from 'react'; import axios from 'axios'; import { toast } from 'react-hot-toast'; import { StockRoom } from '@/types'; import EditStockSections from '@/Components/modals/EditStockSections'; interface Props { room: StockRoom; onClose: () => void; onUpdated: () => void; } export default function EditRoomModal({ room, onClose, onUpdated }: Props) { const [data, setData] = useState(room); const [activePosition, setActivePosition] = useState<() => any | null>(null); useEffect(() => { setData(room); }, [room]); const reload = async () => { try { const resp = await axios.get(`/api/stock-rooms/${room.room_id}`, { withCredentials: true }); setData(resp.data); } catch (error: any) { toast.error(error.response?.data?.message || 'Failed to reload room'); } }; const addLine = async () => { try { const newIndex = data.lines.length + 1; await axios.post('/api/stock-lines', { room_id: room.room_id, line_symbol: `${newIndex}`, line_name: `Line ${newIndex}` }, { withCredentials: true }); await reload(); toast.success('Line added'); } catch (error: any) { toast.error(error.response?.data?.message || 'Failed to add line'); } }; const addRack = async (line_id: number, line_symbol: string, line_name: string) => { try { const line = data.lines.find(l => l.line_id === line_id); const newIndex = line?.racks.length! + 1; await axios.post('/api/stock-racks', { line_id, rack_symbol: `${newIndex}`, rack_name: `Rack ${newIndex}` }, { withCredentials: true }); await reload(); toast.success('Rack added'); } catch (error: any) { toast.error(error.response?.data?.message || 'Failed to add rack'); } }; const addShelf = async (rack_id: number, rack_symbol: string, rack_name: string) => { try { const rack = data.lines .flatMap(l => l.racks) .find(r => r.rack_id === rack_id); const newIndex = rack?.shelves.length! + 1; await axios.post('/api/stock-shelves', { rack_id, shelf_symbol: `${newIndex}`, shelf_name: `Shelf ${newIndex}` }, { withCredentials: true }); await reload(); toast.success('Shelf added'); } catch (error: any) { toast.error(error.response?.data?.message || 'Failed to add shelf'); } }; const addPosition = async (shelf_id: number, shelf_symbol: string, shelf_name: string) => { try { const shelf = data.lines .flatMap(l => l.racks) .flatMap(r => r.shelves) .find(s => s.shelf_id === shelf_id); const newIndex = shelf?.positions.length! + 1; await axios.post('/api/stock-positions', { shelf_id, position_symbol: `${newIndex}`, position_name: `Position ${newIndex}`, capacity: 0 }, { withCredentials: true }); await reload(); toast.success('Position added'); } catch (error: any) { toast.error(error.response?.data?.message || 'Failed to add position'); } }; return (

Edit Room: {data.room_name}

{data.lines.map(line => (
{line.line_name} {line.racks.map(rack => (
{rack.rack_name} {rack.shelves.map(shelf => (
{shelf.shelf_name}
    {shelf.positions.map(pos => (
  • {pos.position_name}
  • ))}
))}
))}
))}
{activePosition && ( { setActivePosition(null); reload(); }} /> )}
); }