feat: add bulk delete, fix csv formatting, fix button hover UI
This commit is contained in:
@@ -4,7 +4,7 @@ import { fetchWithAuth } from "@/lib/apiClient";
|
||||
import { useEffect, useState } from "react"
|
||||
import { Badge } from "@/components/ui/badge"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import { Search, Download, Flame, RefreshCw, Upload } from "lucide-react"
|
||||
import { Search, Download, Flame, RefreshCw, Upload, Trash2 } from "lucide-react"
|
||||
import { useRouter } from "next/navigation"
|
||||
import { useRef } from "react"
|
||||
|
||||
@@ -25,6 +25,8 @@ export default function StudentsPage() {
|
||||
const [students, setStudents] = useState<any[]>([])
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [search, setSearch] = useState("")
|
||||
const [selectedIds, setSelectedIds] = useState<string[]>([])
|
||||
const [deleting, setDeleting] = useState(false)
|
||||
|
||||
const fileInputRef = useRef<HTMLInputElement>(null)
|
||||
|
||||
@@ -45,17 +47,20 @@ export default function StudentsPage() {
|
||||
const results = await rawRes.json()
|
||||
alert(`Successfully registered ${results.length} students!`)
|
||||
|
||||
// Download CSV with passwords
|
||||
let csvContent = "data:text/csv;charset=utf-8,name,email,password\\n"
|
||||
// Download CSV with passwords using Blob to fix formatting on Windows
|
||||
const csvRows = ["name,email,password"];
|
||||
results.forEach((row: any) => {
|
||||
csvContent += `${row.name},${row.email},${row.password}\\n`
|
||||
})
|
||||
const encodedUri = encodeURI(csvContent)
|
||||
const link = document.createElement("a")
|
||||
link.setAttribute("href", encodedUri)
|
||||
link.setAttribute("download", "bulk_registered_passwords.csv")
|
||||
document.body.appendChild(link)
|
||||
link.click()
|
||||
csvRows.push(`${row.name},${row.email},${row.password}`);
|
||||
});
|
||||
const csvContent = csvRows.join("\r\n");
|
||||
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const link = document.createElement("a");
|
||||
link.setAttribute("href", url);
|
||||
link.setAttribute("download", "bulk_registered_passwords.csv");
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
URL.revokeObjectURL(url);
|
||||
|
||||
// Refresh students
|
||||
const fresh = await fetchWithAuth("/api/admin/students").then(r => r.json())
|
||||
@@ -68,6 +73,29 @@ export default function StudentsPage() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const handleDeleteSelected = async () => {
|
||||
if (!selectedIds.length) return
|
||||
if (!confirm(`Are you sure you want to delete ${selectedIds.length} students? This cannot be undone.`)) return
|
||||
|
||||
setDeleting(true)
|
||||
try {
|
||||
const res = await fetchWithAuth("/api/admin/students/bulk", {
|
||||
method: "DELETE",
|
||||
body: JSON.stringify({ user_ids: selectedIds })
|
||||
})
|
||||
if (!res.ok) throw new Error("Delete failed")
|
||||
|
||||
setSelectedIds([])
|
||||
const fresh = await fetchWithAuth("/api/admin/students").then(r => r.json())
|
||||
setStudents(fresh)
|
||||
} catch (err: any) {
|
||||
alert("Error deleting students: " + err.message)
|
||||
} finally {
|
||||
setDeleting(false)
|
||||
}
|
||||
}
|
||||
|
||||
const downloadTemplate = () => {
|
||||
const template = "data:text/csv;charset=utf-8,name,email,phone,target_exam,tier\\nJohn Doe,john@example.com,9876543210,JEE_Main,Free\\nJane Smith,jane@example.com,9876543211,NEET,Pro"
|
||||
const encodedUri = encodeURI(template)
|
||||
@@ -107,14 +135,19 @@ export default function StudentsPage() {
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex gap-2">
|
||||
{selectedIds.length > 0 && (
|
||||
<Button variant="destructive" disabled={deleting} onClick={handleDeleteSelected}>
|
||||
<Trash2 size={16} className="mr-2" /> Delete ({selectedIds.length})
|
||||
</Button>
|
||||
)}
|
||||
<input type="file" accept=".csv" className="hidden" ref={fileInputRef} onChange={handleBulkUpload} />
|
||||
<Button variant="outline" className="border-border hover:bg-secondary" onClick={() => fileInputRef.current?.click()}>
|
||||
<Button variant="outline" className="border-border hover:bg-secondary hover:text-white" onClick={() => fileInputRef.current?.click()}>
|
||||
<Upload size={16} className="mr-2" /> Bulk Register CSV
|
||||
</Button>
|
||||
<Button variant="outline" className="border-border hover:bg-secondary" onClick={downloadTemplate}>
|
||||
<Button variant="outline" className="border-border hover:bg-secondary hover:text-white" onClick={downloadTemplate}>
|
||||
<Download size={16} className="mr-2" /> Download Template
|
||||
</Button>
|
||||
<Button variant="outline" className="border-border hover:bg-secondary">
|
||||
<Button variant="outline" className="border-border hover:bg-secondary hover:text-white">
|
||||
<Download size={16} className="mr-2" /> Export CSV
|
||||
</Button>
|
||||
</div>
|
||||
@@ -143,6 +176,17 @@ export default function StudentsPage() {
|
||||
<table className="w-full text-left text-sm whitespace-nowrap">
|
||||
<thead className="bg-secondary/30 text-muted-foreground border-b border-border">
|
||||
<tr>
|
||||
<th className="px-4 py-4 w-10">
|
||||
<input
|
||||
type="checkbox"
|
||||
className="rounded border-border bg-background"
|
||||
checked={filtered.length > 0 && selectedIds.length === filtered.length}
|
||||
onChange={(e) => {
|
||||
if (e.target.checked) setSelectedIds(filtered.map(s => s.id))
|
||||
else setSelectedIds([])
|
||||
}}
|
||||
/>
|
||||
</th>
|
||||
<th className="px-6 py-4 font-semibold">Student</th>
|
||||
<th className="px-6 py-4 font-semibold">Plan</th>
|
||||
<th className="px-6 py-4 font-semibold">Target</th>
|
||||
@@ -159,6 +203,17 @@ export default function StudentsPage() {
|
||||
className="hover:bg-secondary/20 transition-colors cursor-pointer"
|
||||
onClick={() => router.push(`/admin/students/${s.id}`)}
|
||||
>
|
||||
<td className="px-4 py-4 w-10" onClick={(e) => e.stopPropagation()}>
|
||||
<input
|
||||
type="checkbox"
|
||||
className="rounded border-border bg-background"
|
||||
checked={selectedIds.includes(s.id)}
|
||||
onChange={(e) => {
|
||||
if (e.target.checked) setSelectedIds([...selectedIds, s.id])
|
||||
else setSelectedIds(selectedIds.filter(id => id !== s.id))
|
||||
}}
|
||||
/>
|
||||
</td>
|
||||
<td className="px-6 py-4">
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="w-8 h-8 rounded-full bg-secondary flex items-center justify-center text-xs font-bold shrink-0">
|
||||
|
||||
Reference in New Issue
Block a user