Skip to content

Commit 2dc9a94

Browse files
authored
Merge pull request #60 from AdamVacha/prod-update2
fixed completed subject logic, tightened server logic for dashboard
2 parents ec42e35 + 44db8d3 commit 2dc9a94

2 files changed

Lines changed: 52 additions & 44 deletions

File tree

src/routes/dashboard/+page.server.ts

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ type ProgressInfo = {
1010
nextLessonId?: number | null;
1111
nextModuleId?: number | null;
1212
subjectId: number;
13+
firstModuleInSubject: number | null;
14+
firstLessonInSubject: number | null;
1315
};
1416

1517
// Create a type for the full progress data object
@@ -22,10 +24,15 @@ export const load: PageServerLoad = async ({ locals }) => {
2224

2325
// Initialize with an index signature to allow dynamic keys
2426
const progressData: ProgressData = {
25-
html: { progress: 0, subjectId: 0 },
26-
css: { progress: 0, subjectId: 0 },
27-
javascript: { progress: 0, subjectId: 0 },
28-
backend: { progress: 0, subjectId: 0 }
27+
html: { progress: 0, subjectId: 0, firstLessonInSubject: null, firstModuleInSubject: null },
28+
css: { progress: 0, subjectId: 0, firstLessonInSubject: null, firstModuleInSubject: null },
29+
javascript: {
30+
progress: 0,
31+
subjectId: 0,
32+
firstLessonInSubject: null,
33+
firstModuleInSubject: null
34+
},
35+
backend: { progress: 0, subjectId: 0, firstLessonInSubject: null, firstModuleInSubject: null }
2936
};
3037

3138
if (session?.user?.id) {
@@ -45,7 +52,9 @@ export const load: PageServerLoad = async ({ locals }) => {
4552
if (subjectModules.length === 0) {
4653
progressData[subjectName] = {
4754
progress: 0,
48-
subjectId: subject.id
55+
subjectId: subject.id,
56+
firstLessonInSubject: 0,
57+
firstModuleInSubject: 0
4958
};
5059
continue;
5160
}
@@ -64,7 +73,9 @@ export const load: PageServerLoad = async ({ locals }) => {
6473
if (totalLessons === 0) {
6574
progressData[subjectName] = {
6675
progress: 0,
67-
subjectId: subject.id
76+
subjectId: subject.id,
77+
firstLessonInSubject: 0,
78+
firstModuleInSubject: 0
6879
};
6980
continue;
7081
}
@@ -89,22 +100,32 @@ export const load: PageServerLoad = async ({ locals }) => {
89100
let nextLessonId = null;
90101
let nextModuleId = null;
91102

103+
// Get first module and lesson for this subject (for restart case)
104+
const firstModule = subjectModules.sort(
105+
(a, b) => (a.orderInSubject || 0) - (b.orderInSubject || 0)
106+
)[0];
107+
108+
let firstLessonId: number | null = null;
109+
let firstModuleId: number | null = null;
110+
111+
if (firstModule) {
112+
firstModuleId = firstModule.id;
113+
114+
const firstLesson = subjectLessons
115+
.filter((l) => l.moduleId === firstModule.id)
116+
.sort((a, b) => (a.orderInModule || 0) - (b.orderInModule || 0))[0];
117+
118+
if (firstLesson) {
119+
firstLessonId = firstLesson.id;
120+
}
121+
}
122+
92123
// First, check if there are any completed lessons
93124
if (completedLessonIds.length === 0) {
94125
// No lessons completed yet? go to first lesson in the first module
95-
const firstModule = subjectModules.sort(
96-
(a, b) => (a.orderInSubject || 0) - (b.orderInSubject || 0)
97-
)[0];
98-
99-
if (firstModule) {
100-
const firstLesson = subjectLessons
101-
.filter((l) => l.moduleId === firstModule.id)
102-
.sort((a, b) => (a.orderInModule || 0) - (b.orderInModule || 0))[0];
103-
104-
if (firstLesson) {
105-
nextLessonId = firstLesson.id;
106-
nextModuleId = firstModule.id;
107-
}
126+
if (firstModuleId && firstLessonId) {
127+
nextLessonId = firstLessonId;
128+
nextModuleId = firstModuleId;
108129
}
109130
} else {
110131
// Has completed lessons? find the next uncompleted one
@@ -126,12 +147,14 @@ export const load: PageServerLoad = async ({ locals }) => {
126147
if (nextLessonId) break;
127148
}
128149
}
129-
150+
130151
progressData[subjectName] = {
131152
progress,
132153
nextLessonId,
133154
nextModuleId,
134-
subjectId: subject.id
155+
subjectId: subject.id,
156+
firstModuleInSubject: firstModuleId,
157+
firstLessonInSubject: firstLessonId
135158
};
136159
}
137160
}

src/routes/dashboard/+page.svelte

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,6 @@
5353
});
5454
});
5555
56-
let certificates = [
57-
{ title: 'HTML Basics', description: 'Completed HTML Foundations' },
58-
{ title: 'CSS Mastery', description: 'Completed CSS Animations' },
59-
{ title: 'JavaScript Pro', description: 'Built your first async function' }
60-
];
61-
6256
let messages = {
6357
default: `Welcome, ${username}!<br> Keep stacking your pancakes!`,
6458
html: 'HTML is the backbone of web pages. It structures your content!',
@@ -76,30 +70,21 @@
7670
speechBubbleText = messages[category] || messages.default;
7771
}
7872
79-
/**
73+
/** Navigate to the appropriate lesson for the given subject
8074
* @param {string} subject
8175
*/
8276
function navigateToLesson(subject: string) {
83-
console.log(`Navigating to ${subject}`, nextLessonLinks[subject]);
84-
console.log(`Clicking ${subject} pancake`);
85-
console.log(`Data for ${subject}:`, progressData[subject]);
86-
console.log(`Next lesson link for ${subject}:`, nextLessonLinks[subject]);
87-
88-
// Get the subjectId from mapping
89-
const subjectMap: Record<string, number> = {
90-
html: 1,
91-
css: 2,
92-
javascript: 3,
93-
backend: 4
94-
};
95-
96-
const subjectId = subjectMap[subject] || 1;
77+
// Get the data for this subject
78+
const subjectData = progressData[subject];
9779
80+
// Go to last uncompleted lesson
9881
if (nextLessonLinks[subject]) {
9982
goto(nextLessonLinks[subject] as string);
10083
} else {
101-
// Get the first module and lesson for this subject
102-
goto(`/lessons/subject/${subjectId}`);
84+
// Or go to first lesson of first module if all lessons complete
85+
goto(
86+
`/lessons/subject/${subjectData.subjectId}/module/${subjectData.firstModuleInSubject}/lesson/${subjectData.firstLessonInSubject}`
87+
);
10388
}
10489
}
10590
</script>

0 commit comments

Comments
 (0)