Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(stats): add export to CSV functionality and update translations #281

Merged
merged 1 commit into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion resources/ts/config/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@
"tasksNotDone": "Pending Tasks",
"hoursWorked": "Hours Worked",
"fuelConsumption": "Fuel Consumption",
"noData": "No data to display"
"noData": "No data to display",
"exportCSV": "Export to CSV"
},
"taskTypes": {
"form": {
Expand Down
95 changes: 63 additions & 32 deletions resources/ts/pages/Admin/Statistics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import axiosClient from '@/api/axiosClient';
import { differenceInMonths, parseISO } from 'date-fns';
import { Chart } from 'primereact/chart';
import { Skeleton } from 'primereact/skeleton';
import { Button } from 'primereact/button';
import { SelectButton } from 'primereact/selectbutton';
import { Calendar } from 'primereact/calendar';
import { useEffect, useState } from 'react';
Expand Down Expand Up @@ -73,7 +74,7 @@ export default function Stats() {
const rangeOptions = [
{ label: t('admin.pages.stats.week'), value: 'this_week' },
{ label: t('admin.pages.stats.month'), value: 'this_month' },
{ label: t('admin.pages.stats.custom'), value: 'custom' }
{ label: t('admin.pages.stats.custom'), value: 'custom' },
];

useEffect(() => {
Expand Down Expand Up @@ -107,12 +108,12 @@ export default function Stats() {
if (rangeOption === 'custom' && customFromDate && customToDate) {
const fromIso = customFromDate.toISOString().split('T')[0];
const toIso = customToDate.toISOString().split('T')[0];

setCustomFromIso(fromIso);
setCustomToIso(toIso);
setFromDate(parseIsoToSpanish(fromIso));
setToDate(parseIsoToSpanish(toIso));

const totalMonths = differenceInMonths(customToDate, customFromDate);
if (totalMonths > 12) {
const maxEndDate = new Date(customFromDate);
Expand Down Expand Up @@ -149,7 +150,21 @@ export default function Stats() {
setIsLoading(false);
}
};

const exportToCSV = (data: number[], labels: string[], filename: string) => {
const csvContent =
'data:text/csv;charset=utf-8,' +
[
'Date,Value',
...data.map((value, index) => `${labels[index]},${value}`),
].join('\n');
const encodedUri = encodeURI(csvContent);
const link = document.createElement('a');
link.setAttribute('href', encodedUri);
link.setAttribute('download', `${filename}.csv`);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};
const bentoItems = [
{
key: 'tasksDone',
Expand Down Expand Up @@ -185,24 +200,30 @@ export default function Stats() {
},
];

const maxDate = customFromDate ? new Date(customFromDate.getFullYear() + 1, customFromDate.getMonth(), customFromDate.getDate()) : new Date();
const maxDate = customFromDate
? new Date(
customFromDate.getFullYear() + 1,
customFromDate.getMonth(),
customFromDate.getDate(),
)
: new Date();

return (
<div className="flex flex-col gap-4 p-4">
<div className="flex flex-wrap items-end gap-4">
<div>
<SelectButton
value={rangeOption}
options={rangeOptions}
onChange={(e) => setRangeOption(e.value)}
<SelectButton
value={rangeOption}
options={rangeOptions}
onChange={(e) => setRangeOption(e.value)}
/>
</div>

{rangeOption === 'custom' && (
<>
<div className="flex flex-col">
<label className="mb-2">{t('admin.pages.stats.startDate')}</label>
<Calendar
<Calendar
value={customFromDate}
onChange={(e) => setCustomFromDate(e.value as Date)}
maxDate={new Date()}
Expand All @@ -212,7 +233,7 @@ export default function Stats() {
</div>
<div className="flex flex-col">
<label className="mb-2">{t('admin.pages.stats.endDate')}</label>
<Calendar
<Calendar
value={customToDate}
onChange={(e) => setCustomToDate(e.value as Date)}
minDate={customFromDate || undefined}
Expand Down Expand Up @@ -261,28 +282,38 @@ export default function Stats() {
maxBarThickness: 60,
};
return (
<div
key={item.key}
className="bg-white rounded p-6 border border-gray-300"
style={{ height: '300px' }}>
<div className="flex justify-between items-center mb-4">
<div className="text-sm font-bold uppercase">
{item.label}
</div>
<div className="text-xl">{item.total}</div>
<div key={item.key}>
<div className="flex justify-end mb-2">
<Button
label={t('admin.pages.stats.exportCSV')}
icon="pi pi-file"
className="p-button-sm p-button-text p-button-plain"
style={{ color: 'blue' }}
onClick={() => exportToCSV(item.data, days, item.label)}
/>
</div>
{item.data.length === 0 ? (
<div className="text-center text-gray-500">
{t('admin.pages.stats.noData')}
<div
className="bg-white rounded p-6 border border-gray-300"
style={{ height: '300px' }}>
<div className="flex justify-between items-center mb-4">
<div className="text-sm font-bold uppercase">
{item.label}
</div>
<div className="text-xl">{item.total}</div>
</div>
) : (
<Chart
type="bar"
data={chartData}
options={options}
style={{ height: '88%' }}
/>
)}
{item.data.length === 0 ? (
<div className="text-center text-gray-500">
{t('admin.pages.stats.noData')}
</div>
) : (
<Chart
type="bar"
data={chartData}
options={options}
style={{ height: '88%' }}
/>
)}
</div>
</div>
);
})}
Expand Down