Skip to content

Commit 2c22cbf

Browse files
committed
download logs implemented
1 parent 542339a commit 2c22cbf

File tree

1 file changed

+107
-30
lines changed

1 file changed

+107
-30
lines changed

frontend/src/pages/log_history/LogHistory.jsx

Lines changed: 107 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,61 @@ import ContentHeader from "../../components/ContentHeader/ContentHeader";
44
import LogTable from "../../components/LogHistory/LogTable";
55
import Pagination from "../../components/LogHistory/Pagination";
66
import {
7-
PencilSquareIcon,
7+
// PencilSquareIcon,
88
ArrowDownTrayIcon,
9-
AdjustmentsHorizontalIcon,
9+
// AdjustmentsHorizontalIcon,
1010
EyeIcon,
1111
TrashIcon,
1212
} from "@heroicons/react/24/outline";
1313
import "./LogHistory.css";
1414
import { useAuth } from "../../contexts/AuthContext";
1515
import { fetchData } from "../../utils/helpers/fetchData";
1616

17-
/** Array of log actions */
18-
const logActions = [
19-
{
20-
label: "Configure",
21-
icon: PencilSquareIcon,
22-
onClick: () => {},
23-
},
24-
{
25-
label: "Download",
26-
icon: ArrowDownTrayIcon,
27-
onClick: () => {},
28-
},
29-
{
30-
label: "Filter",
31-
icon: AdjustmentsHorizontalIcon,
32-
onClick: () => {},
33-
},
34-
{
35-
label: "View",
36-
icon: EyeIcon,
37-
onClick: () => {},
38-
},
39-
{
40-
label: "Delete",
41-
icon: TrashIcon,
42-
onClick: () => {},
43-
},
44-
];
17+
const convertToCSV = (data) => {
18+
if (!data || data.length === 0) return '';
19+
const headers = Object.keys(data[0]);
20+
const csvHeader = headers.join(',');
21+
const csvRows = data.map(row =>
22+
headers.map(header => {
23+
let cell = row[header] || '';
24+
// Handle cells that contain commas by wrapping in quotes
25+
if (cell.toString().includes(',')) {
26+
cell = `"${cell}"`;
27+
}
28+
return cell;
29+
}).join(',')
30+
);
31+
return [csvHeader, ...csvRows].join('\n');
32+
};
33+
34+
// /** Array of log actions */
35+
// const logActions = [
36+
// // {
37+
// // label: "Configure",
38+
// // icon: PencilSquareIcon,
39+
// // onClick: () => {},
40+
// // },
41+
// {
42+
// label: "Download",
43+
// icon: ArrowDownTrayIcon,
44+
// onClick: () => {handleDownloadLog},
45+
// },
46+
// // {
47+
// // label: "Filter",
48+
// // icon: AdjustmentsHorizontalIcon,
49+
// // onClick: () => {},
50+
// // },
51+
// {
52+
// label: "View",
53+
// icon: EyeIcon,
54+
// onClick: () => {},
55+
// },
56+
// {
57+
// label: "Delete",
58+
// icon: TrashIcon,
59+
// onClick: () => {},
60+
// },
61+
// ];
4562

4663
export default function LogHistory() {
4764
return (
@@ -54,6 +71,7 @@ export default function LogHistory() {
5471
}
5572

5673
function MainContent() {
74+
5775
/** Retrieve user's logs from API */
5876
const [logs, setLogs] = useState([]);
5977
const { session } = useAuth();
@@ -114,6 +132,65 @@ function MainContent() {
114132
}));
115133
};
116134

135+
const handleDownloadLog = async (selectedLogs) => {
136+
try {
137+
// Get array of selected log IDs
138+
const selectedLogIds = Object.entries(selectedLogs)
139+
.filter(([, isSelected]) => isSelected)
140+
.map(([id]) => id);
141+
142+
if (selectedLogIds.length === 0) return;
143+
144+
// Get the selected logs data
145+
const selectedLogData = logs.filter(log => selectedLogIds.includes(log.id));
146+
147+
// Convert to CSV
148+
const csvContent = convertToCSV(selectedLogData);
149+
150+
// Create and download the file
151+
const blob = new Blob([csvContent], { type: 'text/csv' });
152+
const url = window.URL.createObjectURL(blob);
153+
const link = document.createElement('a');
154+
link.href = url;
155+
link.download = `selected_logs_${new Date().toISOString().split('T')[0]}.csv`;
156+
document.body.appendChild(link);
157+
link.click();
158+
document.body.removeChild(link);
159+
window.URL.revokeObjectURL(url);
160+
} catch (error) {
161+
console.error('Error downloading logs:', error);
162+
}
163+
};
164+
165+
/** Array of log actions */
166+
const logActions = [
167+
// {
168+
// label: "Configure",
169+
// icon: PencilSquareIcon,
170+
// onClick: () => {},
171+
// },
172+
{
173+
label: "Download",
174+
icon: ArrowDownTrayIcon,
175+
onClick: () => handleDownloadLog(selectedLogs),
176+
},
177+
// {
178+
// label: "Filter",
179+
// icon: AdjustmentsHorizontalIcon,
180+
// onClick: () => {},
181+
// },
182+
{
183+
label: "View",
184+
icon: EyeIcon,
185+
onClick: () => {},
186+
},
187+
{
188+
label: "Delete",
189+
icon: TrashIcon,
190+
onClick: () => {},
191+
},
192+
];
193+
117194
/** Check if all current logs are selected */
118195
const allSelected = currentLogs.every((log) => selectedLogs[log.id]);
119196

0 commit comments

Comments
 (0)