Move Server Actions to static paths to fix RSC temporary client reference error
Server Actions imported from dynamic route paths ([courseId]/[moduleId]/[lessonId]) caused "Cannot access toStringTag on the server" crash after saving a lesson. Moved saveLesson, saveHomework, deleteHomework to src/lib/actions/*.ts. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useState, useTransition } from "react";
|
import { useState, useTransition } from "react";
|
||||||
import { saveHomework, deleteHomework } from "@/app/admin/courses/[courseId]/modules/[moduleId]/lessons/[lessonId]/homework-actions";
|
import { saveHomework, deleteHomework } from "@/lib/actions/homework-actions";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
lessonId: string;
|
lessonId: string;
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import Underline from "@tiptap/extension-underline";
|
|||||||
import Placeholder from "@tiptap/extension-placeholder";
|
import Placeholder from "@tiptap/extension-placeholder";
|
||||||
import { Save, Eye, FileUp, ChevronLeft, ChevronRight } from "lucide-react";
|
import { Save, Eye, FileUp, ChevronLeft, ChevronRight } from "lucide-react";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import { saveLesson } from "@/app/admin/courses/[courseId]/modules/[moduleId]/lessons/[lessonId]/actions";
|
import { saveLesson } from "@/lib/actions/lesson-actions";
|
||||||
|
|
||||||
interface LessonData {
|
interface LessonData {
|
||||||
id: string;
|
id: string;
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
"use server";
|
||||||
|
|
||||||
|
import { prisma } from "@/lib/prisma";
|
||||||
|
import { auth } from "@/lib/auth";
|
||||||
|
import { headers } from "next/headers";
|
||||||
|
import { revalidatePath } from "next/cache";
|
||||||
|
|
||||||
|
async function requireAdmin() {
|
||||||
|
const session = await auth.api.getSession({ headers: await headers() });
|
||||||
|
if (!session || session.user.role !== "admin") throw new Error("Forbidden");
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function saveHomework(lessonId: string, description: string) {
|
||||||
|
await requireAdmin();
|
||||||
|
await prisma.homework.upsert({
|
||||||
|
where: { lessonId },
|
||||||
|
update: { description },
|
||||||
|
create: { lessonId, description },
|
||||||
|
});
|
||||||
|
revalidatePath(`/admin/courses`);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deleteHomework(lessonId: string) {
|
||||||
|
await requireAdmin();
|
||||||
|
await prisma.homework.delete({ where: { lessonId } });
|
||||||
|
revalidatePath(`/admin/courses`);
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
"use server";
|
||||||
|
|
||||||
|
import { prisma } from "@/lib/prisma";
|
||||||
|
import { headers } from "next/headers";
|
||||||
|
import { auth } from "@/lib/auth";
|
||||||
|
import { revalidatePath } from "next/cache";
|
||||||
|
|
||||||
|
async function requireAdmin() {
|
||||||
|
const session = await auth.api.getSession({ headers: await headers() });
|
||||||
|
if (!session || session.user.role !== "admin") throw new Error("Forbidden");
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function saveLesson(
|
||||||
|
lessonId: string,
|
||||||
|
courseId: string,
|
||||||
|
moduleId: string,
|
||||||
|
data: {
|
||||||
|
title: string;
|
||||||
|
kinescopeId: string;
|
||||||
|
content: object;
|
||||||
|
published: boolean;
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
await requireAdmin();
|
||||||
|
await prisma.lesson.update({
|
||||||
|
where: { id: lessonId },
|
||||||
|
data: {
|
||||||
|
title: data.title,
|
||||||
|
kinescopeId: data.kinescopeId || null,
|
||||||
|
content: data.content,
|
||||||
|
published: data.published,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
revalidatePath(`/admin/courses/${courseId}/modules/${moduleId}`);
|
||||||
|
revalidatePath(`/admin/courses/${courseId}/modules/${moduleId}/lessons/${lessonId}`);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user