Exportar datos de osp de la db en un excel
This commit is contained in:
@@ -6,6 +6,7 @@ import {
|
||||
TrainingSchema,
|
||||
trainingApiResponseSchema,
|
||||
} from '../schemas/training';
|
||||
import z from 'zod';
|
||||
|
||||
export const getTrainingStatisticsAction = async (
|
||||
params: {
|
||||
@@ -159,3 +160,39 @@ export const getTrainingByIdAction = async (id: number) => {
|
||||
|
||||
return response?.data;
|
||||
};
|
||||
|
||||
export const exportTrainingAction = async (
|
||||
params: {
|
||||
startDate?: string;
|
||||
endDate?: string;
|
||||
stateId?: number;
|
||||
municipalityId?: number;
|
||||
parishId?: number;
|
||||
ospType?: string;
|
||||
} = {},
|
||||
) => {
|
||||
const searchParams = new URLSearchParams();
|
||||
if (params.startDate) searchParams.append('startDate', params.startDate);
|
||||
if (params.endDate) searchParams.append('endDate', params.endDate);
|
||||
if (params.stateId) searchParams.append('stateId', params.stateId.toString());
|
||||
if (params.municipalityId)
|
||||
searchParams.append('municipalityId', params.municipalityId.toString());
|
||||
if (params.parishId)
|
||||
searchParams.append('parishId', params.parishId.toString());
|
||||
if (params.ospType) searchParams.append('ospType', params.ospType);
|
||||
|
||||
|
||||
const [error, response] = await safeFetchApi(
|
||||
z.any(), //Schema
|
||||
`/training/export/all?${searchParams.toString()}`,
|
||||
'GET',
|
||||
undefined,
|
||||
{ responseType: 'arraybuffer' },
|
||||
);
|
||||
|
||||
if (error) {
|
||||
throw new Error(error.message || 'Error al exportar los datos');
|
||||
}
|
||||
|
||||
return Array.from(new Uint8Array(response));
|
||||
};
|
||||
|
||||
@@ -37,6 +37,8 @@ import {
|
||||
YAxis,
|
||||
} from 'recharts';
|
||||
import { useTrainingStatsQuery } from '../hooks/use-training-statistics';
|
||||
import { exportTrainingAction } from '../actions/training-actions';
|
||||
import { Download } from 'lucide-react';
|
||||
|
||||
const OSP_TYPES = [
|
||||
'EPSD',
|
||||
@@ -89,6 +91,38 @@ export function TrainingStatistics() {
|
||||
setOspType('');
|
||||
};
|
||||
|
||||
const [isExporting, setIsExporting] = useState(false);
|
||||
|
||||
const handleExport = async () => {
|
||||
try {
|
||||
setIsExporting(true);
|
||||
const bytes = await exportTrainingAction({
|
||||
startDate: startDate || undefined,
|
||||
endDate: endDate || undefined,
|
||||
stateId: stateId || undefined,
|
||||
municipalityId: municipalityId || undefined,
|
||||
parishId: parishId || undefined,
|
||||
ospType: ospType || undefined,
|
||||
});
|
||||
|
||||
const blob = new Blob([new Uint8Array(bytes)], {
|
||||
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
});
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = `entrenamientos_${new Date().toISOString().split('T')[0]}.xlsx`;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
window.URL.revokeObjectURL(url);
|
||||
document.body.removeChild(a);
|
||||
} catch (error) {
|
||||
console.error('Error exporting:', error);
|
||||
} finally {
|
||||
setIsExporting(false);
|
||||
}
|
||||
};
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="flex justify-center p-8">Cargando estadísticas...</div>
|
||||
@@ -216,10 +250,19 @@ export function TrainingStatistics() {
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
<div className="flex items-end">
|
||||
<div className="flex items-end gap-2">
|
||||
<Button variant="outline" onClick={handleClearFilters}>
|
||||
Limpiar Filtros
|
||||
</Button>
|
||||
<Button
|
||||
variant="default"
|
||||
onClick={handleExport}
|
||||
disabled={isExporting}
|
||||
className="bg-green-600 hover:bg-green-700"
|
||||
>
|
||||
<Download className="mr-2 h-4 w-4" />
|
||||
{isExporting ? 'Exportando...' : 'Exportar Excel'}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
'use server';
|
||||
import { env } from '@/lib/env';
|
||||
import axios, { InternalAxiosRequestConfig } from 'axios';
|
||||
import axios, { AxiosRequestConfig, InternalAxiosRequestConfig } from 'axios';
|
||||
import { z } from 'zod';
|
||||
|
||||
// Crear instancia de Axios con la URL base validada
|
||||
@@ -32,6 +32,7 @@ export const safeFetchApi = async <T extends z.ZodSchema<any>>(
|
||||
url: string,
|
||||
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' = 'GET',
|
||||
data?: any,
|
||||
config?: AxiosRequestConfig,
|
||||
): Promise<
|
||||
[{ type: string; message: string; details?: any } | null, z.infer<T> | null]
|
||||
> => {
|
||||
@@ -40,6 +41,7 @@ export const safeFetchApi = async <T extends z.ZodSchema<any>>(
|
||||
method,
|
||||
url,
|
||||
data,
|
||||
...config,
|
||||
});
|
||||
|
||||
const parsed = schema.safeParse(response.data);
|
||||
|
||||
Reference in New Issue
Block a user