"use client" import { useState, useEffect } from "react" import { useAuth } from "@/context/auth-context" import { useRouter } from "next/navigation" import { toast } from "react-hot-toast" import type { CodingProblem } from "@/lib/types" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Badge } from "@/components/ui/badge" import { Button } from "@/components/ui/button" import Link from "next/link" import { Loader2, CheckCircle2 } from "lucide-react" import { api } from "@/lib/api" // Import api export function CodingProblemList() { const { user, isLoadingAuth, authMethod, token } = useAuth() // Check token for access const router = useRouter() const [problems, setProblems] = useState([]) const [isLoadingProblems, setIsLoadingProblems] = useState(true) const [error, setError] = useState(null) useEffect(() => { if (!isLoadingAuth && !user) { // Allow MetaMask or email auth toast.error("Please login to view coding problems.") router.push("/") return } const fetchProblems = async () => { setIsLoadingProblems(true) setError(null) try { // --- ORIGINAL API CALL (UNCOMMENT WHEN BACKEND IS READY) --- const response = await api.get("/api/coding/problems") setProblems(response.data) // --- ORIGINAL API CALL (UNCOMMENT WHEN BACKEND IS READY) --- } catch (err: any) { console.error("Failed to fetch coding problems:", err) setError(err.response?.data?.message || "Failed to load coding problems.") toast.error(err.response?.data?.message || "Failed to load coding problems.") } finally { setIsLoadingProblems(false) } } if (user) { // Only fetch if user is logged in fetchProblems() } }, [user, isLoadingAuth, router, token]) const getDifficultyColor = (difficulty: CodingProblem["difficulty"]) => { switch (difficulty) { case "Easy": return "bg-success text-white" case "Medium": return "bg-warning text-white" case "Hard": return "bg-destructive text-white" default: return "bg-gray-500 text-white" } } if (isLoadingAuth || isLoadingProblems) { return (
Loading coding problems...
) } if (error) { return (

{error}

) } if (problems.length === 0) { return (

No coding problems available yet.

Check back later for new challenges!

) } return (
{authMethod === "firebase" && !token && (

Limited Access

You are logged in with email. Full functionality, including progress tracking and data persistence, requires connecting your MetaMask wallet.

)}

Coding Practice

{problems.map((problem) => (
{problem.title} {problem.solved && }
{problem.difficulty} {problem.category}

{problem.description}

))}
) }