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

115 lines
2.9 KiB
TypeScript
Raw Normal View History

2025-02-24 08:51:44 +08:00
import { api, useVisitor } from "@nice/client";
import {
courseDetailSelect,
CourseDto,
Lecture,
2025-02-26 16:11:08 +08:00
lectureDetailSelect,
2025-02-26 11:39:53 +08:00
RolePerms,
2025-02-24 08:51:44 +08:00
VisitType,
} from "@nice/common";
import { useAuth } from "@web/src/providers/auth-provider";
2025-02-26 11:39:53 +08:00
import React, {
createContext,
ReactNode,
useEffect,
useMemo,
useState,
} from "react";
2025-02-26 10:19:29 +08:00
import { useNavigate, useParams } 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-02-26 11:39:53 +08:00
canEdit?: boolean;
2025-01-08 00:56:15 +08:00
}
2025-02-26 11:39:53 +08:00
2025-01-08 00:56:15 +08:00
interface CourseFormProviderProps {
children: ReactNode;
editId?: string; // 添加 editId 参数
}
2025-02-26 11:39:53 +08:00
2025-02-27 09:44:51 +08:00
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();
2025-02-26 11:39:53 +08:00
const { user, hasSomePermissions } = useAuth();
2025-02-26 10:19:29 +08:00
const { lectureId } = useParams();
2025-02-26 11:39:53 +08:00
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 },
2025-02-26 15:48:11 +08:00
// include: {
// // sections: { include: { lectures: true } },
// enrollments: true,
// terms:true
// },
select:courseDetailSelect
2025-01-08 00:56:15 +08:00
},
{ enabled: Boolean(editId) }
);
2025-02-26 11:39:53 +08:00
const canEdit = useMemo(() => {
2025-02-27 09:44:51 +08:00
//先判断登陆再判断是否是作者,三个条件满足一个就有编辑权限
2025-02-26 11:39:53 +08:00
const isAuthor = user?.id === course?.authorId;
const isDept = course?.depts
?.map((dept) => dept.id)
.includes(user?.deptId);
const isRoot = hasSomePermissions(RolePerms?.MANAGE_ANY_POST);
return isAuthor || isDept || isRoot;
}, [user, course]);
2025-02-27 09:44:51 +08:00
2025-01-08 00:56:15 +08:00
const [selectedLectureId, setSelectedLectureId] = useState<
string | undefined
2025-02-26 10:19:29 +08:00
>(lectureId || undefined);
2025-02-21 16:11:02 +08:00
const { data: lecture, isLoading: lectureIsLoading } = (
api.post as any
).findFirst.useQuery(
{
where: { id: selectedLectureId },
2025-02-26 16:11:08 +08:00
select: lectureDetailSelect,
2025-02-21 16:11:02 +08:00
},
{ enabled: Boolean(editId) }
);
2025-02-26 11:39:53 +08:00
2025-02-24 08:51:44 +08:00
useEffect(() => {
2025-02-26 16:11:08 +08:00
if (lecture?.id) {
2025-02-24 08:51:44 +08:00
read.mutateAsync({
data: {
visitorId: user?.id || null,
2025-02-26 16:11:08 +08:00
postId: lecture?.id,
2025-02-24 08:51:44 +08:00
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-02-26 11:39:53 +08:00
canEdit,
2025-01-08 00:56:15 +08:00
}}>
{children}
</CourseDetailContext.Provider>
);
}