feat: data export — CSV export for tasks, users, activities
- Backend: GET /api/export/{tasks,users,activities}?month=YYYY-MM
- Frontend: Export panel on Reports page (CEO/CTO/Manager only)
- API: apiExportCsv helper for browser download
This commit is contained in:
@@ -240,7 +240,7 @@ export default function App() {
|
||||
filterUser={null} searchQuery={searchQuery} onToggleDone={handleToggleDone} users={users} />
|
||||
)}
|
||||
{displayPage === 'teamtasks' && <TeamTasksPage tasks={tasks} currentUser={currentUser} users={users} />}
|
||||
{displayPage === 'reports' && <ReportsPage tasks={tasks} users={users} />}
|
||||
{displayPage === 'reports' && <ReportsPage tasks={tasks} users={users} currentUser={currentUser} />}
|
||||
{displayPage === 'members' && <MembersPage tasks={tasks} users={users} currentUser={currentUser} onAddUser={handleAddUser} onDeleteUser={handleDeleteUser} />}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import { useState } from 'react';
|
||||
import type { Task, User } from './data';
|
||||
import { STATUS_COLORS, PRIORITY_COLORS } from './data';
|
||||
import { apiExportCsv } from './api';
|
||||
import { BarChart, Bar, PieChart, Pie, Cell, LineChart, Line, XAxis, YAxis, Tooltip, ResponsiveContainer, Legend } from 'recharts';
|
||||
|
||||
const tooltipStyle = {
|
||||
@@ -8,7 +10,12 @@ const tooltipStyle = {
|
||||
labelStyle: { color: '#94a3b8' },
|
||||
};
|
||||
|
||||
export function ReportsPage({ tasks, users }: { tasks: Task[]; users: User[] }) {
|
||||
export function ReportsPage({ tasks, users, currentUser }: { tasks: Task[]; users: User[]; currentUser: User }) {
|
||||
const [exportType, setExportType] = useState<'tasks' | 'users' | 'activities'>('tasks');
|
||||
const [exportMonth, setExportMonth] = useState('');
|
||||
const [exporting, setExporting] = useState(false);
|
||||
|
||||
const canExport = ['ceo', 'cto', 'manager'].includes(currentUser.role);
|
||||
const total = tasks.length;
|
||||
const completed = tasks.filter(t => t.status === 'done').length;
|
||||
const overdue = tasks.filter(t => new Date(t.dueDate + 'T00:00:00') < new Date() && t.status !== 'done').length;
|
||||
@@ -113,6 +120,44 @@ export function ReportsPage({ tasks, users }: { tasks: Task[]; users: User[] })
|
||||
</ResponsiveContainer>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{canExport && (
|
||||
<div className="chart-card" style={{ marginTop: 24 }}>
|
||||
<div className="chart-card-title" style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
|
||||
<span>📥</span> Export Data
|
||||
</div>
|
||||
<div style={{ display: 'flex', gap: 12, alignItems: 'flex-end', flexWrap: 'wrap' }}>
|
||||
<div className="modal-field" style={{ margin: 0 }}>
|
||||
<label style={{ fontSize: 11, color: '#94a3b8', display: 'block', marginBottom: 4 }}>Dataset</label>
|
||||
<select className="modal-input" style={{ width: 160 }} value={exportType} onChange={e => setExportType(e.target.value as 'tasks' | 'users' | 'activities')}>
|
||||
<option value="tasks">Tasks</option>
|
||||
<option value="users">Users & Workload</option>
|
||||
<option value="activities">Activity Log</option>
|
||||
</select>
|
||||
</div>
|
||||
<div className="modal-field" style={{ margin: 0 }}>
|
||||
<label style={{ fontSize: 11, color: '#94a3b8', display: 'block', marginBottom: 4 }}>Month (optional)</label>
|
||||
<input className="modal-input" type="month" style={{ width: 160 }} value={exportMonth} onChange={e => setExportMonth(e.target.value)} />
|
||||
</div>
|
||||
<button
|
||||
className="btn-primary"
|
||||
disabled={exporting}
|
||||
onClick={async () => {
|
||||
setExporting(true);
|
||||
try {
|
||||
await apiExportCsv(exportType, exportMonth || undefined);
|
||||
} catch (err) {
|
||||
console.error('Export failed:', err);
|
||||
} finally {
|
||||
setExporting(false);
|
||||
}
|
||||
}}
|
||||
>
|
||||
{exporting ? 'Exporting...' : '⬇ Download CSV'}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
15
src/api.ts
15
src/api.ts
@@ -48,6 +48,21 @@ export async function apiDeleteUser(id: string) {
|
||||
});
|
||||
}
|
||||
|
||||
export async function apiExportCsv(type: 'tasks' | 'users' | 'activities', month?: string) {
|
||||
const params = month ? `?month=${month}` : '';
|
||||
const res = await fetch(`${API_BASE}/export/${type}${params}`);
|
||||
if (!res.ok) throw new Error('Export failed');
|
||||
const blob = await res.blob();
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = month ? `${type}_${month}.csv` : `${type}_all.csv`;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
|
||||
// Tasks
|
||||
export async function apiFetchTasks() {
|
||||
return request('/tasks');
|
||||
|
||||
Reference in New Issue
Block a user