mirror of
https://github.com/AustinKelsay/plebdevs.git
synced 2025-06-06 18:31:00 +00:00
Logs for lesson tracking hook
This commit is contained in:
parent
9213982125
commit
069b92d634
@ -18,41 +18,57 @@ const useTrackVideoLesson = ({lessonId, videoDuration, courseId, videoPlayed, pa
|
|||||||
}, [session]);
|
}, [session]);
|
||||||
|
|
||||||
const checkOrCreateUserLesson = useCallback(async () => {
|
const checkOrCreateUserLesson = useCallback(async () => {
|
||||||
if (!session?.user) return false;
|
if (!session?.user) {
|
||||||
|
console.log('📝 [useTrackVideoLesson] No user session found');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
|
console.log('📝 [useTrackVideoLesson] Checking lesson status:', { lessonId, courseId });
|
||||||
const response = await axios.get(`/api/users/${session.user.id}/lessons/${lessonId}?courseId=${courseId}`);
|
const response = await axios.get(`/api/users/${session.user.id}/lessons/${lessonId}?courseId=${courseId}`);
|
||||||
|
|
||||||
if (response.status === 200 && response?.data) {
|
if (response.status === 200 && response?.data) {
|
||||||
|
console.log('📝 [useTrackVideoLesson] Existing lesson found:', response.data);
|
||||||
if (response?.data?.completed) {
|
if (response?.data?.completed) {
|
||||||
|
console.log('✅ [useTrackVideoLesson] Lesson already completed');
|
||||||
setIsCompleted(true);
|
setIsCompleted(true);
|
||||||
completedRef.current = true;
|
completedRef.current = true;
|
||||||
return true;
|
return true;
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
} else if (response.status === 204) {
|
} else if (response.status === 204) {
|
||||||
// Only create a new UserLesson entry if it's a free course or if decryption has been performed for a paid course
|
console.log('📝 [useTrackVideoLesson] No existing lesson found, checking if should create:', {
|
||||||
|
paidCourse,
|
||||||
|
decryptionPerformed
|
||||||
|
});
|
||||||
|
|
||||||
if (paidCourse === false || (paidCourse && decryptionPerformed)) {
|
if (paidCourse === false || (paidCourse && decryptionPerformed)) {
|
||||||
|
console.log('📝 [useTrackVideoLesson] Creating new lesson entry');
|
||||||
await axios.post(`/api/users/${session.user.id}/lessons?courseId=${courseId}`, {
|
await axios.post(`/api/users/${session.user.id}/lessons?courseId=${courseId}`, {
|
||||||
resourceId: lessonId,
|
resourceId: lessonId,
|
||||||
opened: true,
|
opened: true,
|
||||||
openedAt: new Date().toISOString(),
|
openedAt: new Date().toISOString(),
|
||||||
});
|
});
|
||||||
// Call session update after creating a new UserLesson entry
|
console.log('✨ [useTrackVideoLesson] New lesson entry created');
|
||||||
await update();
|
await update();
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
} else {
|
|
||||||
console.error('Error checking or creating UserLesson:', response.statusText);
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error checking or creating UserLesson:', error);
|
console.error('❌ [useTrackVideoLesson] Error in checkOrCreateUserLesson:', error);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}, [session, lessonId, courseId, update, paidCourse, decryptionPerformed]);
|
}, [session, lessonId, courseId, update, paidCourse, decryptionPerformed]);
|
||||||
|
|
||||||
const markLessonAsCompleted = useCallback(async () => {
|
const markLessonAsCompleted = useCallback(async () => {
|
||||||
if (!session?.user || completedRef.current) return;
|
if (!session?.user || completedRef.current) {
|
||||||
|
console.log('📝 [useTrackVideoLesson] Skipping completion:', {
|
||||||
|
hasUser: !!session?.user,
|
||||||
|
alreadyCompleted: completedRef.current
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('📝 [useTrackVideoLesson] Marking lesson as completed:', { lessonId, courseId });
|
||||||
completedRef.current = true;
|
completedRef.current = true;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -64,22 +80,32 @@ const useTrackVideoLesson = ({lessonId, videoDuration, courseId, videoPlayed, pa
|
|||||||
if (response.status === 200) {
|
if (response.status === 200) {
|
||||||
setIsCompleted(true);
|
setIsCompleted(true);
|
||||||
setIsTracking(false);
|
setIsTracking(false);
|
||||||
// Call session update after marking the lesson as completed
|
console.log('✅ [useTrackVideoLesson] Lesson marked as completed successfully');
|
||||||
await update();
|
await update();
|
||||||
} else {
|
} else {
|
||||||
console.error('Failed to mark lesson as completed:', response.statusText);
|
console.error('Failed to mark lesson as completed:', response.statusText);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error marking lesson as completed:', error);
|
console.error('❌ [useTrackVideoLesson] Error marking lesson as completed:', error);
|
||||||
}
|
}
|
||||||
}, [lessonId, courseId, session, update]);
|
}, [lessonId, courseId, session, update]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const initializeTracking = async () => {
|
const initializeTracking = async () => {
|
||||||
if (isAdmin) return;
|
if (isAdmin) {
|
||||||
|
console.log('👑 [useTrackVideoLesson] Admin user - skipping tracking');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('📝 [useTrackVideoLesson] Initializing tracking:', {
|
||||||
|
videoDuration,
|
||||||
|
videoPlayed,
|
||||||
|
paidCourse,
|
||||||
|
decryptionPerformed
|
||||||
|
});
|
||||||
|
|
||||||
const alreadyCompleted = await checkOrCreateUserLesson();
|
const alreadyCompleted = await checkOrCreateUserLesson();
|
||||||
if (!alreadyCompleted && videoDuration && !completedRef.current && videoPlayed && (paidCourse === false || (paidCourse && decryptionPerformed))) {
|
if (!alreadyCompleted && videoDuration && !completedRef.current && videoPlayed && (!paidCourse || (paidCourse && decryptionPerformed))) {
|
||||||
setIsTracking(true);
|
setIsTracking(true);
|
||||||
timerRef.current = setInterval(() => {
|
timerRef.current = setInterval(() => {
|
||||||
setTimeSpent(prevTime => {
|
setTimeSpent(prevTime => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user