fix(dashboard): stop enroll effect loop and duplicate verify emails (#721)

This commit is contained in:
Rotimi Best
2026-07-10 16:05:30 +01:00
committed by GitHub
@@ -18,7 +18,8 @@
let loading = $state(false); let loading = $state(false);
let enrollmentInFlight = $state(false); let enrollmentInFlight = $state(false);
let enrollmentAttemptKey = $state(''); let enrollmentSucceeded = $state(false);
let verificationEmailSent = $state(false);
let enrolledPendingVerification = $state(false); let enrolledPendingVerification = $state(false);
const session = authClient.useSession(); const session = authClient.useSession();
@@ -87,7 +88,7 @@
} }
async function completeEnrollment() { async function completeEnrollment() {
if (!data.course?.id || enrollmentInFlight) { if (!data.course?.id || enrollmentInFlight || enrollmentSucceeded) {
return; return;
} }
@@ -105,6 +106,8 @@
return; return;
} }
enrollmentSucceeded = true;
const studentId = $profile.id || sessionUser?.id || ''; const studentId = $profile.id || sessionUser?.id || '';
const studentEmail = $profile.email || sessionUser?.email || ''; const studentEmail = $profile.email || sessionUser?.email || '';
@@ -117,7 +120,12 @@
if (!isEmailVerified) { if (!isEmailVerified) {
enrolledPendingVerification = true; enrolledPendingVerification = true;
void sendVerificationEmail();
if (!verificationEmailSent) {
verificationEmailSent = true;
void sendVerificationEmail();
}
return; return;
} }
@@ -127,7 +135,6 @@
enrollmentInFlight = false; enrollmentInFlight = false;
if (!navigatingAway) { if (!navigatingAway) {
loading = false; loading = false;
enrollmentAttemptKey = '';
} }
} }
} }
@@ -163,7 +170,7 @@
} }
$effect(() => { $effect(() => {
if (!sessionReady || !isLoggedIn || !canJoinCourse || enrollmentInFlight || loading) { if (!sessionReady || !isLoggedIn || !canJoinCourse || enrollmentInFlight || loading || enrollmentSucceeded) {
return; return;
} }
@@ -171,12 +178,6 @@
return; return;
} }
const attemptKey = `${sessionUser?.id ?? ''}:${data.course?.id ?? ''}`;
if (enrollmentAttemptKey === attemptKey) {
return;
}
enrollmentAttemptKey = attemptKey;
void completeEnrollment(); void completeEnrollment();
}); });