Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ export const getProduct = async (
export function formatCourse(
post: Course & { lessons: Lesson[]; firstLesson: string; groups: Group[] },
): CourseFrontend {
for (const group of sortCourseGroups(post as Course)) {
const sortedGroups = sortCourseGroups(post as Course);

for (const group of sortedGroups) {
(group as GroupWithLessons).lessons = post.lessons
.filter((lesson: Lesson) => lesson.groupId === group.id)
.sort(
Expand All @@ -111,7 +113,7 @@ export function formatCourse(
slug: post.slug,
cost: post.cost,
courseId: post.courseId,
groups: post.groups as GroupWithLessons[],
groups: sortedGroups as GroupWithLessons[],
tags: post.tags,
firstLesson: post.firstLesson,
paymentPlans: post.paymentPlans,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { formatCourse } from "../helpers";

describe("course helpers formatCourse", () => {
it("returns groups sorted by rank and lessons sorted by lessonsOrder", () => {
const formatted = formatCourse({
title: "Course",
description: "{}",
featuredImage: undefined,
updatedAt: new Date().toISOString(),
creatorId: "creator",
slug: "course",
cost: 0,
courseId: "course-1",
tags: [],
paymentPlans: [],
defaultPaymentPlan: "",
firstLesson: "lesson-1",
groups: [
{
id: "group-2",
name: "Group 2",
rank: 2000,
lessonsOrder: ["lesson-3", "lesson-2"],
},
{
id: "group-1",
name: "Group 1",
rank: 1000,
lessonsOrder: ["lesson-1"],
},
],
lessons: [
{
lessonId: "lesson-2",
title: "Lesson 2",
groupId: "group-2",
},
{
lessonId: "lesson-1",
title: "Lesson 1",
groupId: "group-1",
},
{
lessonId: "lesson-3",
title: "Lesson 3",
groupId: "group-2",
},
],
} as any);

expect(formatted.groups.map((group) => group.id)).toEqual([
"group-1",
"group-2",
]);
expect(
formatted.groups[1].lessons.map((lesson) => lesson.lessonId),
).toEqual(["lesson-3", "lesson-2"]);
});
});
6 changes: 4 additions & 2 deletions apps/web/app/(with-contexts)/course/[slug]/[id]/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ export const getProduct = async (
export function formatCourse(
post: Course & { lessons: Lesson[]; firstLesson: string; groups: Group[] },
): CourseFrontend {
for (const group of sortCourseGroups(post as Course)) {
const sortedGroups = sortCourseGroups(post as Course);

for (const group of sortedGroups) {
(group as GroupWithLessons).lessons = post.lessons
.filter((lesson: Lesson) => lesson.groupId === group.id)
.sort(
Expand All @@ -111,7 +113,7 @@ export function formatCourse(
slug: post.slug,
cost: post.cost,
courseId: post.courseId,
groups: post.groups as GroupWithLessons[],
groups: sortedGroups as GroupWithLessons[],
tags: post.tags,
firstLesson: post.firstLesson,
paymentPlans: post.paymentPlans,
Expand Down
Loading
Loading