diff --git a/CLIENT/src/components/dashboard/admin-dashboard.tsx b/CLIENT/src/components/dashboard/admin-dashboard.tsx new file mode 100644 index 0000000..0c6969f --- /dev/null +++ b/CLIENT/src/components/dashboard/admin-dashboard.tsx @@ -0,0 +1,401 @@ +import { useState, useEffect } from "react" +import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" +import { Button } from "@/components/ui/button" +import { Input } from "@/components/ui/input" +import { Textarea } from "@/components/ui/textarea" +import { Label } from "@/components/ui/label" +import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs" +import { Badge } from "@/components/ui/badge" +import { Building2, Clock, MapPin, Plus, RefreshCw } from "lucide-react" +import { toast } from "@/components/ui/use-toast" // Import toast if available, or remove if not + +interface Internship { + id: number + title: string + company: string + location: string + type: string + duration: string | number + description: string + isActive: boolean + stipend: number +} + +export default function AdminDashboard() { + const [activeInternships, setActiveInternships] = useState([]) + const [inactiveInternships, setInactiveInternships] = useState([]) + const [newInternship, setNewInternship] = useState({ + title: "", + company: "", + location: "", + type: "Remote", + duration: "", + description: "", + stipend: 0 + }) + const [isLoading, setIsLoading] = useState(false) + const [isToggling, setIsToggling] = useState(null) + + const fetchInternships = async () => { + try { + console.log("Fetching internships...") + + // Fetch active internships + const activeResponse = await fetch("http://localhost:8000/api/fetchinternships") + if (!activeResponse.ok) { + throw new Error(`Failed to fetch active internships: ${activeResponse.status}`) + } + const activeData = await activeResponse.json() + console.log("Active internships:", activeData) + setActiveInternships(activeData) + + // Fetch inactive internships + const inactiveResponse = await fetch("http://localhost:8000/api/fetchdeletedinternships") + if (!inactiveResponse.ok) { + throw new Error(`Failed to fetch inactive internships: ${inactiveResponse.status}`) + } + const inactiveData = await inactiveResponse.json() + console.log("Inactive internships:", inactiveData) + setInactiveInternships(inactiveData) + + } catch (error) { + console.error("Error fetching internships:", error) + // If toast is available + try { + toast({ + title: "Error fetching internships", + description: error instanceof Error ? error.message : "Unknown error", + variant: "destructive", + }) + } catch (e) { + // If toast is not available, just log + console.error("Toast error:", e) + } + } + } + + useEffect(() => { + fetchInternships() + }, []) + + const handleInputChange = (e: React.ChangeEvent) => { + const { name, value } = e.target + setNewInternship(prev => ({ + ...prev, + [name]: name === "stipend" ? parseInt(value) || 0 : value + })) + } + + const handleAddInternship = async (e: React.FormEvent) => { + e.preventDefault() + setIsLoading(true) + console.log("Adding internship:", newInternship) + + try { + const response = await fetch("http://localhost:8000/api/addinternships", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(newInternship), + }) + + console.log("Add response status:", response.status) + + if (response.ok) { + const data = await response.json() + console.log("Add response data:", data) + + // Reset form and refresh data + setNewInternship({ + title: "", + company: "", + location: "", + type: "Remote", + duration: "", + description: "", + stipend: 0 + }) + + // Show success message if toast is available + try { + toast({ + title: "Success", + description: "Internship added successfully", + }) + } catch (e) { + console.log("Internship added successfully") + } + + await fetchInternships() + } else { + const errorText = await response.text() + throw new Error(`Failed to add internship: ${response.status} - ${errorText}`) + } + } catch (error) { + console.error("Error adding internship:", error) + // If toast is available + try { + toast({ + title: "Error adding internship", + description: error instanceof Error ? error.message : "Unknown error", + variant: "destructive", + }) + } catch (e) { + // If toast is not available, just log to console + console.error("Toast error:", e) + } + } finally { + setIsLoading(false) + } + } + + const toggleInternshipStatus = async (id: number) => { + setIsToggling(id) + console.log("Toggling internship status, ID:", id) + + try { + const response = await fetch(`http://localhost:8000/api/toggleinternship/${id}`, { + method: "PUT", + headers: { + "Content-Type": "application/json", + } + }) + + console.log("Toggle response status:", response.status) + + if (response.ok) { + const data = await response.json() + console.log("Toggle response data:", data) + + // Show success message if toast is available + try { + toast({ + title: "Success", + description: data.message || "Internship status updated", + }) + } catch (e) { + console.log("Internship status updated successfully") + } + + // Refresh data after toggle + await fetchInternships() + } else { + const errorText = await response.text() + throw new Error(`Failed to toggle status: ${response.status} - ${errorText}`) + } + } catch (error) { + console.error("Error toggling status:", error) + // If toast is available + try { + toast({ + title: "Error updating status", + description: error instanceof Error ? error.message : "Unknown error", + variant: "destructive", + }) + } catch (e) { + // If toast is not available, just log + console.error("Toast error:", e) + } + } finally { + setIsToggling(null) + } + } + + const renderInternshipCard = (internship: Internship, isActive: boolean) => ( + + +
+
+ {internship.title} + + + {internship.company} + +
+ {!isActive && ( + Inactive + )} +
+
+ +
+
+ + {internship.location} +
+
+ + {typeof internship.duration === 'number' + ? `${internship.duration} month${internship.duration !== 1 ? 's' : ''}` + : internship.duration} +
+ {internship.type || 'Remote'} +

{internship.description}

+ {internship.stipend > 0 && ( +

Stipend: ₹{internship.stipend}/month

+ )} +
+
+
+ +
+
+ ) + + return ( +
+

Internship Management

+ + + + Active Internships ({activeInternships.length}) + Inactive Internships ({inactiveInternships.length}) + Add New Internship + + + +
+ +
+
+ {activeInternships.map((internship) => renderInternshipCard(internship, true))} + {activeInternships.length === 0 && ( +

No active internships found.

+ )} +
+
+ + +
+ +
+
+ {inactiveInternships.map((internship) => renderInternshipCard(internship, false))} + {inactiveInternships.length === 0 && ( +

No inactive internships found.

+ )} +
+
+ + + + + Add New Internship + Fill in the details to add a new internship position + + +
+
+
+ + +
+
+ + +
+
+ +
+
+ + +
+
+ + +
+
+ +
+
+ + +
+
+ + +
+
+ +
+ +