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

55 lines
1.4 KiB
TypeScript
Raw Normal View History

2025-02-20 20:02:27 +08:00
import { api } from "@nice/client";
2025-01-08 00:56:15 +08:00
import { courseDetailSelect, CourseDto } from "@nice/common";
import React, { createContext, ReactNode, useState } from "react";
2025-02-06 16:32:52 +08:00
2025-01-08 00:56:15 +08:00
interface CourseDetailContextType {
editId?: string; // 添加 editId
course?: CourseDto;
selectedLectureId?: string | undefined;
setSelectedLectureId?: React.Dispatch<React.SetStateAction<string>>;
isLoading?: 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:14:47 +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 },
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) }
);
const [selectedLectureId, setSelectedLectureId] = useState<
string | undefined
>(undefined);
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,
selectedLectureId,
setSelectedLectureId,
isLoading,
2025-01-08 20:29:07 +08:00
isHeaderVisible,
setIsHeaderVisible,
2025-01-08 00:56:15 +08:00
}}>
{children}
</CourseDetailContext.Provider>
);
}