mirror of
https://github.com/AustinKelsay/plebdevs.git
synced 2025-06-23 16:05:24 +00:00
New hook for checking if any courses are completed on profile page
This commit is contained in:
parent
c89fb91a40
commit
2df2724d83
@ -14,6 +14,7 @@ import { nip19 } from "nostr-tools";
|
|||||||
import Image from "next/image";
|
import Image from "next/image";
|
||||||
import GithubContributionChart from "@/components/charts/GithubContributionChart";
|
import GithubContributionChart from "@/components/charts/GithubContributionChart";
|
||||||
import GithubContributionChartDisabled from "@/components/charts/GithubContributionChartDisabled";
|
import GithubContributionChartDisabled from "@/components/charts/GithubContributionChartDisabled";
|
||||||
|
import useCheckCourseProgress from "@/hooks/tracking/useCheckCourseProgress";
|
||||||
import useWindowWidth from "@/hooks/useWindowWidth";
|
import useWindowWidth from "@/hooks/useWindowWidth";
|
||||||
import { useToast } from "@/hooks/useToast";
|
import { useToast } from "@/hooks/useToast";
|
||||||
import UserProgress from "@/components/profile/progress/UserProgress";
|
import UserProgress from "@/components/profile/progress/UserProgress";
|
||||||
@ -27,6 +28,7 @@ const UserProfile = () => {
|
|||||||
const { ndk, addSigner } = useNDKContext();
|
const { ndk, addSigner } = useNDKContext();
|
||||||
const { showToast } = useToast();
|
const { showToast } = useToast();
|
||||||
const menu = useRef(null);
|
const menu = useRef(null);
|
||||||
|
useCheckCourseProgress();
|
||||||
|
|
||||||
const copyToClipboard = (text) => {
|
const copyToClipboard = (text) => {
|
||||||
navigator.clipboard.writeText(text);
|
navigator.clipboard.writeText(text);
|
||||||
|
40
src/hooks/tracking/useCheckCourseProgress.js
Normal file
40
src/hooks/tracking/useCheckCourseProgress.js
Normal 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;
|
Loading…
x
Reference in New Issue
Block a user