UNPKG

@defikitdotnet/education-module-ai

Version:
83 lines (82 loc) 8.85 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var jsx_runtime_1 = require("react/jsx-runtime"); var react_1 = require("react"); var router_1 = require("next/router"); var CourseCard_1 = require("../../components/CourseCard"); var useCourses_1 = require("../../hooks/useCourses"); var button_1 = require("../../components/ui/button"); var dialog_1 = require("../../components/ui/dialog"); var input_1 = require("../../components/ui/input"); var label_1 = require("../../components/ui/label"); var sonner_1 = require("sonner"); var lucide_react_1 = require("lucide-react"); var link_1 = __importDefault(require("next/link")); var TeacherDashboard = function () { var router = (0, router_1.useRouter)(); // State for teacher information var _a = (0, react_1.useState)(null), teacher = _a[0], setTeacher = _a[1]; var _b = (0, react_1.useState)(false), isAuthenticated = _b[0], setIsAuthenticated = _b[1]; var _c = (0, react_1.useState)(""), studentUrl = _c[0], setStudentUrl = _c[1]; var _d = (0, useCourses_1.useCourses)((teacher === null || teacher === void 0 ? void 0 : teacher.id) || "", process.env.NEXT_PUBLIC_BACKEND_URL || "/api"), courses = _d.courses, isLoading = _d.isLoading, error = _d.error, fetchCourses = _d.fetchCourses; (0, react_1.useEffect)(function () { // Check for teacher info in localStorage var storedTeacher = localStorage.getItem('currentTeacher'); if (storedTeacher) { try { var teacherData = JSON.parse(storedTeacher); setTeacher(teacherData); setIsAuthenticated(true); // Generate student access URL using teacher ID var origin_1 = typeof window !== "undefined" ? window.location.origin : ""; setStudentUrl("".concat(origin_1, "/student?teacherId=").concat(teacherData.id)); } catch (e) { console.error("Failed to parse teacher data:", e); } } else { // Redirect to login if no teacher data found router.push('/teacher/login'); } }, [router]); var handleCopyUrl = function () { navigator.clipboard .writeText(studentUrl) .then(function () { sonner_1.toast.success("URL Copied!", { description: "The student access URL has been copied to your clipboard.", }); }) .catch(function (err) { console.error("Failed to copy URL: ", err); sonner_1.toast.error("Copy Failed", { description: "Could not copy the URL. Please try copying manually.", }); }); }; var handleLogout = function () { localStorage.removeItem('currentTeacher'); router.push('/'); // Remove toast notification as the server will handle this }; if (!isAuthenticated || !teacher) { return ((0, jsx_runtime_1.jsx)("div", { className: "min-h-screen bg-gray-50 flex items-center justify-center", children: (0, jsx_runtime_1.jsxs)("div", { className: "bg-white p-8 rounded-lg shadow-md max-w-md w-full", children: [(0, jsx_runtime_1.jsx)("h1", { className: "text-2xl font-bold text-gray-800 mb-6", children: "Teacher Authentication Required" }), (0, jsx_runtime_1.jsx)("p", { className: "text-gray-600 mb-4", children: "You need to log in to access the teacher dashboard." }), (0, jsx_runtime_1.jsx)(link_1.default, { href: "/teacher/login", children: (0, jsx_runtime_1.jsx)(button_1.Button, { className: "w-full", children: "Go to Login" }) })] }) })); } return ((0, jsx_runtime_1.jsxs)("div", { className: "min-h-screen bg-gray-50", children: [(0, jsx_runtime_1.jsx)("header", { className: "bg-white shadow", children: (0, jsx_runtime_1.jsxs)("div", { className: "max-w-7xl mx-auto py-6 px-4 sm:px-6 lg:px-8 flex justify-between items-center", children: [(0, jsx_runtime_1.jsx)("h1", { className: "text-3xl font-bold text-gray-900", children: "Teacher Dashboard" }), (0, jsx_runtime_1.jsxs)("div", { className: "flex items-center space-x-4", children: [(0, jsx_runtime_1.jsxs)("span", { className: "text-sm text-gray-500", children: ["Welcome, ", teacher.name] }), (0, jsx_runtime_1.jsxs)("div", { className: "flex space-x-4", children: [(0, jsx_runtime_1.jsx)(button_1.Button, { variant: "outline", onClick: function () { return router.push("/teacher/create"); }, children: "Create Course Manually" }), (0, jsx_runtime_1.jsx)(button_1.Button, { variant: "outline", onClick: function () { return router.push("/teacher/course/create-with-ai"); }, children: "Create Course with AI" }), (0, jsx_runtime_1.jsxs)(dialog_1.Dialog, { children: [(0, jsx_runtime_1.jsx)(dialog_1.DialogTrigger, { asChild: true, children: (0, jsx_runtime_1.jsx)(button_1.Button, { variant: "outline", children: "Publish & Share" }) }), (0, jsx_runtime_1.jsxs)(dialog_1.DialogContent, { className: "sm:max-w-[425px] bg-white", children: [(0, jsx_runtime_1.jsxs)(dialog_1.DialogHeader, { children: [(0, jsx_runtime_1.jsx)(dialog_1.DialogTitle, { children: "Share Student Access Link" }), (0, jsx_runtime_1.jsx)(dialog_1.DialogDescription, { children: "Share this unique link with your students. They will be required to sign up or log in to view your courses." })] }), (0, jsx_runtime_1.jsx)("div", { className: "grid gap-4 py-4", children: (0, jsx_runtime_1.jsxs)("div", { className: "grid grid-cols-4 items-center gap-4", children: [(0, jsx_runtime_1.jsx)(label_1.Label, { htmlFor: "student-link", className: "text-right", children: "Link" }), (0, jsx_runtime_1.jsx)(input_1.Input, { id: "student-link", value: studentUrl, readOnly: true, className: "col-span-3" })] }) }), (0, jsx_runtime_1.jsxs)(dialog_1.DialogFooter, { className: "sm:justify-start", children: [(0, jsx_runtime_1.jsxs)(button_1.Button, { type: "button", onClick: handleCopyUrl, children: [(0, jsx_runtime_1.jsx)(lucide_react_1.Copy, { className: "mr-2 h-4 w-4" }), " Copy Link"] }), (0, jsx_runtime_1.jsx)(dialog_1.DialogClose, { asChild: true, children: (0, jsx_runtime_1.jsx)(button_1.Button, { type: "button", variant: "ghost", children: "Close" }) })] })] })] }), (0, jsx_runtime_1.jsx)(button_1.Button, { variant: "ghost", onClick: handleLogout, children: "Logout" })] })] })] }) }), (0, jsx_runtime_1.jsxs)("main", { className: "max-w-7xl mx-auto py-6 sm:px-6 lg:px-8", children: [error && ((0, jsx_runtime_1.jsxs)("div", { className: "bg-red-100 border-l-4 border-red-500 text-red-700 p-4 mb-6", role: "alert", children: [(0, jsx_runtime_1.jsx)("p", { className: "font-bold", children: "Error Loading Courses" }), (0, jsx_runtime_1.jsx)("p", { children: typeof error === "string" ? error : JSON.stringify(error) })] })), (0, jsx_runtime_1.jsxs)("div", { className: "px-4 py-6 sm:px-0", children: [(0, jsx_runtime_1.jsx)("h2", { className: "text-xl font-semibold text-gray-800 mb-6", children: "Your Courses" }), isLoading ? ((0, jsx_runtime_1.jsx)("div", { className: "flex justify-center items-center h-64", children: (0, jsx_runtime_1.jsx)("p", { className: "text-gray-500", children: "Loading courses..." }) })) : /* Handle No Courses or Render Courses - Ensure courses is an array */ courses && courses.length > 0 ? ((0, jsx_runtime_1.jsx)("div", { className: "grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6", children: courses.map(function (course) { return ((0, jsx_runtime_1.jsx)(CourseCard_1.CourseCard, { course: course, onClick: function () { return router.push("/teacher/course/".concat(course.id)); } }, course.id)); }) })) : ( /* Handle case where courses is empty array or not loaded yet (and not loading/error) */ (0, jsx_runtime_1.jsxs)("div", { className: "bg-gray-50 border-2 border-dashed border-gray-300 rounded-lg p-12 text-center", children: [(0, jsx_runtime_1.jsx)("h3", { className: "text-lg font-medium text-gray-900 mb-2", children: "No courses found" }), (0, jsx_runtime_1.jsx)("p", { className: "text-gray-500 mb-6", children: "Get started by creating your first course." }), (0, jsx_runtime_1.jsx)(button_1.Button, { onClick: function () { return router.push("/teacher/create"); }, children: "Create Your First Course" })] }))] })] })] })); }; exports.default = TeacherDashboard;