Skip to content

Commit 09be96a

Browse files
committed
still working on the admin/courses/[id] page. right now im implementing being able to change order of course.
1 parent a08fb98 commit 09be96a

File tree

14 files changed

+1361
-83
lines changed

14 files changed

+1361
-83
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"esbuild"
1919
],
2020
"dependencies": {
21+
"@hello-pangea/dnd": "^18.0.1",
2122
"@hookform/resolvers": "^5.2.2",
2223
"@libsql/client": "^0.15.15",
2324
"@radix-ui/react-accordion": "^1.2.12",

pnpm-lock.yaml

Lines changed: 75 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/actions/admin/lesson.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
import { revalidatePath } from "next/cache";
33
import { db } from "@/db/index";
44
import { lessons, units } from "@/db/schema";
5+
import type { Lesson } from "@/db/schema";
56
import { lessonFormSchema } from "@/lib/validations/lesson";
67
import { actionClient } from "@/lib/safe-action";
7-
import { eq, asc } from "drizzle-orm";
8+
import { eq, asc, sql } from "drizzle-orm";
89

910
export const createLessonAction = actionClient
1011
.schema(lessonFormSchema)
@@ -18,16 +19,26 @@ export const createLessonAction = actionClient
1819
contentUrl,
1920
} = parsedInput;
2021

22+
const [maxPosition] = await db
23+
.select({ maxPosition: sql<number>`MAX(${lessons.position})` })
24+
.from(lessons)
25+
.where(eq(lessons.unitId, unitId))
26+
27+
const newPosition = (maxPosition?.maxPosition ?? 0) + 1;
28+
2129
const metadata = JSON.stringify({
2230
title,
2331
description: description ?? "",
2432
});
2533

2634
await db.insert(lessons).values({
2735
unitId,
36+
title,
37+
description,
2838
mediaType, // now real value from the form
2939
metadata,
3040
contentUrl, // real URL from the form
41+
position: newPosition,
3142
// contentBlobId stays null
3243
});
3344

@@ -36,20 +47,25 @@ export const createLessonAction = actionClient
3647
return { success: true };
3748
});
3849

39-
export async function getLessonsForCourse(courseId: number) {
40-
return await db
50+
export async function getLessonsForCourse(courseId: number): Promise<Lesson[]> {
51+
const rows = await db
4152
.select({
42-
lessonId: lessons.id,
53+
id: lessons.id,
4354
unitId: lessons.unitId,
55+
title: lessons.title,
56+
description: lessons.description,
4457
mediaType: lessons.mediaType,
4558
contentUrl: lessons.contentUrl,
4659
contentBlobId: lessons.contentBlobId,
4760
metadata: lessons.metadata,
48-
lessonPosition: lessons.position,
49-
unitPosition: units.position,
61+
position: lessons.position,
62+
createdAt: lessons.createdAt,
63+
updatedAt: lessons.updatedAt,
5064
})
5165
.from(units)
5266
.innerJoin(lessons, eq(units.id, lessons.unitId))
5367
.where(eq(units.courseId, courseId))
5468
.orderBy(asc(units.position), asc(lessons.position));
69+
70+
return rows;
5571
}

src/app/admin/courses/[courseId]/page.tsx

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
// import CourseId from "@/components/admin/CourseId";
1+
import { Button } from "@/components/ui/button";
2+
import {
3+
Card,
4+
CardHeader,
5+
CardTitle,
6+
CardDescription,
7+
CardContent,
8+
} from "@/components/ui/card";
9+
10+
import CourseController from "@/components/admin/course/CourseController";
211
import { getCourseById } from "@/actions/admin/course";
312
import { getUnitsForCourse } from "@/actions/admin/units";
413
import { getLessonsForCourse } from "@/actions/admin/lesson";
5-
import { Button } from "@/components/ui/button";
614
import Link from "next/link";
715

816
export default async function CourseIdPage({
@@ -21,14 +29,33 @@ export default async function CourseIdPage({
2129
console.log("Units:", units);
2230
console.log("Lessons:", lessons);
2331

32+
if (!course) {
33+
return (
34+
// Should be 404 we need one
35+
<div className="">empty</div>
36+
);
37+
}
38+
2439
return (
25-
<div className="">
26-
<Link href={`/admin/courses/${id}/units/create`}>
27-
<Button variant={"outline"} className="cursor-pointer">Create Unit</Button>
28-
</Link>
29-
<Link href={`/admin/courses/${id}/lesson/create`}>
30-
<Button variant={"outline"} className="cursor-pointer">Create Lesson</Button>
31-
</Link>
40+
<div className="flex flex-col gap-4">
41+
<div className="flex justify-end gap-4">
42+
<Link href={`/admin/courses/${id}/lesson/create`}>
43+
<Button variant={"outline"} className="cursor-pointer">Create Lesson</Button>
44+
</Link>
45+
<Link href={`/admin/courses/${id}/units/create`}>
46+
<Button variant={"outline"} className="cursor-pointer">Create Unit</Button>
47+
</Link>
48+
</div>
49+
<Card>
50+
<CardTitle className="px-6 text-3xl">
51+
<span className="font-semibold">Course: </span>
52+
<span className="font-light">{course.title}</span>
53+
</CardTitle>
54+
<CardDescription className="px-6 text-md">{course.description}</CardDescription>
55+
<CardContent>
56+
<CourseController units={units} lessons={lessons} />
57+
</CardContent>
58+
</Card>
3259
</div>
3360
)
3461
}

src/components/admin/CourseId.tsx

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)