mirror of
https://github.com/AustinKelsay/plebdevs.git
synced 2025-06-06 18:31:00 +00:00
Logs for course tracking hook
This commit is contained in:
parent
3c71921a19
commit
5476898b70
@ -8,39 +8,53 @@ const useTrackCourse = ({courseId, paidCourse, decryptionPerformed}) => {
|
|||||||
const completedRef = useRef(false);
|
const completedRef = useRef(false);
|
||||||
|
|
||||||
const checkOrCreateUserCourse = useCallback(async () => {
|
const checkOrCreateUserCourse = useCallback(async () => {
|
||||||
if (!session?.user || !courseId) return false;
|
if (!session?.user || !courseId) {
|
||||||
|
console.log('📝 [useTrackCourse] Missing required data:', {
|
||||||
|
hasUser: !!session?.user,
|
||||||
|
courseId
|
||||||
|
});
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
console.log('📝 [useTrackCourse] Checking course status:', { courseId });
|
||||||
const response = await axios.get(`/api/users/${session.user.id}/courses/${courseId}`);
|
const response = await axios.get(`/api/users/${session.user.id}/courses/${courseId}`);
|
||||||
// fix this condition?
|
|
||||||
if (response.status === 200 && response?.data) {
|
if (response.status === 200 && response?.data) {
|
||||||
|
console.log('✅ [useTrackCourse] Course already exists:', response.data);
|
||||||
setIsCompleted(true);
|
setIsCompleted(true);
|
||||||
completedRef.current = true;
|
completedRef.current = true;
|
||||||
} else if (response.status === 204) {
|
} else if (response.status === 204) {
|
||||||
// Only create a new UserCourse entry if it's a free course or if decryption has been performed for a paid course
|
console.log('📝 [useTrackCourse] No existing course found, checking if should create:', {
|
||||||
|
paidCourse,
|
||||||
|
decryptionPerformed
|
||||||
|
});
|
||||||
|
|
||||||
if (paidCourse === false || (paidCourse && decryptionPerformed)) {
|
if (paidCourse === false || (paidCourse && decryptionPerformed)) {
|
||||||
|
console.log('📝 [useTrackCourse] Creating new course entry');
|
||||||
await axios.post(`/api/users/${session.user.id}/courses?courseSlug=${courseId}`, {
|
await axios.post(`/api/users/${session.user.id}/courses?courseSlug=${courseId}`, {
|
||||||
completed: false,
|
completed: false,
|
||||||
started: true,
|
started: true,
|
||||||
startedAt: new Date().toISOString(),
|
startedAt: new Date().toISOString(),
|
||||||
});
|
});
|
||||||
// Call session update after creating a new UserCourse entry
|
console.log('✨ [useTrackCourse] New course entry created');
|
||||||
await update();
|
await update();
|
||||||
}
|
}
|
||||||
|
|
||||||
setIsCompleted(false);
|
setIsCompleted(false);
|
||||||
return false;
|
return false;
|
||||||
} else {
|
|
||||||
console.error('Error checking or creating UserCourse:', response.statusText);
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error checking or creating UserCourse:', error);
|
console.error('❌ [useTrackCourse] Error in checkOrCreateUserCourse:', error);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}, [courseId, paidCourse, decryptionPerformed]);
|
}, [courseId, paidCourse, decryptionPerformed]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!completedRef.current && courseId && session?.user) {
|
if (!completedRef.current && courseId && session?.user) {
|
||||||
|
console.log('📝 [useTrackCourse] Initializing course tracking:', {
|
||||||
|
courseId,
|
||||||
|
userId: session?.user?.id
|
||||||
|
});
|
||||||
checkOrCreateUserCourse();
|
checkOrCreateUserCourse();
|
||||||
}
|
}
|
||||||
}, [courseId]);
|
}, [courseId]);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user