'use client'; import { useState, useEffect } from 'react'; import { WorkItem, WorkItemFormData, WorkItemStatus } from '@/lib/types'; import { workItemApi } from '@/lib/api-client'; import WorkItemForm from '@/components/WorkItemForm'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faCog, faEdit, faTrash } from '@fortawesome/free-solid-svg-icons'; export default function ManagePage() { const [workItems, setWorkItems] = useState([]); const [editingItem, setEditingItem] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { loadWorkItems(); }, []); const loadWorkItems = async () => { try { setLoading(true); const items = await workItemApi.getWorkItems(); setWorkItems(items); setError(null); } catch (err) { setError(err instanceof Error ? err.message : 'Failed to load work items'); } finally { setLoading(false); } }; const handleCreate = async (data: WorkItemFormData) => { try { const newItem = await workItemApi.createWorkItem(data); setWorkItems([...workItems, newItem]); } catch (err) { alert('Failed to create work item. Please try again.'); console.error('Error creating item:', err); } }; const handleEdit = (item: WorkItem) => { setEditingItem(item); }; const handleUpdate = async (data: WorkItemFormData) => { if (editingItem) { try { const updatedItem = { ...editingItem, title: data.title, description: data.description }; await workItemApi.updateWorkItem(editingItem.id, updatedItem); await loadWorkItems(); setEditingItem(null); } catch (err) { alert('Failed to update work item. Please try again.'); console.error('Error updating item:', err); } } }; const handleDelete = async (id: number) => { if (confirm('Are you sure you want to delete this item?')) { try { await workItemApi.deleteWorkItem(id); await loadWorkItems(); } catch (err) { alert('Failed to delete work item. Please try again.'); console.error('Error deleting item:', err); } } }; const handleCancelEdit = () => { setEditingItem(null); }; if (loading) { return (

Admin Panel

Loading work items...
); } if (error) { return (

Admin Panel

Error: {error}
); } return (

Admin Panel

{editingItem ? 'Edit Work Item' : 'Create New Work Item'}

{editingItem && ( )}

Existing Work Items

{workItems.map((item) => (

{item.title}

{item.description}

))}
); }