'use client'; import { useState } from 'react'; import { useMutation, useQueryClient } from '@tanstack/react-query'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { apiFetch } from '@/lib/api/client'; import type { RequestReportInput } from '@/lib/validators/reports'; const REPORT_TYPE_LABELS: Record = { pipeline: 'Pipeline Summary', revenue: 'Revenue Report', activity: 'Activity Log', occupancy: 'Berth Occupancy', }; export function GenerateReportForm() { const queryClient = useQueryClient(); const [reportType, setReportType] = useState(''); const [name, setName] = useState(''); const [dateFrom, setDateFrom] = useState(''); const [dateTo, setDateTo] = useState(''); const mutation = useMutation({ mutationFn: (data: RequestReportInput) => apiFetch('/api/v1/reports', { method: 'POST', body: data }), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['reports'] }); setReportType(''); setName(''); setDateFrom(''); setDateTo(''); }, }); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); if (!reportType || !name.trim()) return; const payload: RequestReportInput = { reportType: reportType as RequestReportInput['reportType'], name: name.trim(), parameters: { ...(dateFrom ? { dateFrom } : {}), ...(dateTo ? { dateTo } : {}), }, }; mutation.mutate(payload); }; return ( Generate Report
setName(e.target.value)} placeholder="e.g. Pipeline Summary Q1 2025" maxLength={200} />
setDateFrom(e.target.value)} />
setDateTo(e.target.value)} />
{mutation.isError && (

{mutation.error instanceof Error ? mutation.error.message : 'Failed to queue report. Please try again.'}

)} {mutation.isSuccess && (

Report queued successfully. You will be notified when it is ready.

)}
); }