diff --git a/src/components/profile/UserProfile.js b/src/components/profile/UserProfile.js index f0f52df..e657054 100644 --- a/src/components/profile/UserProfile.js +++ b/src/components/profile/UserProfile.js @@ -14,6 +14,7 @@ import { nip19 } from "nostr-tools"; import Image from "next/image"; import GithubContributionChart from "@/components/charts/GithubContributionChart"; import GithubContributionChartDisabled from "@/components/charts/GithubContributionChartDisabled"; +import useCheckCourseProgress from "@/hooks/tracking/useCheckCourseProgress"; import useWindowWidth from "@/hooks/useWindowWidth"; import { useToast } from "@/hooks/useToast"; import UserProgress from "@/components/profile/progress/UserProgress"; @@ -27,6 +28,7 @@ const UserProfile = () => { const { ndk, addSigner } = useNDKContext(); const { showToast } = useToast(); const menu = useRef(null); + useCheckCourseProgress(); const copyToClipboard = (text) => { navigator.clipboard.writeText(text); diff --git a/src/hooks/tracking/useCheckCourseProgress.js b/src/hooks/tracking/useCheckCourseProgress.js new file mode 100644 index 0000000..c85d332 --- /dev/null +++ b/src/hooks/tracking/useCheckCourseProgress.js @@ -0,0 +1,40 @@ +import { useEffect } from 'react'; +import { useSession } from 'next-auth/react'; +import axios from 'axios'; +import { checkCourseCompletion } from '@/db/models/userCourseModels'; + +const useCheckCourseProgress = () => { + const { data: session } = useSession(); + + useEffect(() => { + const updateCourseCompletionStatus = async () => { + if (!session?.user) return; + + const userId = session.user.id; + const userCourses = session.user.userCourses; + + for (const userCourse of userCourses) { + const courseId = userCourse.courseId; + const isCompleted = await checkCourseCompletion(userId, courseId); + + if (isCompleted) { + try { + await axios.put(`/api/users/${userId}/courses/${courseId}`, { + completed: true, + completedAt: new Date().toISOString(), + }); + console.log(`Course ${courseId} marked as completed for user ${userId}`); + } catch (error) { + console.error(`Failed to update course ${courseId} completion status:`, error); + } + } + } + }; + + updateCourseCompletionStatus(); + }, [session]); + + return null; +}; + +export default useCheckCourseProgress; \ No newline at end of file