book_manage/apps/web/src/components/models/course/detail/CourseDetailContext.tsx

92 lines
2.3 KiB
TypeScript
Raw Normal View History

2025-02-24 08:51:44 +08:00
import { api, useVisitor } from "@nice/client";
import {
courseDetailSelect,
CourseDto,
Lecture,
VisitType,
} from "@nice/common";
import { useAuth } from "@web/src/providers/auth-provider";
2025-02-21 13:20:13 +08:00
import React, { createContext, ReactNode, useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";
2025-02-06 16:32:52 +08:00
2025-01-08 00:56:15 +08:00
interface CourseDetailContextType {
editId?: string; // 添加 editId
course?: CourseDto;
2025-02-21 16:11:02 +08:00
lecture?: Lecture;
2025-01-08 00:56:15 +08:00
selectedLectureId?: string | undefined;
setSelectedLectureId?: React.Dispatch<React.SetStateAction<string>>;
isLoading?: boolean;
2025-02-23 18:59:38 +08:00
lectureIsLoading?: boolean;
2025-01-08 20:29:07 +08:00
isHeaderVisible: boolean; // 新增
setIsHeaderVisible: (visible: boolean) => void; // 新增
2025-01-08 00:56:15 +08:00
}
interface CourseFormProviderProps {
children: ReactNode;
editId?: string; // 添加 editId 参数
}
export const CourseDetailContext =
createContext<CourseDetailContextType | null>(null);
export function CourseDetailProvider({
children,
editId,
}: CourseFormProviderProps) {
2025-02-21 13:20:13 +08:00
const navigate = useNavigate();
2025-02-24 08:51:44 +08:00
const { read } = useVisitor();
const { user } = useAuth();
2025-01-08 00:56:15 +08:00
const { data: course, isLoading }: { data: CourseDto; isLoading: boolean } =
2025-02-20 20:02:27 +08:00
(api.post as any).findFirst.useQuery(
2025-01-08 00:56:15 +08:00
{
where: { id: editId },
include: {
2025-02-21 13:14:47 +08:00
// sections: { include: { lectures: true } },
2025-01-08 00:56:15 +08:00
enrollments: true,
},
},
{ enabled: Boolean(editId) }
);
2025-02-24 08:51:44 +08:00
2025-01-08 00:56:15 +08:00
const [selectedLectureId, setSelectedLectureId] = useState<
string | undefined
>(undefined);
2025-02-21 16:11:02 +08:00
const { data: lecture, isLoading: lectureIsLoading } = (
api.post as any
).findFirst.useQuery(
{
where: { id: selectedLectureId },
},
{ enabled: Boolean(editId) }
);
2025-02-24 08:51:44 +08:00
useEffect(() => {
if (course) {
2025-02-25 08:25:54 +08:00
console.log("read");
2025-02-24 08:51:44 +08:00
read.mutateAsync({
data: {
visitorId: user?.id || null,
postId: course.id,
type: VisitType.READED,
},
});
}
}, [course]);
2025-02-21 13:20:13 +08:00
useEffect(() => {
2025-02-21 16:11:02 +08:00
navigate(`/course/${editId}/detail/${selectedLectureId}`);
2025-02-21 13:20:13 +08:00
}, [selectedLectureId, editId]);
2025-01-08 20:29:07 +08:00
const [isHeaderVisible, setIsHeaderVisible] = useState(true); // 新增
2025-01-08 00:56:15 +08:00
return (
<CourseDetailContext.Provider
value={{
editId,
course,
2025-02-21 16:11:02 +08:00
lecture,
2025-01-08 00:56:15 +08:00
selectedLectureId,
setSelectedLectureId,
isLoading,
2025-02-23 18:59:38 +08:00
lectureIsLoading,
2025-01-08 20:29:07 +08:00
isHeaderVisible,
setIsHeaderVisible,
2025-01-08 00:56:15 +08:00
}}>
{children}
</CourseDetailContext.Provider>
);
}