+
{/* Your component content */}
);
}
+
+function VideoIcon() {
+ return (
+
+ );
+}
+
+function NotionIcon() {
+ return (
+
+ );
+}
+
+// Todo: Fix types here
+function Check({ content }: { content: any }) {
+ const [completed, setCompleted] = useState(
+ content?.videoProgress?.markAsCompleted || false,
+ );
+ return (
+ <>
+
{
+ setCompleted(!completed);
+ handleMarkAsCompleted(!completed, content.id);
+ e.stopPropagation();
+ }}
+ type="checkbox"
+ className="w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 dark:focus:ring-blue-600 dark:ring-offset-gray-800 focus:ring-2 dark:bg-gray-700 dark:border-gray-600"
+ />
+ >
+ );
+}
diff --git a/src/db/course.ts b/src/db/course.ts
index 074d30974..9edb8efb6 100644
--- a/src/db/course.ts
+++ b/src/db/course.ts
@@ -1,5 +1,7 @@
import db from '@/db';
import { Cache } from '@/db/Cache';
+import { authOptions } from '@/lib/auth';
+import { getServerSession } from 'next-auth';
export interface Content {
id: number
@@ -10,6 +12,10 @@ export interface Content {
parentId: number | null
createdAt: string
children: Content[]
+ videoProgress?: {
+ currentTimestamp: string
+ markAsCompleted?: boolean
+ }
}
export interface Folder extends Content {
@@ -82,8 +88,8 @@ async function getAllContent() {
return value;
}
const allContent = await db.content.findMany({
- include: {
- videoProgress: true,
+ where: {
+ hidden: false,
},
});
@@ -92,15 +98,13 @@ async function getAllContent() {
return allContent;
}
-export const getFullCourseContent = async (courseId: number) => {
- const value = Cache.getInstance().get('getFullCourseContent', [
+async function getRootCourseContent(courseId: number) {
+ const value = Cache.getInstance().get('getRootCourseContent', [
courseId.toString(),
]);
if (value) {
return value;
}
-
- const contents = await getAllContent();
const courseContent = await db.courseContent.findMany({
orderBy: [
{
@@ -112,9 +116,51 @@ export const getFullCourseContent = async (courseId: number) => {
},
include: { content: true },
});
+ Cache.getInstance().set(
+ 'getRootCourseContent',
+ [courseId.toString()],
+ courseContent,
+ );
+ return courseContent;
+}
+
+function getVideoProgressForUser(userId: string) {
+ return db.videoProgress.findMany({
+ where: {
+ userId,
+ },
+ });
+}
+export const getFullCourseContent = async (courseId: number) => {
+ // const value = Cache.getInstance().get('getFullCourseContent', [
+ // courseId.toString(),
+ // ]);
+ // if (value) {
+ // return value;
+ // }
+ const session = await getServerSession(authOptions);
+ const contents = await getAllContent();
+ const courseContent = await getRootCourseContent(courseId);
+ const videoProgress = await getVideoProgressForUser(session?.user?.id);
const contentMap = new Map
(
- contents.map((content: any) => [content.id, { ...content, children: [] }]),
+ contents.map((content: any) => [
+ content.id,
+ {
+ ...content,
+ children: [],
+ videoProgress:
+ content.type === 'video'
+ ? {
+ duration: videoProgress.find((x) => x.contentId === content.id)
+ ?.currentTimestamp,
+ markAsCompleted: videoProgress.find(
+ (x) => x.contentId === content.id,
+ )?.markAsCompleted,
+ }
+ : null,
+ },
+ ]),
);
const rootContents: any[] = [];
contents
@@ -124,7 +170,7 @@ export const getFullCourseContent = async (courseId: number) => {
contentMap
.get(content.parentId)
.children.push(contentMap.get(content.id));
- } else if (courseContent.find((x) => x.contentId === content.id)) {
+ } else if (courseContent.find((x: any) => x.contentId === content.id)) {
rootContents.push(contentMap.get(content.id));
}
});
diff --git a/src/lib/utils.ts b/src/lib/utils.ts
index feb0c930f..0c5b9aa57 100644
--- a/src/lib/utils.ts
+++ b/src/lib/utils.ts
@@ -168,7 +168,7 @@ export const getFolderPercentCompleted = (childrenContent: any) => {
);
const totalVideosWatched = videos.filter(
({ videoProgress }: any) =>
- videoProgress && videoProgress[0]?.markAsCompleted,
+ videoProgress && videoProgress?.markAsCompleted,
).length;
return Math.ceil((totalVideosWatched / videos.length) * 100);
}