From 9251422e09ee42bfeafd3910445e7663dcc6f6b9 Mon Sep 17 00:00:00 2001 From: ditiqi Date: Tue, 25 Feb 2025 16:04:55 +0800 Subject: [PATCH] add --- .../main/courses/components/CourseCard.tsx | 161 +++++++++--------- .../main/courses/components/CourseList.tsx | 112 +++++++----- .../src/app/main/courses/instructor/page.tsx | 66 ------- .../web/src/app/main/courses/student/page.tsx | 48 ------ apps/web/src/app/main/layout/MainHeader.tsx | 29 ++-- apps/web/src/app/main/layout/MainLayout.tsx | 32 ++-- apps/web/src/app/main/layout/MainProvider.tsx | 31 ++++ apps/web/src/routes/index.tsx | 56 ++---- 8 files changed, 231 insertions(+), 304 deletions(-) delete mode 100755 apps/web/src/app/main/courses/instructor/page.tsx delete mode 100755 apps/web/src/app/main/courses/student/page.tsx create mode 100644 apps/web/src/app/main/layout/MainProvider.tsx diff --git a/apps/web/src/app/main/courses/components/CourseCard.tsx b/apps/web/src/app/main/courses/components/CourseCard.tsx index 84688f3..bd9af23 100755 --- a/apps/web/src/app/main/courses/components/CourseCard.tsx +++ b/apps/web/src/app/main/courses/components/CourseCard.tsx @@ -1,90 +1,89 @@ -import { Card, Rate, Tag ,Typography,Button} from 'antd'; -import { Course } from '../mockData'; -import { UserOutlined, ClockCircleOutlined, PlayCircleOutlined, TeamOutlined } from '@ant-design/icons'; -import { CourseDto } from '@nice/common'; -import { useNavigate } from 'react-router-dom'; - +import { Card, Rate, Tag, Typography, Button } from "antd"; +import { Course } from "../mockData"; +import { + UserOutlined, + ClockCircleOutlined, + PlayCircleOutlined, + TeamOutlined, +} from "@ant-design/icons"; +import { CourseDto } from "@nice/common"; +import { useNavigate } from "react-router-dom"; interface CourseCardProps { - course: CourseDto; + course: CourseDto; } const { Title, Text } = Typography; export default function CourseCard({ course }: CourseCardProps) { - const navigate = useNavigate(); - const handleClick = (course: CourseDto) => { - navigate(`/course/${course.id}/detail`); - } - return ( - handleClick(course)} - key={course.id} - hoverable - className="group overflow-hidden rounded-2xl border border-gray-200 bg-white + const navigate = useNavigate(); + const handleClick = (course: CourseDto) => { + navigate(`/course/${course.id}/detail`); + }; + return ( + handleClick(course)} + key={course.id} + hoverable + className="group overflow-hidden rounded-2xl border border-gray-200 bg-white shadow-xl hover:shadow-2xl transition-all duration-300 transform hover:-translate-y-2" - cover={ -
-
-
- -
- } - > -
-
- - {course.terms[0].name} - - - {course.terms[1].name} - -
- - <button > {course.title}</button> - + cover={ +
+
+
+ +
+ }> +
+
+ + {course.terms?.[0].name} + + + {course.terms?.[1].name} + +
+ + <button> {course.title}</button> + -
- -
- - {course?.depts[0]?.name} - -
- - 观看次数{course?.meta?.views}次 - -
+
+ +
+ + {course?.depts?.[0]?.name} + +
+ + 观看次数{course?.meta?.views}次 + +
- -
- -
- -
- - ); +
+ +
+
+ + ); } diff --git a/apps/web/src/app/main/courses/components/CourseList.tsx b/apps/web/src/app/main/courses/components/CourseList.tsx index f3e3177..4550795 100755 --- a/apps/web/src/app/main/courses/components/CourseList.tsx +++ b/apps/web/src/app/main/courses/components/CourseList.tsx @@ -1,44 +1,74 @@ -import { Pagination, Empty } from 'antd'; -import { Course } from '../mockData'; -import CourseCard from './CourseCard'; -import {CourseDto} from '@nice/common' +import { Pagination, Empty } from "antd"; +import CourseCard from "./CourseCard"; +import { CourseDto, Prisma } from "@nice/common"; +import { api } from "@nice/client"; +import { DefaultArgs } from "@prisma/client/runtime/library"; +import { useEffect, useMemo, useState } from "react"; interface CourseListProps { - courses: CourseDto[]; - total: number; - pageSize: number; - currentPage: number; - onPageChange: (page: number) => void; + params?: { + page?: number; + pageSize?: number; + where?: Prisma.PostWhereInput; + select?: Prisma.PostSelect; + }; } +interface CoursesPagnationProps { + data: { + items: CourseDto[]; + totalPages: number; + }; + isLoading: boolean; +} +export default function CourseList({ params }: CourseListProps) { + const [currentPage, setCurrentPage] = useState(params?.page || 1); + const { data, isLoading }: CoursesPagnationProps = + api.post.findManyWithPagination.useQuery({ + ...params, + page: currentPage, + }); + const totalPages = useMemo(() => { + if (data && !isLoading) { + return data?.totalPages; + } + return 1; + }, [data, isLoading]); -export default function CourseList({ - courses, - total, - pageSize, - currentPage, - onPageChange, -}: CourseListProps) { - return ( -
- {courses.length > 0 ? ( - <> -
- {courses.map(course => ( - - ))} -
-
- -
- - ) : ( - - )} -
- ); -} \ No newline at end of file + const courses = useMemo(() => { + if (data && !isLoading) { + return data?.items; + } + return []; + }, [data, isLoading]); + + useEffect(() => { + setCurrentPage(params?.page || 1); + }, [params?.page]); + function onPageChange(page: number, pageSize: number) { + setCurrentPage(page); + window.scrollTo({ top: 0, behavior: "smooth" }); + } + return ( +
+ {courses.length > 0 ? ( + <> +
+ {courses.map((course) => ( + + ))} +
+
+ +
+ + ) : ( + + )} +
+ ); +} diff --git a/apps/web/src/app/main/courses/instructor/page.tsx b/apps/web/src/app/main/courses/instructor/page.tsx deleted file mode 100755 index a914925..0000000 --- a/apps/web/src/app/main/courses/instructor/page.tsx +++ /dev/null @@ -1,66 +0,0 @@ -import { PlusIcon } from "@heroicons/react/24/outline"; -import { CourseList } from "@web/src/components/models/course/list/course-list"; -import { Button } from "@web/src/components/common/element/Button"; -import { api } from "@nice/client"; -import { useState } from "react"; -import { CourseCard } from "@web/src/components/models/course/card/CourseCard"; -import { useNavigate } from "react-router-dom"; -import { useAuth } from "@web/src/providers/auth-provider"; - -export default function InstructorCoursesPage() { - const navigate = useNavigate(); - const [currentPage, setCurrentPage] = useState(1); - const { user } = useAuth(); - - const { data: paginationRes, refetch } = - api.post.findManyWithPagination.useQuery({ - page: currentPage, - pageSize: 8, - where: { - instructors: { - some: { - instructorId: user?.id, - }, - }, - }, - }); - - const handlePageChange = (page: number) => { - setCurrentPage(page); - refetch(); - }; - - return ( -
-
-
-

- 我教授的课程 -

- -
- ( - { - navigate(`/course/${course.id}/editor`, { - replace: true, - }); - }} - course={course} - /> - )} - /> -
-
- ); -} diff --git a/apps/web/src/app/main/courses/student/page.tsx b/apps/web/src/app/main/courses/student/page.tsx deleted file mode 100755 index 2508c50..0000000 --- a/apps/web/src/app/main/courses/student/page.tsx +++ /dev/null @@ -1,48 +0,0 @@ -import { CourseList } from "@web/src/components/models/course/list/course-list"; -import { api } from "@nice/client"; -import { useState } from "react"; -import { CourseCard } from "@web/src/components/models/course/card/CourseCard"; -import { useAuth } from "@web/src/providers/auth-provider"; - -export default function StudentCoursesPage() { - const [currentPage, setCurrentPage] = useState(1); - const { user } = useAuth(); - - const { data: paginationRes, refetch } = - api.post.findManyWithPagination.useQuery({ - page: currentPage, - pageSize: 8, - where: { - enrollments: { - some: { - studentId: user?.id, - }, - }, - }, - }); - - const handlePageChange = (page: number) => { - setCurrentPage(page); - refetch(); - }; - - return ( -
-
-
-

- 我参加的课程 -

-
- ( - - )}> -
-
- ); -} diff --git a/apps/web/src/app/main/layout/MainHeader.tsx b/apps/web/src/app/main/layout/MainHeader.tsx index 40053e1..47503b5 100755 --- a/apps/web/src/app/main/layout/MainHeader.tsx +++ b/apps/web/src/app/main/layout/MainHeader.tsx @@ -1,19 +1,17 @@ -import { useState } from "react"; +import { useContext, useState } from "react"; import { Input, Layout, Avatar, Button, Dropdown } from "antd"; import { EditFilled, SearchOutlined, UserOutlined } from "@ant-design/icons"; import { useAuth } from "@web/src/providers/auth-provider"; import { useNavigate, useSearchParams } from "react-router-dom"; import { UserMenu } from "./UserMenu/UserMenu"; import { NavigationMenu } from "./NavigationMenu"; - +import { useMainContext } from "./MainProvider"; const { Header } = Layout; export function MainHeader() { - const [searchValue, setSearchValue] = useState(""); const { isAuthenticated, user } = useAuth(); const navigate = useNavigate(); - const [searchParams,setSearchParams] = useSearchParams(); - + const { searchValue, setSearchValue } = useMainContext(); return (
@@ -37,16 +35,17 @@ export function MainHeader() { value={searchValue} onChange={(e) => setSearchValue(e.target.value)} onPressEnter={(e) => { - //console.log(e) - //setSearchValue(""); - setSearchParams((prev)=>{ - if(searchParams.get("category")) prev.delete("category") - return prev - }) - navigate( - `/courses/?searchValue=${searchValue}` - ); - window.scrollTo({ top: 0, behavior: "smooth" }); + if ( + !window.location.pathname.startsWith( + "/courses/" + ) + ) { + navigate(`/courses/`); + window.scrollTo({ + top: 0, + behavior: "smooth", + }); + } }} />
diff --git a/apps/web/src/app/main/layout/MainLayout.tsx b/apps/web/src/app/main/layout/MainLayout.tsx index 09f5670..0a0074b 100755 --- a/apps/web/src/app/main/layout/MainLayout.tsx +++ b/apps/web/src/app/main/layout/MainLayout.tsx @@ -1,19 +1,21 @@ -import { Layout } from 'antd'; -import { Outlet } from 'react-router-dom'; -import { MainHeader } from './MainHeader'; -import { MainFooter } from './MainFooter'; +import { Layout } from "antd"; +import { Outlet } from "react-router-dom"; +import { MainHeader } from "./MainHeader"; +import { MainFooter } from "./MainFooter"; +import { MainProvider } from "./MainProvider"; const { Content } = Layout; export function MainLayout() { - return ( - - - - - - - - - ); -} \ No newline at end of file + return ( + + + + + + + + + + ); +} diff --git a/apps/web/src/app/main/layout/MainProvider.tsx b/apps/web/src/app/main/layout/MainProvider.tsx new file mode 100644 index 0000000..d503c39 --- /dev/null +++ b/apps/web/src/app/main/layout/MainProvider.tsx @@ -0,0 +1,31 @@ +import React, { createContext, ReactNode, useContext, useState } from "react"; + +interface MainContextType { + searchValue?: string; + setSearchValue?: React.Dispatch>; +} + +const MainContext = createContext(null); +interface MainProviderProps { + children: ReactNode; +} + +export function MainProvider({ children }: MainProviderProps) { + const [searchValue, setSearchValue] = useState(""); + return ( + + {children} + + ); +} +export const useMainContext = () => { + const context = useContext(MainContext); + if (!context) { + throw new Error("useMainContext must be used within MainProvider"); + } + return context; +}; diff --git a/apps/web/src/routes/index.tsx b/apps/web/src/routes/index.tsx index 70f60a8..07c7c9c 100755 --- a/apps/web/src/routes/index.tsx +++ b/apps/web/src/routes/index.tsx @@ -8,8 +8,6 @@ import { import ErrorPage from "../app/error"; import WithAuth from "../components/utils/with-auth"; import LoginPage from "../app/login"; -import StudentCoursesPage from "../app/main/courses/student/page"; -import InstructorCoursesPage from "../app/main/courses/instructor/page"; import HomePage from "../app/main/home/page"; import { CourseDetailPage } from "../app/main/course/detail/page"; import { CourseBasicForm } from "../components/models/course/editor/form/CourseBasicForm"; @@ -41,7 +39,6 @@ export type CustomRouteObject = | CustomIndexRouteObject | CustomNonIndexRouteObject; export const routes: CustomRouteObject[] = [ - { path: "/", errorElement: , @@ -66,39 +63,16 @@ export const routes: CustomRouteObject[] = [ path: "courses", element: , }, - { - path: "my-courses", - }, + { path: "profiles", }, - { - path: "courses", - children: [ - { - path: "student", - element: ( - - - - ), - }, - { - path: "instructor", - element: ( - - - - ), - }, - ], - }, + // 课程预览页面 { path: "coursePreview/:id?", - element: - } - + element: , + }, ], }, { @@ -106,20 +80,27 @@ export const routes: CustomRouteObject[] = [ children: [ { path: ":id?/editor", - element: , + element: ( + + + + ), children: [ { index: true, - element: , + element: ( + + + + ), }, - // { - // path: "goal", - // element: , - // }, + { path: "content", element: ( - + + + ), }, // { @@ -146,5 +127,4 @@ export const routes: CustomRouteObject[] = [ }, ]; - export const router = createBrowserRouter(routes);