import React, { useState, useEffect } from 'react'; import { Link } from 'react-router-dom'; import { useAuth } from '../contexts/AuthContext'; import { choreService, Chore } from '../api/chores'; import ChoreCard from '../components/ChoreCard'; import CreateChoreModal from '../components/CreateChoreModal'; import EditChoreModal from '../components/EditChoreModal'; import api from '../api/axios'; interface User { id: number; username: string; full_name: string; is_active: boolean; } const Dashboard: React.FC = () => { const { user, logout } = useAuth(); const [chores, setChores] = useState([]); const [users, setUsers] = useState([]); const [isLoading, setIsLoading] = useState(true); const [showCreateModal, setShowCreateModal] = useState(false); const [editingChoreId, setEditingChoreId] = useState(null); const [filter, setFilter] = useState<'all' | 'my' | 'today'>('all'); const [selectedUserId, setSelectedUserId] = useState(null); const [hideBirthdayChores, setHideBirthdayChores] = useState(false); useEffect(() => { loadData(); }, [selectedUserId, hideBirthdayChores]); const loadData = async () => { setIsLoading(true); try { const [choresData, usersData] = await Promise.all([ choreService.getChores({ user_id: selectedUserId || undefined, exclude_birthdays: hideBirthdayChores }), api.get('/api/v1/users') ]); setChores(choresData); setUsers(usersData.data.filter(u => u.is_active)); } catch (error) { console.error('Failed to load data:', error); } finally { setIsLoading(false); } }; const handleCompleteChore = async (id: number) => { try { await choreService.completeChore(id); await loadData(); } catch (error) { console.error('Failed to complete chore:', error); } }; const handleDeleteChore = async (id: number) => { if (window.confirm('Are you sure you want to delete this chore?')) { try { await choreService.deleteChore(id); await loadData(); } catch (error) { console.error('Failed to delete chore:', error); } } }; const handleEditChore = (id: number) => { setEditingChoreId(id); }; const filteredChores = chores.filter((chore) => { if (filter === 'my') { return chore.assigned_users?.some(u => u.id === user?.id); } if (filter === 'today') { const today = new Date().toISOString().split('T')[0]; return chore.due_date?.startsWith(today) || chore.frequency === 'daily'; } return true; }); // Calculate stats const todayChores = chores.filter((chore) => { const today = new Date().toISOString().split('T')[0]; const isToday = chore.due_date?.startsWith(today) || chore.frequency === 'daily'; const notCompleted = chore.status !== 'completed'; return isToday && notCompleted; }); const myChores = chores.filter((chore) => chore.assigned_users?.some(u => u.id === user?.id) && chore.status !== 'completed' ); const totalPoints = filteredChores.reduce((sum, chore) => chore.status !== 'completed' ? sum + chore.points : sum, 0 ); const myPoints = myChores.reduce((sum, chore) => sum + chore.points, 0); return (
{/* Header */}

Family Hub

Welcome back, {user?.full_name}!

Settings
{/* Main Content */}
{/* Stats */}

Today's Tasks

{todayChores.length}

My Tasks

{myChores.length}

My Points

{myPoints}

Total Available

{totalPoints}

{/* Filters and Actions */}
{/* User Filter Dropdown */} {/* Birthday Filter Toggle */}
{/* Active Filters Display */} {(selectedUserId || hideBirthdayChores) && (
{selectedUserId && (
User: {users.find(u => u.id === selectedUserId)?.full_name}
)} {hideBirthdayChores && (
🎂 Hiding birthday chores
)}
)} {/* Chores List */} {isLoading ? (

Loading chores...

) : filteredChores.length === 0 ? (

No chores found

{selectedUserId ? "This user has no assigned chores." : hideBirthdayChores ? "All chores are birthday chores today! 🎂" : "Get started by creating a new chore."}

) : (
{filteredChores.map((chore) => ( ))}
)}
{/* Modals */} {showCreateModal && ( setShowCreateModal(false)} onSuccess={() => { setShowCreateModal(false); loadData(); }} /> )} {editingChoreId && ( setEditingChoreId(null)} onSuccess={() => { setEditingChoreId(null); loadData(); }} /> )}
); }; export default Dashboard;