Add dashboard page with chore management

This commit is contained in:
2026-01-27 22:22:21 +11:00
parent cdfcb1bcce
commit 93a2b8acb1

View File

@@ -0,0 +1,220 @@
import React, { useState, useEffect } from 'react';
import { useAuth } from '../contexts/AuthContext';
import { choreService, Chore } from '../api/chores';
import ChoreCard from '../components/ChoreCard';
import CreateChoreModal from '../components/CreateChoreModal';
const Dashboard: React.FC = () => {
const { user, logout } = useAuth();
const [chores, setChores] = useState<Chore[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [showCreateModal, setShowCreateModal] = useState(false);
const [filter, setFilter] = useState<'all' | 'my' | 'today'>('all');
useEffect(() => {
loadChores();
}, []);
const loadChores = async () => {
try {
const data = await choreService.getChores();
setChores(data);
} catch (error) {
console.error('Failed to load chores:', error);
} finally {
setIsLoading(false);
}
};
const handleCompleteChore = async (id: number) => {
try {
await choreService.completeChore(id);
await loadChores();
} 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 loadChores();
} catch (error) {
console.error('Failed to delete chore:', error);
}
}
};
const filteredChores = chores.filter((chore) => {
if (filter === 'my') {
return chore.assigned_to === user?.id;
}
if (filter === 'today') {
const today = new Date().toISOString().split('T')[0];
return chore.due_date === today || chore.frequency === 'daily';
}
return true;
});
const todayChores = chores.filter((chore) => {
const today = new Date().toISOString().split('T')[0];
return (chore.due_date === today || chore.frequency === 'daily') && chore.status !== 'completed';
});
const myChores = chores.filter((chore) => chore.assigned_to === user?.id && chore.status !== 'completed');
return (
<div className="min-h-screen bg-gray-50">
{/* Header */}
<header className="bg-white shadow-sm">
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-4 flex justify-between items-center">
<div>
<h1 className="text-2xl font-bold text-gray-900">Family Hub</h1>
<p className="text-sm text-gray-600">Welcome back, {user?.full_name}!</p>
</div>
<button
onClick={logout}
className="px-4 py-2 text-sm text-gray-700 hover:text-gray-900 hover:bg-gray-100 rounded-lg transition-colors"
>
Sign Out
</button>
</div>
</header>
{/* Main Content */}
<main className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
{/* Stats */}
<div className="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8">
<div className="bg-white rounded-lg shadow p-6">
<div className="flex items-center justify-between">
<div>
<p className="text-sm text-gray-600">Today's Tasks</p>
<p className="text-3xl font-bold text-blue-600">{todayChores.length}</p>
</div>
<div className="p-3 bg-blue-100 rounded-full">
<svg className="w-8 h-8 text-blue-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2" />
</svg>
</div>
</div>
</div>
<div className="bg-white rounded-lg shadow p-6">
<div className="flex items-center justify-between">
<div>
<p className="text-sm text-gray-600">My Tasks</p>
<p className="text-3xl font-bold text-green-600">{myChores.length}</p>
</div>
<div className="p-3 bg-green-100 rounded-full">
<svg className="w-8 h-8 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z" />
</svg>
</div>
</div>
</div>
<div className="bg-white rounded-lg shadow p-6">
<div className="flex items-center justify-between">
<div>
<p className="text-sm text-gray-600">Total Tasks</p>
<p className="text-3xl font-bold text-purple-600">{chores.length}</p>
</div>
<div className="p-3 bg-purple-100 rounded-full">
<svg className="w-8 h-8 text-purple-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10" />
</svg>
</div>
</div>
</div>
</div>
{/* Actions */}
<div className="flex justify-between items-center mb-6">
<div className="flex gap-2">
<button
onClick={() => setFilter('all')}
className={`px-4 py-2 rounded-lg transition-colors ${
filter === 'all'
? 'bg-blue-600 text-white'
: 'bg-white text-gray-700 hover:bg-gray-50'
}`}
>
All Tasks
</button>
<button
onClick={() => setFilter('today')}
className={`px-4 py-2 rounded-lg transition-colors ${
filter === 'today'
? 'bg-blue-600 text-white'
: 'bg-white text-gray-700 hover:bg-gray-50'
}`}
>
Today
</button>
<button
onClick={() => setFilter('my')}
className={`px-4 py-2 rounded-lg transition-colors ${
filter === 'my'
? 'bg-blue-600 text-white'
: 'bg-white text-gray-700 hover:bg-gray-50'
}`}
>
My Tasks
</button>
</div>
<button
onClick={() => setShowCreateModal(true)}
className="px-4 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 transition-colors flex items-center gap-2"
>
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 4v16m8-8H4" />
</svg>
Create Task
</button>
</div>
{/* Chores List */}
{isLoading ? (
<div className="text-center py-12">
<div className="inline-block animate-spin rounded-full h-12 w-12 border-b-2 border-blue-600"></div>
<p className="mt-4 text-gray-600">Loading tasks...</p>
</div>
) : filteredChores.length === 0 ? (
<div className="text-center py-12 bg-white rounded-lg shadow">
<svg className="mx-auto h-12 w-12 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2" />
</svg>
<h3 className="mt-2 text-sm font-medium text-gray-900">No tasks</h3>
<p className="mt-1 text-sm text-gray-500">Get started by creating a new task.</p>
</div>
) : (
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
{filteredChores.map((chore) => (
<ChoreCard
key={chore.id}
chore={chore}
onComplete={handleCompleteChore}
onDelete={handleDeleteChore}
/>
))}
</div>
)}
</main>
{/* Create Chore Modal */}
{showCreateModal && (
<CreateChoreModal
onClose={() => setShowCreateModal(false)}
onSuccess={() => {
setShowCreateModal(false);
loadChores();
}}
/>
)}
</div>
);
};
export default Dashboard;