New hook for checking if any courses are completed on profile page

This commit is contained in:
austinkelsay 2024-10-05 17:28:57 -05:00
parent c89fb91a40
commit 2df2724d83
2 changed files with 42 additions and 0 deletions

View File

@ -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);

View File

@ -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;