seed-database-button.tsx61 lines, 1.5 KB
| 1 | "use client"; |
| 2 | |
| 3 | import { useState } from "react"; |
| 4 | import { Button } from "@/components/ui/button"; |
| 5 | import { seedDatabase } from "@/server/actions/admin/seed-database"; |
| 6 | import { toast } from "sonner"; |
| 7 | import { useRouter } from "next/navigation"; |
| 8 | |
| 9 | export function SeedDatabaseButton() { |
| 10 | const [isLoading, setIsLoading] = useState(false); |
| 11 | const router = useRouter(); |
| 12 | |
| 13 | const handleSeed = async () => { |
| 14 | // Confirm before seeding |
| 15 | if ( |
| 16 | !confirm( |
| 17 | "Are you sure you want to seed the database with test data? This will add test users and URLs." |
| 18 | ) |
| 19 | ) { |
| 20 | return; |
| 21 | } |
| 22 | |
| 23 | setIsLoading(true); |
| 24 | |
| 25 | try { |
| 26 | const response = await seedDatabase(); |
| 27 | |
| 28 | if (response.success) { |
| 29 | toast.success("Database seeded", { |
| 30 | description: "Test data has been added to the database.", |
| 31 | }); |
| 32 | // Refresh the page to show updated data |
| 33 | router.refresh(); |
| 34 | } else { |
| 35 | toast.error("Error", { |
| 36 | description: response.error || "Failed to seed database", |
| 37 | }); |
| 38 | } |
| 39 | } catch (error) { |
| 40 | console.error("Error seeding database:", error); |
| 41 | toast.error("Error", { |
| 42 | description: "An unexpected error occurred", |
| 43 | }); |
| 44 | } finally { |
| 45 | setIsLoading(false); |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | return ( |
| 50 | <Button onClick={handleSeed} disabled={isLoading}> |
| 51 | {isLoading ? ( |
| 52 | <> |
| 53 | <span className="mr-2 h-4 w-4 animate-spin rounded-full border-2 border-current border-t-transparent" /> |
| 54 | Seeding... |
| 55 | </> |
| 56 | ) : ( |
| 57 | "Seed Database" |
| 58 | )} |
| 59 | </Button> |
| 60 | ); |
| 61 | } |