Skip to content

Commit 278c910

Browse files
fix(lessons): fix lesson completion toggle, progress tracking, and XP toast
- Add missing DELETE RLS policy on lesson_completions so students can un-complete lessons - Remove invalid tenant_id filter on lesson_completions query (table has no tenant_id column), fixing sidebar progress always showing 0% - Fix i18n namespace from 'gamification' to 'components.gamification' for XP award toast - Add error handling on insert/delete operations with user-facing error toasts Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 4751b25 commit 278c910

3 files changed

Lines changed: 24 additions & 5 deletions

File tree

app/[locale]/dashboard/student/courses/[courseId]/lessons/[lessonId]/lesson-navigation.tsx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function LessonNavigation({
3838
requireSequentialCompletion = false,
3939
}: LessonNavigationProps) {
4040
const t = useTranslations('components.lessonNavigation')
41-
const tGamification = useTranslations('gamification')
41+
const tGamification = useTranslations('components.gamification')
4242
const [loading, setLoading] = useState(false)
4343
const [completed, setCompleted] = useState(isCompleted)
4444
const [certificateCode, setCertificateCode] = useState<string | null>(null)
@@ -59,21 +59,35 @@ export function LessonNavigation({
5959
}
6060

6161
if (completed) {
62-
await supabase
62+
const { error } = await supabase
6363
.from('lesson_completions')
6464
.delete()
6565
.eq('lesson_id', lessonId)
6666
.eq('user_id', user.id)
6767

68+
if (error) {
69+
console.error('Failed to uncomplete lesson:', error)
70+
toast.error('Failed to update lesson status')
71+
setLoading(false)
72+
return
73+
}
74+
6875
setCompleted(false)
6976
setLoading(false)
7077
startTransition(() => { router.refresh() })
7178
} else {
72-
await supabase.from('lesson_completions').insert({
79+
const { error } = await supabase.from('lesson_completions').insert({
7380
lesson_id: lessonId,
7481
user_id: user.id,
7582
})
7683

84+
if (error) {
85+
console.error('Failed to complete lesson:', error)
86+
toast.error('Failed to mark lesson as complete')
87+
setLoading(false)
88+
return
89+
}
90+
7791
setCompleted(true)
7892
setLoading(false)
7993
toast.success(tGamification('xpAwarded.lesson_completion'))

app/[locale]/dashboard/student/courses/[courseId]/lessons/[lessonId]/page.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,7 @@ export default async function LessonPage({ params }: PageProps) {
179179
supabase
180180
.from('lesson_completions')
181181
.select('lesson_id')
182-
.eq('user_id', userId)
183-
.eq('tenant_id', tenantId),
182+
.eq('user_id', userId),
184183
supabase
185184
.from('lesson_resources')
186185
.select('id, file_name, file_size, mime_type')
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- Add missing DELETE policy on lesson_completions
2+
-- Students need to be able to un-complete lessons (toggle)
3+
CREATE POLICY "Students can delete own completions"
4+
ON lesson_completions
5+
FOR DELETE
6+
USING (auth.uid() = user_id);

0 commit comments

Comments
 (0)