'use client'; import React, { useState, useEffect } from 'react'; import { Plus, Pencil, Trash2, Search, AlertCircle } from 'lucide-react'; import { useAuthStore } from '@/stores'; import dispatchHistoryService from '@/services/dispatchHistoryService'; import DispatchFormModal from '@/components/dispatch/DispatchFormModal'; import type { DispatchHistory } from '@/types'; export default function DispatchManagePage() { const user = useAuthStore((s) => s.user); const [items, setItems] = useState([]); const [loading, setLoading] = useState(true); const [search, setSearch] = useState(''); const [modalOpen, setModalOpen] = useState(false); const [editData, setEditData] = useState | null>(null); const [deleteConfirm, setDeleteConfirm] = useState(null); const fetchData = () => { dispatchHistoryService.getAll({ size: 100 }).then((res) => { setItems(res.data.content || []); }).catch(() => { console.error('Failed to fetch dispatches'); }).finally(() => { setLoading(false); }); }; useEffect(() => { fetchData(); }, []); const handleDelete = async (id: number) => { try { await dispatchHistoryService.deleteDispatch(id); setDeleteConfirm(null); fetchData(); } catch { alert('Failed to delete dispatch.'); } }; if (!user || user.roleCode === 'EMPLOYEE') { return (

Access Denied

Only managers and admins can access this page.

); } const filtered = items.filter((item) => item.projectName?.toLowerCase().includes(search.toLowerCase()) || item.roleTitle?.toLowerCase().includes(search.toLowerCase()) ); return (

Dispatch Management

Manage employee project assignments

setSearch(e.target.value)} placeholder="Search by project or role..." className="w-full pl-9 pr-3 py-2 rounded-xl border border-border text-sm focus:outline-none focus:ring-2 focus:ring-primary" />
{loading && } {!loading && filtered.length === 0 && ( )} {filtered.map((item) => ( ))}
Project Role Employee Period Actions
Loading...
No dispatch records found.
{item.projectName || `Project #${item.projectId}`} {item.roleTitle} Emp #{item.empId} {item.startDate} ~ {item.endDate || 'Present'}
setModalOpen(false)} onSaved={fetchData} editData={editData} /> {deleteConfirm && (
setDeleteConfirm(null)}>
e.stopPropagation()}>

Delete Dispatch

Are you sure you want to delete this dispatch record?

)}
); }