Module Update
This commit is contained in:
196
src/modules/textbooks/actions.ts
Normal file
196
src/modules/textbooks/actions.ts
Normal file
@@ -0,0 +1,196 @@
|
||||
"use server";
|
||||
|
||||
import { revalidatePath } from "next/cache";
|
||||
import {
|
||||
createTextbook,
|
||||
createChapter,
|
||||
updateChapterContent,
|
||||
deleteChapter,
|
||||
createKnowledgePoint,
|
||||
deleteKnowledgePoint,
|
||||
updateTextbook,
|
||||
deleteTextbook
|
||||
} from "./data-access";
|
||||
import { CreateTextbookInput, UpdateTextbookInput } from "./types";
|
||||
|
||||
export type ActionState = {
|
||||
success: boolean;
|
||||
message?: string;
|
||||
errors?: Record<string, string[]>;
|
||||
};
|
||||
|
||||
// ... existing createTextbookAction ...
|
||||
|
||||
export async function createTextbookAction(
|
||||
prevState: ActionState | null,
|
||||
formData: FormData
|
||||
): Promise<ActionState> {
|
||||
// ... implementation same as before
|
||||
const rawData: CreateTextbookInput = {
|
||||
title: formData.get("title") as string,
|
||||
subject: formData.get("subject") as string,
|
||||
grade: formData.get("grade") as string,
|
||||
publisher: formData.get("publisher") as string,
|
||||
};
|
||||
|
||||
if (!rawData.title || !rawData.subject || !rawData.grade) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Please fill in all required fields.",
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
await createTextbook(rawData);
|
||||
revalidatePath("/teacher/textbooks");
|
||||
return {
|
||||
success: true,
|
||||
message: "Textbook created successfully.",
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Failed to create textbook:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Failed to create textbook.",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export async function updateTextbookAction(
|
||||
textbookId: string,
|
||||
prevState: ActionState | null,
|
||||
formData: FormData
|
||||
): Promise<ActionState> {
|
||||
const rawData: UpdateTextbookInput = {
|
||||
id: textbookId,
|
||||
title: formData.get("title") as string,
|
||||
subject: formData.get("subject") as string,
|
||||
grade: formData.get("grade") as string,
|
||||
publisher: formData.get("publisher") as string,
|
||||
};
|
||||
|
||||
if (!rawData.title || !rawData.subject || !rawData.grade) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Please fill in all required fields.",
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
await updateTextbook(rawData);
|
||||
revalidatePath(`/teacher/textbooks/${textbookId}`);
|
||||
return {
|
||||
success: true,
|
||||
message: "Textbook updated successfully.",
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Failed to update textbook:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Failed to update textbook.",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteTextbookAction(
|
||||
textbookId: string
|
||||
): Promise<ActionState> {
|
||||
try {
|
||||
await deleteTextbook(textbookId);
|
||||
revalidatePath("/teacher/textbooks");
|
||||
return {
|
||||
success: true,
|
||||
message: "Textbook deleted successfully.",
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Failed to delete textbook:", error);
|
||||
return {
|
||||
success: false,
|
||||
message: "Failed to delete textbook.",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export async function createChapterAction(
|
||||
textbookId: string,
|
||||
parentId: string | undefined,
|
||||
prevState: ActionState | null,
|
||||
formData: FormData
|
||||
): Promise<ActionState> {
|
||||
const title = formData.get("title") as string;
|
||||
|
||||
if (!title) return { success: false, message: "Title is required" };
|
||||
|
||||
try {
|
||||
await createChapter({
|
||||
textbookId,
|
||||
title,
|
||||
parentId,
|
||||
order: 0 // Default order
|
||||
});
|
||||
revalidatePath(`/teacher/textbooks/${textbookId}`);
|
||||
return { success: true, message: "Chapter created successfully" };
|
||||
} catch (error) {
|
||||
return { success: false, message: "Failed to create chapter" };
|
||||
}
|
||||
}
|
||||
|
||||
export async function updateChapterContentAction(
|
||||
chapterId: string,
|
||||
content: string,
|
||||
textbookId: string
|
||||
): Promise<ActionState> {
|
||||
try {
|
||||
await updateChapterContent({ chapterId, content });
|
||||
revalidatePath(`/teacher/textbooks/${textbookId}`);
|
||||
return { success: true, message: "Content updated successfully" };
|
||||
} catch (error) {
|
||||
return { success: false, message: "Failed to update content" };
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteChapterAction(
|
||||
chapterId: string,
|
||||
textbookId: string
|
||||
): Promise<ActionState> {
|
||||
try {
|
||||
await deleteChapter(chapterId);
|
||||
revalidatePath(`/teacher/textbooks/${textbookId}`);
|
||||
return { success: true, message: "Chapter deleted successfully" };
|
||||
} catch (error) {
|
||||
return { success: false, message: "Failed to delete chapter" };
|
||||
}
|
||||
}
|
||||
|
||||
export async function createKnowledgePointAction(
|
||||
chapterId: string,
|
||||
textbookId: string,
|
||||
prevState: ActionState | null,
|
||||
formData: FormData
|
||||
): Promise<ActionState> {
|
||||
const name = formData.get("name") as string;
|
||||
const description = formData.get("description") as string;
|
||||
|
||||
if (!name) return { success: false, message: "Name is required" };
|
||||
|
||||
try {
|
||||
await createKnowledgePoint({ name, description, chapterId });
|
||||
revalidatePath(`/teacher/textbooks/${textbookId}`);
|
||||
return { success: true, message: "Knowledge point created successfully" };
|
||||
} catch (error) {
|
||||
return { success: false, message: "Failed to create knowledge point" };
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteKnowledgePointAction(
|
||||
kpId: string,
|
||||
textbookId: string
|
||||
): Promise<ActionState> {
|
||||
try {
|
||||
await deleteKnowledgePoint(kpId);
|
||||
revalidatePath(`/teacher/textbooks/${textbookId}`);
|
||||
return { success: true, message: "Knowledge point deleted successfully" };
|
||||
} catch (error) {
|
||||
return { success: false, message: "Failed to delete knowledge point" };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user