import React, { useState, useEffect } from 'react'; import { toast } from 'sonner'; import { Loader2 } from 'lucide-react'; import { api } from '@/api'; import { Button } from '@/components/ui/button'; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from '@/components/ui/alert-dialog'; import { SectionCard } from './dataShared'; export default function SeedDemoDataSection({ onSeeded, cardProps = {} }) { const [loading, setLoading] = useState(false); const [seeded, setSeeded] = useState(false); const [counts, setCounts] = useState({ bills: 0, categories: 0 }); const [clearing, setClearing] = useState(false); const [showClearConfirm, setShowClearConfirm] = useState(false); const [statusLoading, setStatusLoading] = useState(true); useEffect(() => { api.seededStatus() .then(data => { setSeeded(data.seeded); if (data.seeded) setCounts({ bills: data.seededBills || 0, categories: data.seededCategories || 0 }); }) .catch(err => console.error('Failed to check seeded status:', err)) .finally(() => setStatusLoading(false)); }, []); const handleSeed = async () => { setLoading(true); try { const data = await api.seedDemoData(); if (!data || typeof data !== 'object') throw new Error('Invalid response from server'); setCounts({ bills: data.billsCreated || 0, categories: data.categoriesCreated || 0 }); setSeeded(true); toast.success(`Created ${data.billsCreated || 0} demo bills successfully.`); setTimeout(() => onSeeded?.(), 100); } catch (err) { console.error('Seed error:', err); toast.error(err?.message || err?.error || 'Failed to seed demo data.'); } finally { setLoading(false); } }; const handleClearDemoData = async () => { setClearing(true); try { const data = await api.clearDemoData(); setSeeded(false); setCounts({ bills: 0, categories: 0 }); setShowClearConfirm(false); toast.success(`Removed ${data.billsDeleted || 0} demo bills and ${data.categoriesDeleted || 0} demo categories.`); onSeeded?.(); } catch (err) { toast.error(err.message || 'Failed to clear demo data.'); } finally { setClearing(false); } }; return (
{statusLoading ? (

Loading…

) : seeded ? ( <>

Demo data seeded

Bills

{counts.bills}

Categories

{counts.categories}

) : (

Create 20 realistic demo bills and 8 demo categories for testing purposes. The data will be associated with your account.

)}
Clear Demo Data This will remove {counts.bills} demo bills and {counts.categories} demo categories from your account. This cannot be undone. Cancel {clearing ? <>Clearing… : 'Clear Data'}
); }