From 9eb21e3ab4c4a7ee6436b2cbf200cecfbf46dc07 Mon Sep 17 00:00:00 2001 From: dmitriylaukhin Date: Sat, 25 Apr 2026 13:58:27 +0500 Subject: [PATCH] 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 --- src/components/admin/homework-editor.tsx | 2 +- src/components/admin/lesson-editor.tsx | 2 +- src/lib/actions/homework-actions.ts | 27 ++++++++++++++++++ src/lib/actions/lesson-actions.ts | 36 ++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 src/lib/actions/homework-actions.ts create mode 100644 src/lib/actions/lesson-actions.ts diff --git a/src/components/admin/homework-editor.tsx b/src/components/admin/homework-editor.tsx index f6d6663..19f8a5d 100644 --- a/src/components/admin/homework-editor.tsx +++ b/src/components/admin/homework-editor.tsx @@ -1,7 +1,7 @@ "use client"; 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 { lessonId: string; diff --git a/src/components/admin/lesson-editor.tsx b/src/components/admin/lesson-editor.tsx index 3982c8a..d8b205a 100644 --- a/src/components/admin/lesson-editor.tsx +++ b/src/components/admin/lesson-editor.tsx @@ -9,7 +9,7 @@ import Underline from "@tiptap/extension-underline"; import Placeholder from "@tiptap/extension-placeholder"; import { Save, Eye, FileUp, ChevronLeft, ChevronRight } from "lucide-react"; 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 { id: string; diff --git a/src/lib/actions/homework-actions.ts b/src/lib/actions/homework-actions.ts new file mode 100644 index 0000000..87d4427 --- /dev/null +++ b/src/lib/actions/homework-actions.ts @@ -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`); +} diff --git a/src/lib/actions/lesson-actions.ts b/src/lib/actions/lesson-actions.ts new file mode 100644 index 0000000..df419b5 --- /dev/null +++ b/src/lib/actions/lesson-actions.ts @@ -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}`); +}