Skip to content

Commit 141471c

Browse files
author
Rajat
committed
Re-arrange sections
1 parent 9d85021 commit 141471c

9 files changed

Lines changed: 913 additions & 226 deletions

File tree

apps/web/app/(with-contexts)/course-old/[slug]/[id]/helpers.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ export const getProduct = async (
9292
export function formatCourse(
9393
post: Course & { lessons: Lesson[]; firstLesson: string; groups: Group[] },
9494
): CourseFrontend {
95-
for (const group of sortCourseGroups(post as Course)) {
95+
const sortedGroups = sortCourseGroups(post as Course);
96+
97+
for (const group of sortedGroups) {
9698
(group as GroupWithLessons).lessons = post.lessons
9799
.filter((lesson: Lesson) => lesson.groupId === group.id)
98100
.sort(
@@ -111,7 +113,7 @@ export function formatCourse(
111113
slug: post.slug,
112114
cost: post.cost,
113115
courseId: post.courseId,
114-
groups: post.groups as GroupWithLessons[],
116+
groups: sortedGroups as GroupWithLessons[],
115117
tags: post.tags,
116118
firstLesson: post.firstLesson,
117119
paymentPlans: post.paymentPlans,
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { formatCourse } from "../helpers";
2+
3+
describe("course helpers formatCourse", () => {
4+
it("returns groups sorted by rank and lessons sorted by lessonsOrder", () => {
5+
const formatted = formatCourse({
6+
title: "Course",
7+
description: "{}",
8+
featuredImage: undefined,
9+
updatedAt: new Date().toISOString(),
10+
creatorId: "creator",
11+
slug: "course",
12+
cost: 0,
13+
courseId: "course-1",
14+
tags: [],
15+
paymentPlans: [],
16+
defaultPaymentPlan: "",
17+
firstLesson: "lesson-1",
18+
groups: [
19+
{
20+
id: "group-2",
21+
name: "Group 2",
22+
rank: 2000,
23+
lessonsOrder: ["lesson-3", "lesson-2"],
24+
},
25+
{
26+
id: "group-1",
27+
name: "Group 1",
28+
rank: 1000,
29+
lessonsOrder: ["lesson-1"],
30+
},
31+
],
32+
lessons: [
33+
{
34+
lessonId: "lesson-2",
35+
title: "Lesson 2",
36+
groupId: "group-2",
37+
},
38+
{
39+
lessonId: "lesson-1",
40+
title: "Lesson 1",
41+
groupId: "group-1",
42+
},
43+
{
44+
lessonId: "lesson-3",
45+
title: "Lesson 3",
46+
groupId: "group-2",
47+
},
48+
],
49+
} as any);
50+
51+
expect(formatted.groups.map((group) => group.id)).toEqual([
52+
"group-1",
53+
"group-2",
54+
]);
55+
expect(
56+
formatted.groups[1].lessons.map((lesson) => lesson.lessonId),
57+
).toEqual(["lesson-3", "lesson-2"]);
58+
});
59+
});

apps/web/app/(with-contexts)/course/[slug]/[id]/helpers.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ export const getProduct = async (
9292
export function formatCourse(
9393
post: Course & { lessons: Lesson[]; firstLesson: string; groups: Group[] },
9494
): CourseFrontend {
95-
for (const group of sortCourseGroups(post as Course)) {
95+
const sortedGroups = sortCourseGroups(post as Course);
96+
97+
for (const group of sortedGroups) {
9698
(group as GroupWithLessons).lessons = post.lessons
9799
.filter((lesson: Lesson) => lesson.groupId === group.id)
98100
.sort(
@@ -111,7 +113,7 @@ export function formatCourse(
111113
slug: post.slug,
112114
cost: post.cost,
113115
courseId: post.courseId,
114-
groups: post.groups as GroupWithLessons[],
116+
groups: sortedGroups as GroupWithLessons[],
115117
tags: post.tags,
116118
firstLesson: post.firstLesson,
117119
paymentPlans: post.paymentPlans,

0 commit comments

Comments
 (0)